Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- 백준
- 완전탐색
- INNER JOIN
- 코딩테스트
- JOIN
- bixby studio
- 네트워크 플로우
- maximum flow
- 후기
- 분할정복
- 빅스비
- backjoon
- DP
- SWEA
- SQL
- Network Flow
- ICPC
- SDS 알고특강
- 메모이제이션
- SWTest
- BOJ
- 세그먼트트리
- 최대유량
- 빅스비 스튜디오
- 알고리즘
- 최대 유량
- Baekjoon
- 삼성
- 이분탐색
- 프로그래머스
Archives
- Today
- Total
답은 알고리즘 뿐이야!
[BOJ 2449] 전구 본문
문제 출처 : https://www.acmicpc.net/problem/2449
문제 풀이 :
DP 문제입니다.
cache[L][R] : L번째 전구부터 R번째 전구까지 통일 시키는데 색을 바꾼 최소횟수
양쪽 구간의 색을 통일 시킬때 변수를 하나 두시고 양쪽 구간의 시작 색깔이 같다면 바꾸는 비용을 0으로, 다르다면 바꾸는 비용을 1로 계산하여 쌓아가시면 됩니다.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<iostream> | |
#include<algorithm> | |
using namespace std; | |
int n, k, arr[200], cache[200][200]; | |
int func(int l, int r) { | |
if (l >= r)return 0; | |
int& ret = cache[l][r]; | |
if (ret)return ret; | |
ret = 1e9; | |
for (int i = l; i < r; i++) { | |
int p = arr[l] == arr[i+1] ? 0 : 1; | |
ret = min(ret, func(l, i) + func(i + 1, r) + p); | |
} | |
return ret; | |
} | |
int main() { | |
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); | |
cin >> n >> k; | |
for (int i = 0; i < n; i++)cin >> arr[i]; | |
cout << func(0, n - 1); | |
} |
'알고리즘 > 백준문제풀이' 카테고리의 다른 글
[BOJ 10319] 좀비 아포칼립스 (0) | 2020.08.18 |
---|---|
[BOJ 13505] 두 수 XOR (0) | 2020.08.17 |
[BOJ 2342] Dance Dance Revolution (0) | 2020.08.16 |
[BOJ 2207] 가위바위보 (0) | 2020.06.17 |
[BOJ 14725] 개미굴 (0) | 2020.05.30 |