/**
____ ____ ____ ____ ____ ____
||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], fen[maxn];
void add(int pos, int val)
{
for (int i = pos; i <= n; i += (i & -i))
fen[i] += val;
}
int query(int pos)
{
int s = 0;
for (int i = pos; i > 0; i -= (i & -i))
s += fen[i];
return s;
}
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 ++)
{
dp[i] = n * n;
for (int j = 0; j < i; j ++)
{
if (j > 0)
add(pos[j], 1);
int st = j + 1, sum = 0;
for (int k = i; k > j; k --)
{
ll cur = pos[k] + (j + (i - k) - query(pos[k] - 1));
///cout << i << " " << j << " " << k << " " << cur << endl;
add(pos[k], 1);
cur -= st;
st ++;
sum = sum + max((ll)0, cur);
}
for (int k = i; k > j; k --)
add(pos[k], - 1);
///cout << i << " " << j << " " << sum << endl;
dp[i] = min(dp[i], dp[j] + sum);
}
for (int j = 0; j < i; j ++)
if (j > 0)
add(pos[j], - 1);
///cout << i << " -- " << dp[i] << endl;
}
cout << dp[n] << endl;
}
int main()
{
solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
26 ms |
212 KB |
Output is correct |
22 |
Correct |
24 ms |
212 KB |
Output is correct |
23 |
Correct |
25 ms |
324 KB |
Output is correct |
24 |
Correct |
28 ms |
300 KB |
Output is correct |
25 |
Correct |
24 ms |
212 KB |
Output is correct |
26 |
Correct |
25 ms |
212 KB |
Output is correct |
27 |
Correct |
27 ms |
212 KB |
Output is correct |
28 |
Correct |
24 ms |
316 KB |
Output is correct |
29 |
Correct |
24 ms |
312 KB |
Output is correct |
30 |
Correct |
27 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
26 ms |
212 KB |
Output is correct |
22 |
Correct |
24 ms |
212 KB |
Output is correct |
23 |
Correct |
25 ms |
324 KB |
Output is correct |
24 |
Correct |
28 ms |
300 KB |
Output is correct |
25 |
Correct |
24 ms |
212 KB |
Output is correct |
26 |
Correct |
25 ms |
212 KB |
Output is correct |
27 |
Correct |
27 ms |
212 KB |
Output is correct |
28 |
Correct |
24 ms |
316 KB |
Output is correct |
29 |
Correct |
24 ms |
312 KB |
Output is correct |
30 |
Correct |
27 ms |
212 KB |
Output is correct |
31 |
Correct |
2393 ms |
308 KB |
Output is correct |
32 |
Correct |
2228 ms |
308 KB |
Output is correct |
33 |
Correct |
2200 ms |
312 KB |
Output is correct |
34 |
Correct |
2165 ms |
308 KB |
Output is correct |
35 |
Correct |
2154 ms |
308 KB |
Output is correct |
36 |
Correct |
2142 ms |
308 KB |
Output is correct |
37 |
Correct |
2158 ms |
312 KB |
Output is correct |
38 |
Correct |
2136 ms |
308 KB |
Output is correct |
39 |
Correct |
2124 ms |
304 KB |
Output is correct |
40 |
Correct |
2126 ms |
304 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
26 ms |
212 KB |
Output is correct |
22 |
Correct |
24 ms |
212 KB |
Output is correct |
23 |
Correct |
25 ms |
324 KB |
Output is correct |
24 |
Correct |
28 ms |
300 KB |
Output is correct |
25 |
Correct |
24 ms |
212 KB |
Output is correct |
26 |
Correct |
25 ms |
212 KB |
Output is correct |
27 |
Correct |
27 ms |
212 KB |
Output is correct |
28 |
Correct |
24 ms |
316 KB |
Output is correct |
29 |
Correct |
24 ms |
312 KB |
Output is correct |
30 |
Correct |
27 ms |
212 KB |
Output is correct |
31 |
Correct |
2393 ms |
308 KB |
Output is correct |
32 |
Correct |
2228 ms |
308 KB |
Output is correct |
33 |
Correct |
2200 ms |
312 KB |
Output is correct |
34 |
Correct |
2165 ms |
308 KB |
Output is correct |
35 |
Correct |
2154 ms |
308 KB |
Output is correct |
36 |
Correct |
2142 ms |
308 KB |
Output is correct |
37 |
Correct |
2158 ms |
312 KB |
Output is correct |
38 |
Correct |
2136 ms |
308 KB |
Output is correct |
39 |
Correct |
2124 ms |
304 KB |
Output is correct |
40 |
Correct |
2126 ms |
304 KB |
Output is correct |
41 |
Execution timed out |
5037 ms |
360 KB |
Time limit exceeded |
42 |
Halted |
0 ms |
0 KB |
- |