#include<iostream>
#include<vector>
#include<algorithm>
#define INF 1000000000
using namespace std;
int main() {
int n;
cin >> n;
vector<int>v(n);
for (auto &i : v) cin >> i;
vector<vector<int> >dp((1 << n), vector<int>(n, INF));
vector<int>back(n, 0);
for (int i = n-2 ; i >= 0 ; i--) {
back[i] = back[i+1] | (1 << (i+1));
//cout << back[i] << " ";
}
//cout << "\n";
int cost = 1;
for (int i = 0 ; i < n ; i++) dp[1 << i][i] = i;
for (int i = 1 ; i < (1 << n) ; i++) {
int t = __builtin_popcount(i);
for (int j = 0 ; j < n ; j++) {
for (int k = 0 ; k < n ; k++) {
if ( ((i >> k) & 1)==0 && (v[j] < v[k] + 2) ) {
int cost = k - t + __builtin_popcount(i & back[k]);
//cout << k << " " << t << " " << __builtin_popcount(i & back[k]) << "\n";
dp[i | (1 << k) ][k] = min(dp[i | (1 << k)][k], dp[i][j] + cost);
}
}
}
}
/*for (int j = 0 ; j < n ; j++) {
for (int i = 0 ; i < n ; i++) {
cout << dp[(1 << n) - 1][i] << endl;
}
}*/
cout << *min_element(dp[(1 << n) - 1].begin(), dp[(1 << n) - 1].end()) << "\n";
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:19:6: warning: unused variable 'cost' [-Wunused-variable]
19 | int cost = 1;
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
428 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
436 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
428 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
436 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
371 ms |
27104 KB |
Output is correct |
12 |
Correct |
788 ms |
61988 KB |
Output is correct |
13 |
Correct |
820 ms |
61992 KB |
Output is correct |
14 |
Correct |
1727 ms |
123548 KB |
Output is correct |
15 |
Correct |
1820 ms |
123548 KB |
Output is correct |
16 |
Correct |
1787 ms |
123548 KB |
Output is correct |
17 |
Correct |
1772 ms |
123548 KB |
Output is correct |
18 |
Correct |
1866 ms |
123556 KB |
Output is correct |
19 |
Correct |
1832 ms |
123544 KB |
Output is correct |
20 |
Correct |
1767 ms |
123544 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
428 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
436 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
371 ms |
27104 KB |
Output is correct |
12 |
Correct |
788 ms |
61988 KB |
Output is correct |
13 |
Correct |
820 ms |
61992 KB |
Output is correct |
14 |
Correct |
1727 ms |
123548 KB |
Output is correct |
15 |
Correct |
1820 ms |
123548 KB |
Output is correct |
16 |
Correct |
1787 ms |
123548 KB |
Output is correct |
17 |
Correct |
1772 ms |
123548 KB |
Output is correct |
18 |
Correct |
1866 ms |
123556 KB |
Output is correct |
19 |
Correct |
1832 ms |
123544 KB |
Output is correct |
20 |
Correct |
1767 ms |
123544 KB |
Output is correct |
21 |
Runtime error |
1 ms |
604 KB |
Execution killed with signal 11 |
22 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
428 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
436 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
371 ms |
27104 KB |
Output is correct |
12 |
Correct |
788 ms |
61988 KB |
Output is correct |
13 |
Correct |
820 ms |
61992 KB |
Output is correct |
14 |
Correct |
1727 ms |
123548 KB |
Output is correct |
15 |
Correct |
1820 ms |
123548 KB |
Output is correct |
16 |
Correct |
1787 ms |
123548 KB |
Output is correct |
17 |
Correct |
1772 ms |
123548 KB |
Output is correct |
18 |
Correct |
1866 ms |
123556 KB |
Output is correct |
19 |
Correct |
1832 ms |
123544 KB |
Output is correct |
20 |
Correct |
1767 ms |
123544 KB |
Output is correct |
21 |
Runtime error |
1 ms |
604 KB |
Execution killed with signal 11 |
22 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
428 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
436 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
371 ms |
27104 KB |
Output is correct |
12 |
Correct |
788 ms |
61988 KB |
Output is correct |
13 |
Correct |
820 ms |
61992 KB |
Output is correct |
14 |
Correct |
1727 ms |
123548 KB |
Output is correct |
15 |
Correct |
1820 ms |
123548 KB |
Output is correct |
16 |
Correct |
1787 ms |
123548 KB |
Output is correct |
17 |
Correct |
1772 ms |
123548 KB |
Output is correct |
18 |
Correct |
1866 ms |
123556 KB |
Output is correct |
19 |
Correct |
1832 ms |
123544 KB |
Output is correct |
20 |
Correct |
1767 ms |
123544 KB |
Output is correct |
21 |
Runtime error |
1 ms |
604 KB |
Execution killed with signal 11 |
22 |
Halted |
0 ms |
0 KB |
- |