#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;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
328 KB |
Output is correct |
9 |
Correct |
0 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
328 KB |
Output is correct |
9 |
Correct |
0 ms |
332 KB |
Output is correct |
10 |
Correct |
0 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 |
328 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
328 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
328 KB |
Output is correct |
9 |
Correct |
0 ms |
332 KB |
Output is correct |
10 |
Correct |
0 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 |
328 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
328 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
2 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 |
1228 KB |
Output is correct |
25 |
Correct |
2 ms |
1232 KB |
Output is correct |
26 |
Correct |
2 ms |
1228 KB |
Output is correct |
27 |
Correct |
2 ms |
1228 KB |
Output is correct |
28 |
Correct |
2 ms |
1108 KB |
Output is correct |
29 |
Correct |
2 ms |
1108 KB |
Output is correct |
30 |
Correct |
3 ms |
1236 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
328 KB |
Output is correct |
9 |
Correct |
0 ms |
332 KB |
Output is correct |
10 |
Correct |
0 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 |
328 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
328 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
2 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 |
1228 KB |
Output is correct |
25 |
Correct |
2 ms |
1232 KB |
Output is correct |
26 |
Correct |
2 ms |
1228 KB |
Output is correct |
27 |
Correct |
2 ms |
1228 KB |
Output is correct |
28 |
Correct |
2 ms |
1108 KB |
Output is correct |
29 |
Correct |
2 ms |
1108 KB |
Output is correct |
30 |
Correct |
3 ms |
1236 KB |
Output is correct |
31 |
Correct |
22 ms |
4820 KB |
Output is correct |
32 |
Correct |
23 ms |
4732 KB |
Output is correct |
33 |
Correct |
24 ms |
4808 KB |
Output is correct |
34 |
Correct |
25 ms |
4820 KB |
Output is correct |
35 |
Correct |
22 ms |
4820 KB |
Output is correct |
36 |
Correct |
23 ms |
4812 KB |
Output is correct |
37 |
Correct |
23 ms |
4820 KB |
Output is correct |
38 |
Correct |
23 ms |
4784 KB |
Output is correct |
39 |
Correct |
22 ms |
4844 KB |
Output is correct |
40 |
Correct |
23 ms |
4736 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
328 KB |
Output is correct |
9 |
Correct |
0 ms |
332 KB |
Output is correct |
10 |
Correct |
0 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 |
328 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
328 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
2 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 |
1228 KB |
Output is correct |
25 |
Correct |
2 ms |
1232 KB |
Output is correct |
26 |
Correct |
2 ms |
1228 KB |
Output is correct |
27 |
Correct |
2 ms |
1228 KB |
Output is correct |
28 |
Correct |
2 ms |
1108 KB |
Output is correct |
29 |
Correct |
2 ms |
1108 KB |
Output is correct |
30 |
Correct |
3 ms |
1236 KB |
Output is correct |
31 |
Correct |
22 ms |
4820 KB |
Output is correct |
32 |
Correct |
23 ms |
4732 KB |
Output is correct |
33 |
Correct |
24 ms |
4808 KB |
Output is correct |
34 |
Correct |
25 ms |
4820 KB |
Output is correct |
35 |
Correct |
22 ms |
4820 KB |
Output is correct |
36 |
Correct |
23 ms |
4812 KB |
Output is correct |
37 |
Correct |
23 ms |
4820 KB |
Output is correct |
38 |
Correct |
23 ms |
4784 KB |
Output is correct |
39 |
Correct |
22 ms |
4844 KB |
Output is correct |
40 |
Correct |
23 ms |
4736 KB |
Output is correct |
41 |
Correct |
991 ms |
67296 KB |
Output is correct |
42 |
Correct |
1004 ms |
67380 KB |
Output is correct |
43 |
Correct |
975 ms |
67528 KB |
Output is correct |
44 |
Correct |
979 ms |
67660 KB |
Output is correct |
45 |
Correct |
986 ms |
67512 KB |
Output is correct |
46 |
Correct |
987 ms |
67436 KB |
Output is correct |
47 |
Correct |
992 ms |
67416 KB |
Output is correct |
48 |
Correct |
985 ms |
67456 KB |
Output is correct |
49 |
Correct |
985 ms |
67388 KB |
Output is correct |
50 |
Correct |
981 ms |
67484 KB |
Output is correct |
51 |
Correct |
971 ms |
67408 KB |
Output is correct |
52 |
Correct |
978 ms |
67468 KB |
Output is correct |
53 |
Correct |
975 ms |
67404 KB |
Output is correct |
54 |
Correct |
972 ms |
67524 KB |
Output is correct |
55 |
Correct |
983 ms |
67460 KB |
Output is correct |