/// #if (code == true)
#include <bits/stdc++.h>
using namespace std;
#define pll pair<long long, long long>
#define len(a) ((int)((a).size()))
#define all(a) a.begin(), a.end()
#define add push_back
#define mkp make_pair
#define ll long long
#define fr first
#define sc second
const long long INF = 1000000000ll * 1000000003ll;
const long long MOD = 1000000007ll;
const int N = 5e3 + 5;
ll n;
ll a[N], ind[N], r[N];
ll pq[N][N], sum[N], x[N];
ll dp[N];
void solve(){
cin >> n;
for(int i = 0; i < n; i++){
cin >> a[i];
ind[a[i]] = i;
}
// cout << endl;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= n; j++){
pq[i][j] = pq[i - 1][j] + pq[i][j - 1] - pq[i - 1][j - 1] + (ind[j] < ind[i]);
if(i == j) x[i] = x[i - 1] + pq[i][j] - pq[i - 1][j];
// cout << pq[i][j] << " ";
}
// cout << endl;
}
// cout << endl;
for(int i = 1; i <= n; i++){
r[i] = r[i - 1] + ind[i];
}
// for(int i = 1; i <= n; i++){
// cout << x[i] << " ";
// }
// cout << endl << endl;
fill(dp, dp + n + 2, INF);
dp[1] = 0;
for(int i = 1; i <= n; i++){
for(int j = i; j <= n; j++){
dp[j + 1] = min(dp[j + 1], dp[i] + x[j] - x[i - 1] - (pq[j][i - 1] - pq[i - 1][i - 1]) + pq[j][n] - pq[i - 1][n] - (pq[j][j] - pq[i - 1][j]));
// cout << i << " " << j + 1 << " | " << dp[i] << " + " << x[j] << " - " << x[i - 1] << " - (" << pq[j][i - 1] << " - " << pq[i - 1][i - 1] << ") + " << pq[j][n] << " - " << pq[i - 1][n] << " - (" << pq[j][j - 1] << " - " << pq[i - 1][j - 1] << ") = " << dp[j + 1] << endl;
}
}
// for(int i = 1; i <= n; i++){
// cout << dp[i] << " ";
// }
cout << dp[n + 1] << endl;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
int _ = 1;
// cout << fixed;
// cout.precision(15);
// cin >> _ ;
while(_--) solve();
return 0;
}
/// #else
/// #include <bits/stdc++.h> using namespace std; int main() { cout << "Hello World!"; }
/// #endif
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
368 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
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 |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
368 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 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 |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
0 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 |
1 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 |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
368 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 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 |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
0 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 |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
1364 KB |
Output is correct |
22 |
Correct |
1 ms |
1364 KB |
Output is correct |
23 |
Correct |
1 ms |
1364 KB |
Output is correct |
24 |
Correct |
1 ms |
1364 KB |
Output is correct |
25 |
Correct |
1 ms |
1364 KB |
Output is correct |
26 |
Correct |
1 ms |
1364 KB |
Output is correct |
27 |
Correct |
1 ms |
1364 KB |
Output is correct |
28 |
Correct |
1 ms |
1364 KB |
Output is correct |
29 |
Correct |
1 ms |
1364 KB |
Output is correct |
30 |
Correct |
1 ms |
1364 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 |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
368 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 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 |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
0 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 |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
1364 KB |
Output is correct |
22 |
Correct |
1 ms |
1364 KB |
Output is correct |
23 |
Correct |
1 ms |
1364 KB |
Output is correct |
24 |
Correct |
1 ms |
1364 KB |
Output is correct |
25 |
Correct |
1 ms |
1364 KB |
Output is correct |
26 |
Correct |
1 ms |
1364 KB |
Output is correct |
27 |
Correct |
1 ms |
1364 KB |
Output is correct |
28 |
Correct |
1 ms |
1364 KB |
Output is correct |
29 |
Correct |
1 ms |
1364 KB |
Output is correct |
30 |
Correct |
1 ms |
1364 KB |
Output is correct |
31 |
Correct |
10 ms |
8532 KB |
Output is correct |
32 |
Correct |
12 ms |
8532 KB |
Output is correct |
33 |
Correct |
11 ms |
8612 KB |
Output is correct |
34 |
Correct |
9 ms |
8532 KB |
Output is correct |
35 |
Correct |
11 ms |
8604 KB |
Output is correct |
36 |
Correct |
12 ms |
8532 KB |
Output is correct |
37 |
Correct |
11 ms |
8532 KB |
Output is correct |
38 |
Correct |
9 ms |
8604 KB |
Output is correct |
39 |
Correct |
9 ms |
8532 KB |
Output is correct |
40 |
Correct |
10 ms |
8704 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 |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
368 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 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 |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
0 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 |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
1364 KB |
Output is correct |
22 |
Correct |
1 ms |
1364 KB |
Output is correct |
23 |
Correct |
1 ms |
1364 KB |
Output is correct |
24 |
Correct |
1 ms |
1364 KB |
Output is correct |
25 |
Correct |
1 ms |
1364 KB |
Output is correct |
26 |
Correct |
1 ms |
1364 KB |
Output is correct |
27 |
Correct |
1 ms |
1364 KB |
Output is correct |
28 |
Correct |
1 ms |
1364 KB |
Output is correct |
29 |
Correct |
1 ms |
1364 KB |
Output is correct |
30 |
Correct |
1 ms |
1364 KB |
Output is correct |
31 |
Correct |
10 ms |
8532 KB |
Output is correct |
32 |
Correct |
12 ms |
8532 KB |
Output is correct |
33 |
Correct |
11 ms |
8612 KB |
Output is correct |
34 |
Correct |
9 ms |
8532 KB |
Output is correct |
35 |
Correct |
11 ms |
8604 KB |
Output is correct |
36 |
Correct |
12 ms |
8532 KB |
Output is correct |
37 |
Correct |
11 ms |
8532 KB |
Output is correct |
38 |
Correct |
9 ms |
8604 KB |
Output is correct |
39 |
Correct |
9 ms |
8532 KB |
Output is correct |
40 |
Correct |
10 ms |
8704 KB |
Output is correct |
41 |
Correct |
438 ms |
196280 KB |
Output is correct |
42 |
Correct |
427 ms |
196404 KB |
Output is correct |
43 |
Correct |
435 ms |
196356 KB |
Output is correct |
44 |
Correct |
418 ms |
196356 KB |
Output is correct |
45 |
Correct |
405 ms |
196360 KB |
Output is correct |
46 |
Correct |
415 ms |
196472 KB |
Output is correct |
47 |
Correct |
416 ms |
196484 KB |
Output is correct |
48 |
Correct |
428 ms |
196428 KB |
Output is correct |
49 |
Correct |
424 ms |
196436 KB |
Output is correct |
50 |
Correct |
416 ms |
196328 KB |
Output is correct |
51 |
Correct |
434 ms |
196336 KB |
Output is correct |
52 |
Correct |
419 ms |
196392 KB |
Output is correct |
53 |
Correct |
454 ms |
196396 KB |
Output is correct |
54 |
Correct |
446 ms |
196512 KB |
Output is correct |
55 |
Correct |
425 ms |
196332 KB |
Output is correct |