#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pll pair<ll,ll>
#define pii pair<int,int>
#define fs first
#define sc second
#define tlll tuple<ll,ll,ll>
const int mxn = 5050;
int N;
int arr[mxn],pos[mxn];
int aux[mxn][mxn];
ll bit1[mxn],bit2[mxn];
int dp[mxn];
void modify(ll bit[],int p,int v){
for(;p<mxn;p+=p&-p)bit[p] += v;
return;
}
int getval(ll bit[],int s,int e){
int re = 0;
for(;e>0;e-= e&-e)re += bit[e];
s--;
for(;s>0;s-= s&-s)re -= bit[s];
return re;
}
void calc(){
fill(dp,dp+mxn,mxn*mxn);
dp[0] = 0;
for(int i = 0;i<=N;i++){
memset(bit1,0,sizeof(bit1));
memset(bit2,0,sizeof(bit2));
int sh = 0;
for(int j = 1;j<=N;j++){
if(j>i)modify(bit1,pos[j],1);
else modify(bit2,pos[j],1);
}
for(int j = 1;j<=i;j++){
sh += getval(bit1,0,pos[j]);
}
for(int j = i+1;j<=N;j++){
int tdp = dp[i]+aux[i+1][j];
sh = sh+getval(bit1,0,pos[j]-1)-getval(bit2,pos[j]+1,N);
modify(bit1,pos[j],-1);
modify(bit2,pos[j],1);
dp[j] = min(dp[j],tdp+sh);
}
}
return;
}
int main(){
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin>>N;
for(int i = 1;i<=N;i++)cin>>arr[i],pos[arr[i]] = i;
for(int s = 1;s<=N;s++){
memset(bit1,0,sizeof(bit1));
for(int e = s;e<=N;e++){
aux[s][e] = aux[s][e-1];
aux[s][e] += getval(bit1,0,pos[e]);
modify(bit1,pos[e],1);
}
}
calc();
cout<<dp[N];
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
600 KB |
Output is correct |
6 |
Correct |
0 ms |
604 KB |
Output is correct |
7 |
Incorrect |
1 ms |
600 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
600 KB |
Output is correct |
6 |
Correct |
0 ms |
604 KB |
Output is correct |
7 |
Incorrect |
1 ms |
600 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
600 KB |
Output is correct |
6 |
Correct |
0 ms |
604 KB |
Output is correct |
7 |
Incorrect |
1 ms |
600 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
600 KB |
Output is correct |
6 |
Correct |
0 ms |
604 KB |
Output is correct |
7 |
Incorrect |
1 ms |
600 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
600 KB |
Output is correct |
6 |
Correct |
0 ms |
604 KB |
Output is correct |
7 |
Incorrect |
1 ms |
600 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |