#include <bits/stdc++.h>
//#pragma GCC optimize("fast-math")
using namespace std;
typedef long long ll;
int v[5005],n,dp[5005],nrinv[5005][5005],poz[5005],aib[5005],curpoz[5005];
int lsb(int x)
{
return x&(-x);
}
void update(int poz,int val)
{
for(int i=poz;i<=n;i+=lsb(i))
aib[i]+=val;
}
int suma(int poz)
{
int rez=0;
for(int i=poz;i>=1;i-=lsb(i))
rez+=aib[i];
return rez;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>v[i];
poz[v[i]]=i;
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
aib[j]=0;
int inv=0;
for(int j=i;j<=n;j++)
{
int p=poz[j];
inv+=suma(p);
update(p,+1);
nrinv[i][j]=inv;
}
}
for(int k=n;k>=1;k--)
{
if(k==n)
{
dp[k]=0;
continue;
}
/*vector<int> p;
for(int i=1;i<=n;i++)
if(v[i]<=k)
p.push_back(v[i]);*/
for(int i=1;i<=n;i++)
aib[i]=0;
int lg=0;
for(int i=1;i<=n;i++)
if(v[i]>=k)
{
lg++;
curpoz[v[i]]=lg;
update(lg,+1);
}
dp[k]=2e9;
int need=0;
for(int i=n;i>=k;i--)
{
int val=need+nrinv[k][i]+dp[i+1];
dp[k]=min(dp[k],val);
int st=suma(curpoz[i]-1);
st=curpoz[i]-1-st;
need-=st;
int dr=suma(lg)-suma(curpoz[i]);
need+=dr;
update(curpoz[i],-1);
}
}
cout<<dp[1];
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
352 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
352 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
352 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
3 ms |
1108 KB |
Output is correct |
22 |
Correct |
2 ms |
1108 KB |
Output is correct |
23 |
Correct |
2 ms |
1108 KB |
Output is correct |
24 |
Correct |
2 ms |
1108 KB |
Output is correct |
25 |
Correct |
2 ms |
1108 KB |
Output is correct |
26 |
Correct |
2 ms |
1108 KB |
Output is correct |
27 |
Correct |
2 ms |
1108 KB |
Output is correct |
28 |
Correct |
2 ms |
1108 KB |
Output is correct |
29 |
Correct |
2 ms |
1224 KB |
Output is correct |
30 |
Correct |
2 ms |
1108 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
352 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
3 ms |
1108 KB |
Output is correct |
22 |
Correct |
2 ms |
1108 KB |
Output is correct |
23 |
Correct |
2 ms |
1108 KB |
Output is correct |
24 |
Correct |
2 ms |
1108 KB |
Output is correct |
25 |
Correct |
2 ms |
1108 KB |
Output is correct |
26 |
Correct |
2 ms |
1108 KB |
Output is correct |
27 |
Correct |
2 ms |
1108 KB |
Output is correct |
28 |
Correct |
2 ms |
1108 KB |
Output is correct |
29 |
Correct |
2 ms |
1224 KB |
Output is correct |
30 |
Correct |
2 ms |
1108 KB |
Output is correct |
31 |
Correct |
24 ms |
4820 KB |
Output is correct |
32 |
Correct |
23 ms |
4836 KB |
Output is correct |
33 |
Correct |
23 ms |
4828 KB |
Output is correct |
34 |
Correct |
21 ms |
4836 KB |
Output is correct |
35 |
Correct |
23 ms |
4836 KB |
Output is correct |
36 |
Correct |
22 ms |
4820 KB |
Output is correct |
37 |
Correct |
22 ms |
4820 KB |
Output is correct |
38 |
Correct |
22 ms |
4840 KB |
Output is correct |
39 |
Correct |
22 ms |
4820 KB |
Output is correct |
40 |
Correct |
22 ms |
4784 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
352 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
3 ms |
1108 KB |
Output is correct |
22 |
Correct |
2 ms |
1108 KB |
Output is correct |
23 |
Correct |
2 ms |
1108 KB |
Output is correct |
24 |
Correct |
2 ms |
1108 KB |
Output is correct |
25 |
Correct |
2 ms |
1108 KB |
Output is correct |
26 |
Correct |
2 ms |
1108 KB |
Output is correct |
27 |
Correct |
2 ms |
1108 KB |
Output is correct |
28 |
Correct |
2 ms |
1108 KB |
Output is correct |
29 |
Correct |
2 ms |
1224 KB |
Output is correct |
30 |
Correct |
2 ms |
1108 KB |
Output is correct |
31 |
Correct |
24 ms |
4820 KB |
Output is correct |
32 |
Correct |
23 ms |
4836 KB |
Output is correct |
33 |
Correct |
23 ms |
4828 KB |
Output is correct |
34 |
Correct |
21 ms |
4836 KB |
Output is correct |
35 |
Correct |
23 ms |
4836 KB |
Output is correct |
36 |
Correct |
22 ms |
4820 KB |
Output is correct |
37 |
Correct |
22 ms |
4820 KB |
Output is correct |
38 |
Correct |
22 ms |
4840 KB |
Output is correct |
39 |
Correct |
22 ms |
4820 KB |
Output is correct |
40 |
Correct |
22 ms |
4784 KB |
Output is correct |
41 |
Correct |
989 ms |
67348 KB |
Output is correct |
42 |
Correct |
974 ms |
67380 KB |
Output is correct |
43 |
Correct |
981 ms |
67472 KB |
Output is correct |
44 |
Correct |
974 ms |
67532 KB |
Output is correct |
45 |
Correct |
990 ms |
67464 KB |
Output is correct |
46 |
Correct |
986 ms |
67492 KB |
Output is correct |
47 |
Correct |
990 ms |
67488 KB |
Output is correct |
48 |
Correct |
976 ms |
67632 KB |
Output is correct |
49 |
Correct |
984 ms |
67508 KB |
Output is correct |
50 |
Correct |
976 ms |
67532 KB |
Output is correct |
51 |
Correct |
984 ms |
67832 KB |
Output is correct |
52 |
Correct |
974 ms |
67512 KB |
Output is correct |
53 |
Correct |
962 ms |
67464 KB |
Output is correct |
54 |
Correct |
986 ms |
67388 KB |
Output is correct |
55 |
Correct |
969 ms |
67552 KB |
Output is correct |