/**
____ ____ ____ ____ ____ ____
||l |||e |||i |||n |||a |||d ||
||__|||__|||__|||__|||__|||__||
|/__\|/__\|/__\|/__\|/__\|/__\|
**/
#include<bits/stdc++.h>
#define endl '\n'
using namespace std;
typedef long long ll;
void speed()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
}
const int maxn = 5010;
int n, a[maxn], dp[maxn], pos[maxn], cnt[maxn][maxn], par[maxn][maxn];
void solve()
{
cin >> n;
for (int i = 1; i <= n; i ++)
{
cin >> a[i];
pos[a[i]] = i;
}
for (int i = 1; i <= n; i ++)
for (int j = 1; j <= n; j ++)
{
cnt[i][j] = cnt[i - 1][j] + cnt[i][j - 1] - cnt[i - 1][j - 1];
if (pos[i] == j)
cnt[i][j] ++;
}
for (int i = 1; i <= n; i ++)
for (int j = 1; j <= n; j ++)
par[i][j] = par[i][j - 1] + cnt[i][pos[j]];
/**for (int i = 1; i <= n; i ++, cout << endl)
for (int j = 1; j <= n; j ++)
cout << cnt[i][j] << " ";*/
for (int i = 1; i <= n; i ++)
{
dp[i] = n * n;
int all = 0;
for (int k = i; k > 0; k --)
all += (cnt[n][pos[k]] - cnt[i][pos[k]]) + cnt[k - 1][pos[k]];
for (int j = 0; j < i; j ++)
{
if (j > 0)
all -= ((cnt[n][pos[j]] - cnt[i][pos[j]]) + cnt[j - 1][pos[j]]);
int sum = - (par[j][i] - par[j][j]);
/**for (int k = i; k > j; k --)
{
///ll cur = pos[k] + (j + (i - k) - query(pos[k] - 1));
ll cur = - cnt[j][pos[k]];
///cout << i << " : " << j << " : " << k << " " << cnt[n][pos[k]] << " " << cnt[k][pos[k]] << endl;
//cur -= st;
//st ++;
sum = sum + cur;
}*/
///cout << i << " " << j << " " << sum << endl;
dp[i] = min(dp[i], dp[j] + sum + all);
}
///cout << i << " -- " << dp[i] << endl;
}
cout << dp[n] << endl;
}
int main()
{
solve();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 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 |
340 KB |
Output is correct |
5 |
Correct |
1 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 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 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 |
340 KB |
Output is correct |
5 |
Correct |
1 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 |
468 KB |
Output is correct |
12 |
Correct |
0 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
0 ms |
468 KB |
Output is correct |
16 |
Correct |
0 ms |
468 KB |
Output is correct |
17 |
Correct |
0 ms |
468 KB |
Output is correct |
18 |
Correct |
0 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
0 ms |
468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 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 |
340 KB |
Output is correct |
5 |
Correct |
1 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 |
468 KB |
Output is correct |
12 |
Correct |
0 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
0 ms |
468 KB |
Output is correct |
16 |
Correct |
0 ms |
468 KB |
Output is correct |
17 |
Correct |
0 ms |
468 KB |
Output is correct |
18 |
Correct |
0 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
0 ms |
468 KB |
Output is correct |
21 |
Correct |
2 ms |
2132 KB |
Output is correct |
22 |
Correct |
2 ms |
2260 KB |
Output is correct |
23 |
Correct |
2 ms |
2260 KB |
Output is correct |
24 |
Correct |
2 ms |
2260 KB |
Output is correct |
25 |
Correct |
2 ms |
2260 KB |
Output is correct |
26 |
Correct |
2 ms |
2260 KB |
Output is correct |
27 |
Correct |
2 ms |
2260 KB |
Output is correct |
28 |
Correct |
2 ms |
2260 KB |
Output is correct |
29 |
Correct |
2 ms |
2260 KB |
Output is correct |
30 |
Correct |
2 ms |
2260 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 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 |
340 KB |
Output is correct |
5 |
Correct |
1 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 |
468 KB |
Output is correct |
12 |
Correct |
0 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
0 ms |
468 KB |
Output is correct |
16 |
Correct |
0 ms |
468 KB |
Output is correct |
17 |
Correct |
0 ms |
468 KB |
Output is correct |
18 |
Correct |
0 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
0 ms |
468 KB |
Output is correct |
21 |
Correct |
2 ms |
2132 KB |
Output is correct |
22 |
Correct |
2 ms |
2260 KB |
Output is correct |
23 |
Correct |
2 ms |
2260 KB |
Output is correct |
24 |
Correct |
2 ms |
2260 KB |
Output is correct |
25 |
Correct |
2 ms |
2260 KB |
Output is correct |
26 |
Correct |
2 ms |
2260 KB |
Output is correct |
27 |
Correct |
2 ms |
2260 KB |
Output is correct |
28 |
Correct |
2 ms |
2260 KB |
Output is correct |
29 |
Correct |
2 ms |
2260 KB |
Output is correct |
30 |
Correct |
2 ms |
2260 KB |
Output is correct |
31 |
Correct |
14 ms |
11648 KB |
Output is correct |
32 |
Correct |
14 ms |
11740 KB |
Output is correct |
33 |
Correct |
14 ms |
11720 KB |
Output is correct |
34 |
Correct |
14 ms |
11720 KB |
Output is correct |
35 |
Correct |
15 ms |
11732 KB |
Output is correct |
36 |
Correct |
18 ms |
11732 KB |
Output is correct |
37 |
Correct |
15 ms |
11696 KB |
Output is correct |
38 |
Correct |
15 ms |
11740 KB |
Output is correct |
39 |
Correct |
16 ms |
11732 KB |
Output is correct |
40 |
Correct |
14 ms |
11764 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 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 |
340 KB |
Output is correct |
5 |
Correct |
1 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 |
468 KB |
Output is correct |
12 |
Correct |
0 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
0 ms |
468 KB |
Output is correct |
16 |
Correct |
0 ms |
468 KB |
Output is correct |
17 |
Correct |
0 ms |
468 KB |
Output is correct |
18 |
Correct |
0 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
0 ms |
468 KB |
Output is correct |
21 |
Correct |
2 ms |
2132 KB |
Output is correct |
22 |
Correct |
2 ms |
2260 KB |
Output is correct |
23 |
Correct |
2 ms |
2260 KB |
Output is correct |
24 |
Correct |
2 ms |
2260 KB |
Output is correct |
25 |
Correct |
2 ms |
2260 KB |
Output is correct |
26 |
Correct |
2 ms |
2260 KB |
Output is correct |
27 |
Correct |
2 ms |
2260 KB |
Output is correct |
28 |
Correct |
2 ms |
2260 KB |
Output is correct |
29 |
Correct |
2 ms |
2260 KB |
Output is correct |
30 |
Correct |
2 ms |
2260 KB |
Output is correct |
31 |
Correct |
14 ms |
11648 KB |
Output is correct |
32 |
Correct |
14 ms |
11740 KB |
Output is correct |
33 |
Correct |
14 ms |
11720 KB |
Output is correct |
34 |
Correct |
14 ms |
11720 KB |
Output is correct |
35 |
Correct |
15 ms |
11732 KB |
Output is correct |
36 |
Correct |
18 ms |
11732 KB |
Output is correct |
37 |
Correct |
15 ms |
11696 KB |
Output is correct |
38 |
Correct |
15 ms |
11740 KB |
Output is correct |
39 |
Correct |
16 ms |
11732 KB |
Output is correct |
40 |
Correct |
14 ms |
11764 KB |
Output is correct |
41 |
Correct |
578 ms |
196444 KB |
Output is correct |
42 |
Correct |
616 ms |
196504 KB |
Output is correct |
43 |
Correct |
581 ms |
196456 KB |
Output is correct |
44 |
Correct |
595 ms |
196496 KB |
Output is correct |
45 |
Correct |
584 ms |
196452 KB |
Output is correct |
46 |
Correct |
585 ms |
196500 KB |
Output is correct |
47 |
Correct |
639 ms |
196576 KB |
Output is correct |
48 |
Correct |
584 ms |
196556 KB |
Output is correct |
49 |
Correct |
601 ms |
196636 KB |
Output is correct |
50 |
Correct |
595 ms |
196452 KB |
Output is correct |
51 |
Correct |
560 ms |
196560 KB |
Output is correct |
52 |
Correct |
616 ms |
196552 KB |
Output is correct |
53 |
Correct |
597 ms |
196476 KB |
Output is correct |
54 |
Correct |
586 ms |
196476 KB |
Output is correct |
55 |
Correct |
583 ms |
196428 KB |
Output is correct |