File tree 6 files changed +243
-0
lines changed
6 files changed +243
-0
lines changed Original file line number Diff line number Diff line change
1
+ #include < bits/stdc++.h>
2
+
3
+ using namespace std ;
4
+
5
+ int testCase, n, m;
6
+ int arr[20 ][20 ];
7
+ int dp[20 ][20 ];
8
+
9
+ int main (void ) {
10
+ // 테스트 케이스(Test Case) 입력
11
+ cin >> testCase;
12
+ for (int tc = 0 ; tc < testCase; tc++) {
13
+ // 금광 정보 입력
14
+ cin >> n >> m;
15
+ for (int i = 0 ; i < n; i++) {
16
+ for (int j = 0 ; j < m; j++) {
17
+ cin >> arr[i][j];
18
+ }
19
+ }
20
+ // 다이나믹 프로그래밍을 위한 2차원 DP 테이블 초기화
21
+ int index = 0 ;
22
+ for (int i = 0 ; i < n; i++) {
23
+ for (int j = 0 ; j < m; j++) {
24
+ dp[i][j] = arr[i][j];
25
+ }
26
+ }
27
+ // 다이나믹 프로그래밍 진행
28
+ for (int j = 1 ; j < m; j++) {
29
+ for (int i = 0 ; i < n; i++) {
30
+ int leftUp, leftDown, left;
31
+ // 왼쪽 위에서 오는 경우
32
+ if (i == 0 ) leftUp = 0 ;
33
+ else leftUp = dp[i - 1 ][j - 1 ];
34
+ // 왼쪽 아래에서 오는 경우
35
+ if (i == n - 1 ) leftDown = 0 ;
36
+ else leftDown = dp[i + 1 ][j - 1 ];
37
+ // 왼쪽에서 오는 경우
38
+ left = dp[i][j - 1 ];
39
+ dp[i][j] = dp[i][j] + max (leftUp, max (leftDown, left));
40
+ }
41
+ }
42
+ int result = 0 ;
43
+ for (int i = 0 ; i < n; i++) {
44
+ result = max (result, dp[i][m - 1 ]);
45
+ }
46
+ cout << result << ' \n ' ;
47
+ }
48
+ }
Original file line number Diff line number Diff line change
1
+ #include < bits/stdc++.h>
2
+
3
+ using namespace std ;
4
+
5
+ int n;
6
+ int dp[500 ][500 ]; // 다이나믹 프로그래밍을 위한 DP 테이블 초기화
7
+
8
+ int main (void ) {
9
+ cin >> n;
10
+ for (int i = 0 ; i < n; i++) {
11
+ for (int j = 0 ; j < i + 1 ; j++) {
12
+ cin >> dp[i][j];
13
+ }
14
+ }
15
+ // 다이나믹 프로그래밍으로 2번째 줄부터 내려가면서 확인
16
+ for (int i = 1 ; i < n; i++) {
17
+ for (int j = 0 ; j <= i; j++) {
18
+ int upLeft, up;
19
+ // 왼쪽 위에서 내려오는 경우
20
+ if (j == 0 ) upLeft = 0 ;
21
+ else upLeft = dp[i - 1 ][j - 1 ];
22
+ // 바로 위에서 내려오는 경우
23
+ if (j == i) up = 0 ;
24
+ else up = dp[i - 1 ][j];
25
+ // 최대 합을 저장
26
+ dp[i][j] = dp[i][j] + max (upLeft, up);
27
+ }
28
+ }
29
+ int result = 0 ;
30
+ for (int i = 0 ; i < n; i++) {
31
+ result = max (result, dp[n - 1 ][i]);
32
+ }
33
+ cout << result << ' \n ' ;
34
+ }
Original file line number Diff line number Diff line change
1
+ #include < bits/stdc++.h>
2
+
3
+ using namespace std ;
4
+
5
+ int n; // 전체 상담 개수
6
+ vector<int > t; // 각 상담을 완료하는데 걸리는 기간
7
+ vector<int > p; // 각 상담을 완료했을 때 받을 수 있는 금액
8
+ int dp[15 ]; // 다이나믹 프로그래밍을 위한 1차원 DP 테이블 초기화
9
+ int maxValue;
10
+
11
+ int main (void ) {
12
+ cin >> n;
13
+
14
+ for (int i = 0 ; i < n; i++) {
15
+ int x, y;
16
+ cin >> x >> y;
17
+ t.push_back (x);
18
+ p.push_back (y);
19
+ }
20
+
21
+ // 리스트를 뒤에서부터 거꾸로 확인
22
+ for (int i = n - 1 ; i >= 0 ; i--) {
23
+ int time = t[i] + i;
24
+ // 상담이 기간 안에 끝나는 경우
25
+ if (time <= n) {
26
+ // 점화식에 맞게, 현재까지의 최고 이익 계산
27
+ dp[i] = max (p[i] + dp[time ], maxValue);
28
+ maxValue = dp[i];
29
+ }
30
+ // 상담이 기간을 벗어나는 경우
31
+ else dp[i] = maxValue;
32
+ }
33
+
34
+ cout << maxValue << ' \n ' ;
35
+ }
Original file line number Diff line number Diff line change
1
+ #include < bits/stdc++.h>
2
+
3
+ using namespace std ;
4
+
5
+ int n;
6
+ vector<int > v;
7
+
8
+ int main (void ) {
9
+ cin >> n;
10
+
11
+ for (int i = 0 ; i < n; i++) {
12
+ int x;
13
+ cin >> x;
14
+ v.push_back (x);
15
+ }
16
+
17
+ // 순서를 뒤집어 '최장 증가 부분 수열' 문제로 변환
18
+ reverse (v.begin (), v.end ());
19
+
20
+ // 다이나믹 프로그래밍을 위한 1차원 DP 테이블 초기화
21
+ int dp[2000 ];
22
+ for (int i = 0 ; i < n; i++) {
23
+ dp[i] = 1 ;
24
+ }
25
+
26
+ // 가장 긴 증가하는 부분 수열(LIS) 알고리즘 수행
27
+ for (int i = 1 ; i < n; i++) {
28
+ for (int j = 0 ; j < i; j++) {
29
+ if (v[j] < v[i]) {
30
+ dp[i] = max (dp[i], dp[j] + 1 );
31
+ }
32
+ }
33
+ }
34
+
35
+ // 열외해야 하는 병사의 최소 수를 출력
36
+ int maxValue = 0 ;
37
+ for (int i = 0 ; i < n; i++) {
38
+ maxValue = max (maxValue, dp[i]);
39
+ }
40
+ cout << n - maxValue << ' \n ' ;
41
+ }
Original file line number Diff line number Diff line change
1
+ #include < bits/stdc++.h>
2
+
3
+ using namespace std ;
4
+
5
+ int n;
6
+ int ugly[1000 ]; // 못생긴 수를 담기 위한 테이블 (1차원 DP 테이블)
7
+
8
+ int main (void ) {
9
+ cin >> n;
10
+
11
+ // 2배, 3배, 5배를 위한 인덱스
12
+ int i2 = 0 , i3 = 0 , i5 = 0 ;
13
+ // 처음에 곱셈 값을 초기화
14
+ int next2 = 2 , next3 = 3 , next5 = 5 ;
15
+
16
+ ugly[0 ] = 1 ; // 첫 번째 못생긴 수는 1
17
+ // 1부터 n까지의 못생긴 수들을 찾기
18
+ for (int l = 1 ; l < n; l++) {
19
+ // 가능한 곱셈 결과 중에서 가장 작은 수를 선택
20
+ ugly[l] = min (next2, min (next3, next5));
21
+ // 인덱스에 따라서 곱셈 결과를 증가
22
+ if (ugly[l] == next2) {
23
+ i2 += 1 ;
24
+ next2 = ugly[i2] * 2 ;
25
+ }
26
+ if (ugly[l] == next3) {
27
+ i3 += 1 ;
28
+ next3 = ugly[i3] * 3 ;
29
+ }
30
+ if (ugly[l] == next5) {
31
+ i5 += 1 ;
32
+ next5 = ugly[i5] * 5 ;
33
+ }
34
+ }
35
+
36
+ // n번째 못생긴 수를 출력
37
+ cout << ugly[n - 1 ] << ' \n ' ;
38
+ }
Original file line number Diff line number Diff line change
1
+ #include < bits/stdc++.h>
2
+
3
+ using namespace std ;
4
+
5
+ // 두 문자열을 입력 받기
6
+ string str1;
7
+ string str2;
8
+
9
+ // 최소 편집 거리(Edit Distance) 계산을 위한 다이나믹 프로그래밍
10
+ int editDist (string str1, string str2) {
11
+ int n = str1.size ();
12
+ int m = str2.size ();
13
+
14
+ // 다이나믹 프로그래밍을 위한 2차원 DP 테이블 초기화
15
+ vector<vector<int > > dp (n + 1 , vector<int >(m + 1 ));
16
+
17
+ // DP 테이블 초기 설정
18
+ for (int i = 1 ; i <= n; i++) {
19
+ dp[i][0 ] = i;
20
+ }
21
+ for (int j = 1 ; j <= m; j++) {
22
+ dp[0 ][j] = j;
23
+ }
24
+
25
+ // 최소 편집 거리 계산
26
+ for (int i = 1 ; i <= n; i++) {
27
+ for (int j = 1 ; j <= m; j++) {
28
+ // 문자가 같다면, 왼쪽 위에 해당하는 수를 그대로 대입
29
+ if (str1[i - 1 ] == str2[j - 1 ]) {
30
+ dp[i][j] = dp[i - 1 ][j - 1 ];
31
+ }
32
+ // 문자가 다르다면, 세 가지 경우 중에서 최솟값 찾기
33
+ else { // 삽입(왼쪽), 삭제(위쪽), 교체(왼쪽 위) 중에서 최소 비용을 찾아 대입
34
+ dp[i][j] = 1 + min (dp[i][j - 1 ], min (dp[i - 1 ][j], dp[i - 1 ][j - 1 ]));
35
+ }
36
+ }
37
+ }
38
+
39
+ return dp[n][m];
40
+ }
41
+
42
+ int main (void ) {
43
+ cin >> str1 >> str2;
44
+
45
+ // 최소 편집 거리 출력
46
+ cout << editDist (str1, str2) << ' \n ' ;
47
+ }
You can’t perform that action at this time.
0 commit comments