-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy path1079C. Playing Piano.cpp
74 lines (62 loc) · 1.27 KB
/
1079C. Playing Piano.cpp
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/*
Idea:
- Dynamic Programming
- If you reach the base case in the recursion, then
you have an answer, so print it.
- This method work if the dynamic programming answer
is true or false.
*/
#include <bits/stdc++.h>
using namespace std;
int const N = 1e5 + 1;
int n, a[N], dp[N][6];
vector<int> sol;
int rec(int idx, int prv) {
if(idx == n) {
for(int i = 0; i < sol.size(); ++i)
printf("%s%d", i == 0 ? "" : " ", sol[i]);
puts("");
exit(0);
}
int &ret = dp[idx][prv];
if(ret != -1)
return ret;
ret = 0;
if(a[idx] == a[idx - 1]) {
for(int i = 1; i <= 5; ++i)
if(i != prv) {
sol.push_back(i);
rec(idx + 1, i);
sol.pop_back();
}
}
if(a[idx] > a[idx - 1]) {
for(int i = prv + 1; i <= 5; ++i) {
sol.push_back(i);
rec(idx + 1, i);
sol.pop_back();
}
}
if(a[idx] < a[idx - 1]) {
for(int i = prv - 1; i >= 1; --i) {
sol.push_back(i);
rec(idx + 1, i);
sol.pop_back();
}
}
return ret;
}
int main() {
scanf("%d", &n);
for(int i = 0; i < n; ++i)
scanf("%d", a + i);
memset(dp, -1, sizeof dp);
for(int i = 1; i <= 5; ++i) {
sol.push_back(i);
if(rec(1, i))
return 0;
sol.pop_back();
}
puts("-1");
return 0;
}