#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 5007;
const ll INF = 1e18 + 7;
const int logo = 20;
const int off = 1 << logo;
ll inv[MAXN][MAXN], dp[MAXN], c[MAXN];
//lesrig[y][x] - koliko brojeva je manje od y nadesno a oni su >= x
int lesrig[MAXN][MAXN], p[MAXN], arr[MAXN];
ll vl(ll x){
return (ll)x * (x - 1) / (ll)2;
}
void solve(){
int n;
cin >> n;
for(int i=1; i<=n; i++) cin >> arr[i];
for(int pf=1; pf<=n; pf++){
int uz = pf;
for(int i=pf; i; i--){
if(arr[i] > pf) c[pf] += (uz - i), uz--;
}
uz++;
for(int i=pf+1; i<=n; i++){
if(arr[i] <= pf){
c[pf] += (i - uz);
uz++;
}
}
}
for(int i=0; i<=n; i++) p[i] = 0;
for(int pos=n; pos; pos--){
int cur = arr[pos];
for(int x=1; x<=n; x++) lesrig[cur][x] = p[cur] - p[x - 1];
for(int i=cur; i<=n; i++) p[i]++;
}
for(int R=1; R<=n; R++){
for(int L=1; L<R; L++){
inv[L][R] = inv[L][R - 1] + lesrig[R][L];
}
}
dp[0] = 0;
for(int i=1; i<=n; i++) dp[i] = INF;
for(int R=1; R<=n; R++){
for(int L=0; L<R; L++){
int len = R - L;
dp[R] = min(dp[R], dp[L] + vl(len) - inv[L + 1][R] + c[R]);
}
}
cout << dp[n] << "\n";
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int tt = 1;
//cin >> tt;
while(tt--) solve();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2484 KB |
Output is correct |
5 |
Correct |
0 ms |
2552 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Incorrect |
1 ms |
4444 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2484 KB |
Output is correct |
5 |
Correct |
0 ms |
2552 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Incorrect |
1 ms |
4444 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2484 KB |
Output is correct |
5 |
Correct |
0 ms |
2552 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Incorrect |
1 ms |
4444 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2484 KB |
Output is correct |
5 |
Correct |
0 ms |
2552 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Incorrect |
1 ms |
4444 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2484 KB |
Output is correct |
5 |
Correct |
0 ms |
2552 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Incorrect |
1 ms |
4444 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |