// vaziat meshki-ghermeze !
#include <bits/stdc++.h>
#define pb push_back
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define debug(x) cerr << #x << " : " << x << '\n'
using namespace std;
typedef long long ll;
typedef long double ld;
typedef string str;
typedef pair<ll, ll> pll;
const ll Mod = 1000000007LL;
const int N = 5e3 + 10;
const ll Inf = 2242545357980376863LL;
const ll Log = 30;
int p[N], rv[N];
int sm[N][N];
int dp[N];
int C2(int x){ return x * (x - 1) / 2; }
int main(){
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int n;
cin >> n;
for(int i = 1; i <= n; i++){
cin >> p[i];
rv[p[i]] = i;
}
for(int i = 1; i <= n; i++){
for(int j = i + 1; j <= n; j++){
if(rv[i] > rv[j])
sm[i][j] ++;
}
}
for(int i = 1; i <= n; i++)
for(int j = i - 1; j >= 1; j--)
sm[j][i] += sm[j + 1][i] + sm[j][i - 1] - sm[j + 1][i - 1];
memset(dp, 31, sizeof dp);
dp[0] = 0;
for(int i = 0; i < n; i++){
for(int j = i + 1; j <= n; j++)
dp[j] = min(dp[j], dp[i] - sm[i + 1][j] + C2(j - i) - sm[i + 1][j]);
}
cout << dp[n] + sm[1][n] << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
324 KB |
Output is correct |
8 |
Correct |
2 ms |
320 KB |
Output is correct |
9 |
Correct |
1 ms |
320 KB |
Output is correct |
10 |
Correct |
2 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
324 KB |
Output is correct |
8 |
Correct |
2 ms |
320 KB |
Output is correct |
9 |
Correct |
1 ms |
320 KB |
Output is correct |
10 |
Correct |
2 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
2 ms |
332 KB |
Output is correct |
17 |
Correct |
2 ms |
328 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
324 KB |
Output is correct |
8 |
Correct |
2 ms |
320 KB |
Output is correct |
9 |
Correct |
1 ms |
320 KB |
Output is correct |
10 |
Correct |
2 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
2 ms |
332 KB |
Output is correct |
17 |
Correct |
2 ms |
328 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
3 ms |
1228 KB |
Output is correct |
22 |
Correct |
3 ms |
1228 KB |
Output is correct |
23 |
Correct |
2 ms |
1228 KB |
Output is correct |
24 |
Correct |
2 ms |
1228 KB |
Output is correct |
25 |
Correct |
2 ms |
1228 KB |
Output is correct |
26 |
Correct |
2 ms |
1228 KB |
Output is correct |
27 |
Correct |
1 ms |
1228 KB |
Output is correct |
28 |
Correct |
2 ms |
1228 KB |
Output is correct |
29 |
Correct |
2 ms |
1228 KB |
Output is correct |
30 |
Correct |
2 ms |
1216 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
324 KB |
Output is correct |
8 |
Correct |
2 ms |
320 KB |
Output is correct |
9 |
Correct |
1 ms |
320 KB |
Output is correct |
10 |
Correct |
2 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
2 ms |
332 KB |
Output is correct |
17 |
Correct |
2 ms |
328 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
3 ms |
1228 KB |
Output is correct |
22 |
Correct |
3 ms |
1228 KB |
Output is correct |
23 |
Correct |
2 ms |
1228 KB |
Output is correct |
24 |
Correct |
2 ms |
1228 KB |
Output is correct |
25 |
Correct |
2 ms |
1228 KB |
Output is correct |
26 |
Correct |
2 ms |
1228 KB |
Output is correct |
27 |
Correct |
1 ms |
1228 KB |
Output is correct |
28 |
Correct |
2 ms |
1228 KB |
Output is correct |
29 |
Correct |
2 ms |
1228 KB |
Output is correct |
30 |
Correct |
2 ms |
1216 KB |
Output is correct |
31 |
Correct |
8 ms |
4812 KB |
Output is correct |
32 |
Correct |
7 ms |
4812 KB |
Output is correct |
33 |
Correct |
9 ms |
4936 KB |
Output is correct |
34 |
Correct |
6 ms |
4812 KB |
Output is correct |
35 |
Correct |
6 ms |
4804 KB |
Output is correct |
36 |
Correct |
6 ms |
4812 KB |
Output is correct |
37 |
Correct |
7 ms |
4812 KB |
Output is correct |
38 |
Correct |
6 ms |
4812 KB |
Output is correct |
39 |
Correct |
6 ms |
4812 KB |
Output is correct |
40 |
Correct |
7 ms |
4812 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
324 KB |
Output is correct |
8 |
Correct |
2 ms |
320 KB |
Output is correct |
9 |
Correct |
1 ms |
320 KB |
Output is correct |
10 |
Correct |
2 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
2 ms |
332 KB |
Output is correct |
17 |
Correct |
2 ms |
328 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
3 ms |
1228 KB |
Output is correct |
22 |
Correct |
3 ms |
1228 KB |
Output is correct |
23 |
Correct |
2 ms |
1228 KB |
Output is correct |
24 |
Correct |
2 ms |
1228 KB |
Output is correct |
25 |
Correct |
2 ms |
1228 KB |
Output is correct |
26 |
Correct |
2 ms |
1228 KB |
Output is correct |
27 |
Correct |
1 ms |
1228 KB |
Output is correct |
28 |
Correct |
2 ms |
1228 KB |
Output is correct |
29 |
Correct |
2 ms |
1228 KB |
Output is correct |
30 |
Correct |
2 ms |
1216 KB |
Output is correct |
31 |
Correct |
8 ms |
4812 KB |
Output is correct |
32 |
Correct |
7 ms |
4812 KB |
Output is correct |
33 |
Correct |
9 ms |
4936 KB |
Output is correct |
34 |
Correct |
6 ms |
4812 KB |
Output is correct |
35 |
Correct |
6 ms |
4804 KB |
Output is correct |
36 |
Correct |
6 ms |
4812 KB |
Output is correct |
37 |
Correct |
7 ms |
4812 KB |
Output is correct |
38 |
Correct |
6 ms |
4812 KB |
Output is correct |
39 |
Correct |
6 ms |
4812 KB |
Output is correct |
40 |
Correct |
7 ms |
4812 KB |
Output is correct |
41 |
Correct |
261 ms |
67308 KB |
Output is correct |
42 |
Correct |
237 ms |
67332 KB |
Output is correct |
43 |
Correct |
256 ms |
67292 KB |
Output is correct |
44 |
Correct |
262 ms |
67344 KB |
Output is correct |
45 |
Correct |
245 ms |
67264 KB |
Output is correct |
46 |
Correct |
258 ms |
67352 KB |
Output is correct |
47 |
Correct |
236 ms |
67352 KB |
Output is correct |
48 |
Correct |
257 ms |
67400 KB |
Output is correct |
49 |
Correct |
238 ms |
67284 KB |
Output is correct |
50 |
Correct |
246 ms |
67460 KB |
Output is correct |
51 |
Correct |
261 ms |
67512 KB |
Output is correct |
52 |
Correct |
233 ms |
67260 KB |
Output is correct |
53 |
Correct |
250 ms |
67292 KB |
Output is correct |
54 |
Correct |
268 ms |
67304 KB |
Output is correct |
55 |
Correct |
251 ms |
67288 KB |
Output is correct |