10000 Update 14501_1.cpp · kimhaech/basic-algo-lecture@839be9b · GitHub
[go: up one dir, main page]

Skip to content

Commit 839be9b

Browse files
Update 14501_1.cpp
1 parent c4163f4 commit 839be9b

File tree

1 file changed

+23
-19
lines changed

1 file changed

+23
-19
lines changed

0x10/solutions/14501_1.cpp

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,34 @@
1-
// Authored by : sukam09
2-
// Co-authored by : -
3-
// http://boj.kr/f954e10df8224f1189c74c9a8566396d
1+
// Authored by : Hot6Mania
2+
// Co-authored by : BaaaaaaaaaaarkingDog
3+
// http://boj.kr/5d3cab9ae35648e3b0c6613432975e97
44
#include <bits/stdc++.h>
55
using namespace std;
66

7-
int t[20];
8-
int p[20];
9-
int d[20]; // i번째 일에 상담을 시작했을 때 얻을 수 있는 최대 수익
7+
int n;
8+
int t[20], p[20], d[20];
9+
10+
// d[i] : i-1번째 날까지 상담을 했을 때 벌 수 있는 최대 금액
1011

1112
int main(void){
1213
ios::sync_with_stdio(0);
1314
cin.tie(0);
1415

15-
int n;
1616
cin >> n;
17+
for(int i = 1; i <= n; ++i) cin >> t[i] >> p[i];
1718

18-
for (int i = 1; i <= n; i++) cin >> t[i] >> p[i];
19-
20-
for (int i = n; i >= 1; i--) {
21-
// i번째 일에 상담을 할 수 있을 경우
22-
if (i + t[i] <= n + 1) {
23-
// i번째 일에 상담을 했을 때와 상담을 하지 않았을 때 얻을 수 있는 수익 중 최대 수익을 취함
24-
d[i] = max(d[i + t[i]] + p[i], d[i + 1]);
25-
}
26-
else d[i] = d[i + 1];
27-
}
19+
for(int i = 1; i <= n; ++i){
20+
// d[i]값 확정
21+
d[i] = max(d[i], d[i-1]);
22+
23+
// i번째 날 상담을 할 경우 i+t[i]-1은 상담이 종료되는 날
24+
if(i + t[i]-1 <= n) // 상담이 n일 이전에 종료될 경우
25+
d[i + t[i]] = max(d[i + t[i]], d[i] + p[i]); // d[i+t[i]] 갱신
26+
}
27+
cout << max(d[n], d[n+1]);
28+
}
2829

29-
cout << *max_element(d, d + n + 1);
30-
}
30+
/*
31+
테이블을 채워나가는 방식이 조금 낯설 수 있으나,
32+
d[i] = max(d[i], d[i-1]); 를 한 순간 d[i]값이 확정되고 이후
33+
d[i+t[i]]를 갱신한다고 이해할 수 있다.
34+
*/

0 commit comments

Comments
 (0)
0