//Autor: Bartłomiej Czarkowski
#include <bits/stdc++.h>
using namespace std;
#ifdef DEBUG
template<class A,class B>auto&operator<<(ostream&o,pair<A,B>p){return o<<'('<<p.first<<", "<<p.second<<')';}
template<class T>auto operator<<(ostream&o,T x)->decltype(x.end(),o){o<<'{';int i=0;for(auto e:x)o<<(", ")+2*!i++<<e;return o<<'}';}
#define debug(x...) cerr<<"["#x"]: ",[](auto...$){((cerr<<$<<"; "),...);}(x),cerr<<'\n'
#else
#define debug(...) {}
#endif
const int N = 5010;
int n;
int t[N];
int p[N];
int dp[N];
int nas[N];
int sum[N][N];
int wylicz(int l, int r) {
int ret = 0;
for (int i = l; i <= r; ++i) {
// ile na lewo od wartości i jest w przedziale wartości <r + 1, n>
ret += sum[i][n] - sum[i][r];
// ile na lewo od wartości i jest w przedziale wartości <l, i - 1>
ret += nas[i] - sum[i][l - 1];
}
debug(l, r, ret);
return ret;
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i) {
scanf("%d", &t[i]);
p[t[i]] = i;
}
for (int i = 1; i <= n; ++i) { // pozycja
for (int j = 1; j < i; ++j) {
if (t[j] < t[i]) {
++nas[t[i]];
}
}
for (int j = 1; j < i; ++j) {
++sum[t[i]][t[j]];
}
for (int j = 1; j <= n; ++j) {
sum[t[i]][j] += sum[t[i]][j - 1];
}
}
for (int r = 1; r <= n; ++r) {
dp[r] = 1e9;
for (int l = r; l; --l) {
dp[r] = min(dp[r], dp[l - 1] + wylicz(l, r));
}
}
printf("%d\n", dp[n]);
return 0;
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:33:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
33 | scanf("%d", &n);
| ~~~~~^~~~~~~~~~
Main.cpp:35:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
35 | scanf("%d", &t[i]);
| ~~~~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
3 ms |
1288 KB |
Output is correct |
22 |
Correct |
5 ms |
1236 KB |
Output is correct |
23 |
Correct |
3 ms |
1236 KB |
Output is correct |
24 |
Correct |
3 ms |
1236 KB |
Output is correct |
25 |
Correct |
3 ms |
1236 KB |
Output is correct |
26 |
Correct |
4 ms |
1236 KB |
Output is correct |
27 |
Correct |
3 ms |
1308 KB |
Output is correct |
28 |
Correct |
3 ms |
1236 KB |
Output is correct |
29 |
Correct |
3 ms |
1236 KB |
Output is correct |
30 |
Correct |
3 ms |
1236 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
3 ms |
1288 KB |
Output is correct |
22 |
Correct |
5 ms |
1236 KB |
Output is correct |
23 |
Correct |
3 ms |
1236 KB |
Output is correct |
24 |
Correct |
3 ms |
1236 KB |
Output is correct |
25 |
Correct |
3 ms |
1236 KB |
Output is correct |
26 |
Correct |
4 ms |
1236 KB |
Output is correct |
27 |
Correct |
3 ms |
1308 KB |
Output is correct |
28 |
Correct |
3 ms |
1236 KB |
Output is correct |
29 |
Correct |
3 ms |
1236 KB |
Output is correct |
30 |
Correct |
3 ms |
1236 KB |
Output is correct |
31 |
Correct |
199 ms |
5972 KB |
Output is correct |
32 |
Correct |
198 ms |
6032 KB |
Output is correct |
33 |
Correct |
190 ms |
6036 KB |
Output is correct |
34 |
Correct |
188 ms |
6048 KB |
Output is correct |
35 |
Correct |
233 ms |
6092 KB |
Output is correct |
36 |
Correct |
189 ms |
6052 KB |
Output is correct |
37 |
Correct |
183 ms |
6056 KB |
Output is correct |
38 |
Correct |
186 ms |
6052 KB |
Output is correct |
39 |
Correct |
230 ms |
6048 KB |
Output is correct |
40 |
Correct |
198 ms |
6048 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
3 ms |
1288 KB |
Output is correct |
22 |
Correct |
5 ms |
1236 KB |
Output is correct |
23 |
Correct |
3 ms |
1236 KB |
Output is correct |
24 |
Correct |
3 ms |
1236 KB |
Output is correct |
25 |
Correct |
3 ms |
1236 KB |
Output is correct |
26 |
Correct |
4 ms |
1236 KB |
Output is correct |
27 |
Correct |
3 ms |
1308 KB |
Output is correct |
28 |
Correct |
3 ms |
1236 KB |
Output is correct |
29 |
Correct |
3 ms |
1236 KB |
Output is correct |
30 |
Correct |
3 ms |
1236 KB |
Output is correct |
31 |
Correct |
199 ms |
5972 KB |
Output is correct |
32 |
Correct |
198 ms |
6032 KB |
Output is correct |
33 |
Correct |
190 ms |
6036 KB |
Output is correct |
34 |
Correct |
188 ms |
6048 KB |
Output is correct |
35 |
Correct |
233 ms |
6092 KB |
Output is correct |
36 |
Correct |
189 ms |
6052 KB |
Output is correct |
37 |
Correct |
183 ms |
6056 KB |
Output is correct |
38 |
Correct |
186 ms |
6052 KB |
Output is correct |
39 |
Correct |
230 ms |
6048 KB |
Output is correct |
40 |
Correct |
198 ms |
6048 KB |
Output is correct |
41 |
Execution timed out |
5087 ms |
98548 KB |
Time limit exceeded |
42 |
Halted |
0 ms |
0 KB |
- |