#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll INF = 1e18+7;
const ll MOD = 998244353;
typedef pair<ll,ll> ii;
#define iii pair<ll,ii>
#define ull unsigned ll
#define f(i,a,b) for(ll i = a;i < b;i++)
#define pb push_back
#define vll vector<ll>
#define F first
#define S second
#define all(x) (x).begin(), (x).end()
///I hope I will get uprating and don't make mistakes
///I will never stop programming
///sqrt(-1) Love C++
///Please don't hack me
///@TheofanisOrfanou Theo830
///Think different approaches (bs,dp,greedy,graphs,shortest paths,mst)
///Stay Calm
///Look for special cases
///Beware of overflow and array bounds
///Think the problem backwards
///Training
int main(void){
ios_base::sync_with_stdio(0);
cin.tie(0);
ll n;
cin>>n;
ll arr[n];
ll pre[n+1][n+1][3];
ll pos[n+1];
f(i,0,n){
cin>>arr[i];
pos[arr[i]] = i;
}
f(i,0,n+1){
f(j,0,n+1){
pre[i][j][0] = pre[i][j][1] = pre[i][j][2] = 0;
}
}
f(i,1,n+1){
f(j,i+1,n+1){
pre[i][j][1] = pre[i][j-1][1] + (pos[i] > pos[j]);
}
}
f(j,1,n+1){
f(i,1,j+1){
pre[j][i][0] = pre[j][i-1][0] + (pos[i] < pos[j]);
pre[j][i][2] = pre[j][i-1][2] + (pos[i] > pos[j]);
}
}
ll dp[n+1] = {0};
for(ll i = n-1;i >= 0;i--){
dp[i] = 1e18;
ll sum = 0,sumi = 0;
f(j,i+1,n+1){
sum += pre[j][j][0] - pre[j][i][0];
sumi += pre[j][n][1] - pre[j][j][1];
sumi -= pre[j][j][2] - pre[j][i][2];
dp[i] = min(dp[i],dp[j] + sum + sumi);
}
}
cout<<dp[0]<<"\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
320 KB |
Output is correct |
16 |
Correct |
1 ms |
320 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
316 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
320 KB |
Output is correct |
16 |
Correct |
1 ms |
320 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
316 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
1236 KB |
Output is correct |
22 |
Correct |
1 ms |
1236 KB |
Output is correct |
23 |
Correct |
1 ms |
1236 KB |
Output is correct |
24 |
Correct |
1 ms |
1236 KB |
Output is correct |
25 |
Correct |
1 ms |
1236 KB |
Output is correct |
26 |
Correct |
1 ms |
1236 KB |
Output is correct |
27 |
Correct |
1 ms |
1236 KB |
Output is correct |
28 |
Correct |
1 ms |
1236 KB |
Output is correct |
29 |
Correct |
1 ms |
1236 KB |
Output is correct |
30 |
Correct |
1 ms |
1216 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
320 KB |
Output is correct |
16 |
Correct |
1 ms |
320 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
316 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
1236 KB |
Output is correct |
22 |
Correct |
1 ms |
1236 KB |
Output is correct |
23 |
Correct |
1 ms |
1236 KB |
Output is correct |
24 |
Correct |
1 ms |
1236 KB |
Output is correct |
25 |
Correct |
1 ms |
1236 KB |
Output is correct |
26 |
Correct |
1 ms |
1236 KB |
Output is correct |
27 |
Correct |
1 ms |
1236 KB |
Output is correct |
28 |
Correct |
1 ms |
1236 KB |
Output is correct |
29 |
Correct |
1 ms |
1236 KB |
Output is correct |
30 |
Correct |
1 ms |
1216 KB |
Output is correct |
31 |
Correct |
21 ms |
15316 KB |
Output is correct |
32 |
Correct |
15 ms |
15316 KB |
Output is correct |
33 |
Correct |
15 ms |
15368 KB |
Output is correct |
34 |
Correct |
17 ms |
15316 KB |
Output is correct |
35 |
Correct |
15 ms |
15316 KB |
Output is correct |
36 |
Correct |
14 ms |
15292 KB |
Output is correct |
37 |
Correct |
16 ms |
15444 KB |
Output is correct |
38 |
Correct |
15 ms |
15404 KB |
Output is correct |
39 |
Correct |
16 ms |
15316 KB |
Output is correct |
40 |
Correct |
15 ms |
15316 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
320 KB |
Output is correct |
16 |
Correct |
1 ms |
320 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
316 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
1236 KB |
Output is correct |
22 |
Correct |
1 ms |
1236 KB |
Output is correct |
23 |
Correct |
1 ms |
1236 KB |
Output is correct |
24 |
Correct |
1 ms |
1236 KB |
Output is correct |
25 |
Correct |
1 ms |
1236 KB |
Output is correct |
26 |
Correct |
1 ms |
1236 KB |
Output is correct |
27 |
Correct |
1 ms |
1236 KB |
Output is correct |
28 |
Correct |
1 ms |
1236 KB |
Output is correct |
29 |
Correct |
1 ms |
1236 KB |
Output is correct |
30 |
Correct |
1 ms |
1216 KB |
Output is correct |
31 |
Correct |
21 ms |
15316 KB |
Output is correct |
32 |
Correct |
15 ms |
15316 KB |
Output is correct |
33 |
Correct |
15 ms |
15368 KB |
Output is correct |
34 |
Correct |
17 ms |
15316 KB |
Output is correct |
35 |
Correct |
15 ms |
15316 KB |
Output is correct |
36 |
Correct |
14 ms |
15292 KB |
Output is correct |
37 |
Correct |
16 ms |
15444 KB |
Output is correct |
38 |
Correct |
15 ms |
15404 KB |
Output is correct |
39 |
Correct |
16 ms |
15316 KB |
Output is correct |
40 |
Correct |
15 ms |
15316 KB |
Output is correct |
41 |
Correct |
887 ms |
587140 KB |
Output is correct |
42 |
Correct |
905 ms |
587292 KB |
Output is correct |
43 |
Correct |
839 ms |
587540 KB |
Output is correct |
44 |
Correct |
870 ms |
587596 KB |
Output is correct |
45 |
Correct |
888 ms |
587516 KB |
Output is correct |
46 |
Correct |
912 ms |
587760 KB |
Output is correct |
47 |
Correct |
917 ms |
587724 KB |
Output is correct |
48 |
Correct |
836 ms |
587708 KB |
Output is correct |
49 |
Correct |
825 ms |
587712 KB |
Output is correct |
50 |
Correct |
812 ms |
587756 KB |
Output is correct |
51 |
Correct |
801 ms |
587756 KB |
Output is correct |
52 |
Correct |
804 ms |
587760 KB |
Output is correct |
53 |
Correct |
839 ms |
587808 KB |
Output is correct |
54 |
Correct |
816 ms |
587880 KB |
Output is correct |
55 |
Correct |
797 ms |
587724 KB |
Output is correct |