File tree Expand file tree Collapse file tree 2 files changed +81
-0
lines changed
Expand file tree Collapse file tree 2 files changed +81
-0
lines changed Original file line number Diff line number Diff line change 1+ #include < iostream>
2+ #include < algorithm>
3+
4+ using namespace std ;
5+
6+ int n;
7+ int stairs[301 ]; // 각 계단의 점수
8+
9+ // idx: 현재 계단의 인덱스, count: 연속으로 밟은 계단 수 (최대 2)
10+ int step (int idx, int count)
11+ {
12+ // 기저 사례: 계단을 초과한 경우
13+ if (idx > n)
14+ {
15+ return 0 ;
16+ }
17+
18+ // 마지막 계단에 도달한 경우
19+ if (idx == n)
20+ {
21+ return stairs[idx];
22+ }
23+
24+ int result = 0 ;
25+
26+ // 한 계단을 밟는 경우, 연속 2번을 넘지 않도록 제한
27+ if (count < 2 )
28+ {
29+ result = max (result, stairs[idx] + step (idx + 1 , count + 1 ));
30+ }
31+
32+ // 두 계단을 밟는 경우
33+ result = max (result, stairs[idx] + step (idx + 2 , 1 ));
34+
35+ return result;
36+ }
37+
38+ int main ()
39+ {
40+ ios::sync_with_stdio (0 );
41+ cin.tie (0 );
42+ freopen (" input.txt" , " r" , stdin);
43+ // 계단 개수 입력
44+ cin >> n;
45+
46+ // 계단 점수 입력
47+ for (int i = 1 ; i <= n; i++)
48+ {
49+ cin >> stairs[i];
50+ }
51+
52+ // 1번째 계단부터 시작
53+ cout << step (1 , 1 ) << ' \n ' ;
54+
55+ return 0 ;
56+ }
Original file line number Diff line number Diff line change 1+ #include < iostream>
2+
3+ using namespace std ;
4+
5+ int N, M;
6+ vector<double > locations;
7+
8+ // 이번엔 DP로 풀어보기
9+ // 그리디로 풀린다는 것은 거의 대부분 DP로도 풀린다.
10+
11+ int main (void ) {
12+ int cc;
13+ cin >> cc;
14+
15+ for (int c = 0 ; c < cc; c++) {
16+ cin >> N >> M;
17+
18+ locations.clear ();
19+ locations.assign (M, -1 );
20+ for (int i = 0 ; i < M; i++) {
21+ cin >> locations[i];
22+ }
23+ }
24+ return 0 ;
25+ }
You can’t perform that action at this time.
0 commit comments