#include<bits/stdc++.h>
#include "books.h"
using namespace std;
using ll=long long;
const ll MAX_TAILLE=1000*1000+5,MAX_MEMO=1000+5,DECA=(1<<10),INFINI=1000*1000*1000;
ll nbVal,rep,posDeb,nbCompo,debInter,finInter,maxFin;
vector<ll> obj,listeCompo;
ll idCompo[MAX_TAILLE],finCompo[MAX_TAILLE],tailleCompo[MAX_TAILLE],debCompo[MAX_TAILLE];
ll arbreMin[2*DECA],arbreMax[2*DECA];
ll memo[MAX_MEMO][MAX_MEMO];
void DFS(ll pos) {
if (idCompo[pos]==0) {
idCompo[pos]=nbCompo;
tailleCompo[nbCompo]++;
DFS(obj[pos]);
}
}
ll calcMin(ll deb,ll fin) {
if (deb==fin) {
return arbreMin[deb];
}
if (deb%2==1) {
return min(arbreMin[deb],calcMin(deb+1,fin));
}
if (fin%2==0) {
return min(arbreMin[fin],calcMin(deb,fin-1));
}
return calcMin(deb/2,fin/2);
}
ll calcMax(ll deb,ll fin) {
if (deb==fin) {
return arbreMax[deb];
}
if (deb%2==1) {
return max(arbreMax[deb],calcMax(deb+1,fin));
}
if (fin%2==0) {
return max(arbreMax[fin],calcMax(deb,fin-1));
}
return calcMax(deb/2,fin/2);
}
ll dyna(ll deb,ll fin) {
if (deb<=debInter and fin>=finInter) {
return 0;
}
if (memo[deb][fin]!=-1) {
return memo[deb][fin];
}
memo[deb][fin]=INFINI;
if (fin<nbVal-1) {
if (calcMax(DECA+deb,DECA+fin)>fin) {
memo[deb][fin]=min(memo[deb][fin],dyna(deb,fin+1));
}
else {
memo[deb][fin]=min(memo[deb][fin],dyna(deb,fin+1)+2);
}
}
if (deb>0) {
if (calcMin(DECA+deb,DECA+fin)<deb) {
memo[deb][fin]=min(memo[deb][fin],dyna(deb-1,fin));
}
else {
memo[deb][fin]=min(memo[deb][fin],dyna(deb-1,fin)+2);
}
}
return memo[deb][fin];
}
ll minimum_walk(vector<int> p, int s) {
posDeb=s;
nbVal=p.size();
for (auto i:p) {
obj.push_back(i);
}
for (ll i=nbVal-1;i>=0;i--) {
if (idCompo[i]==0) {
nbCompo++;
DFS(i);
finCompo[nbCompo]=i;
}
debCompo[idCompo[i]]=i;
rep+=abs(i-obj[i]);
}
finInter=nbVal-1;
while (finInter>=0 and tailleCompo[idCompo[finInter]]==1) {
finInter--;
}
debInter=0;
while (debInter<=nbVal-1 and tailleCompo[idCompo[debInter]]==1) {
debInter++;
}
if (posDeb==0) {
ll pos=0;
while (pos<finInter) {
maxFin=max(maxFin,finCompo[idCompo[pos]]);
if (maxFin==pos) {
rep+=2;
}
pos++;
}
return rep;
}
for (ll i=0;i<MAX_MEMO;i++) {
for (ll j=0;j<MAX_MEMO;j++) {
memo[i][j]=-1;
}
}
for (ll i=0;i<2*DECA;i++) {
arbreMin[i]=INFINI;
arbreMax[i]=-INFINI;
}
for (ll i=0;i<nbVal;i++) {
arbreMin[DECA+i]=debCompo[idCompo[i]];
arbreMax[DECA+i]=finCompo[idCompo[i]];
}
for (ll i=DECA-1;i>0;i--) {
arbreMin[i]=min(arbreMin[2*i],arbreMin[2*i+1]);
arbreMax[i]=max(arbreMax[2*i],arbreMax[2*i+1]);
}
return rep+dyna(posDeb,posDeb);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 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 |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
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 |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 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 |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
0 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 |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
0 ms |
340 KB |
Output is correct |
23 |
Correct |
0 ms |
340 KB |
Output is correct |
24 |
Correct |
0 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
0 ms |
340 KB |
Output is correct |
27 |
Correct |
0 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
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 |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 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 |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
0 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 |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
0 ms |
340 KB |
Output is correct |
23 |
Correct |
0 ms |
340 KB |
Output is correct |
24 |
Correct |
0 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
0 ms |
340 KB |
Output is correct |
27 |
Correct |
0 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
0 ms |
340 KB |
Output is correct |
30 |
Correct |
170 ms |
23948 KB |
Output is correct |
31 |
Correct |
178 ms |
23972 KB |
Output is correct |
32 |
Correct |
107 ms |
47396 KB |
Output is correct |
33 |
Correct |
108 ms |
40168 KB |
Output is correct |
34 |
Correct |
99 ms |
46928 KB |
Output is correct |
35 |
Correct |
99 ms |
43472 KB |
Output is correct |
36 |
Correct |
94 ms |
36684 KB |
Output is correct |
37 |
Correct |
92 ms |
31456 KB |
Output is correct |
38 |
Correct |
94 ms |
30816 KB |
Output is correct |
39 |
Correct |
118 ms |
30760 KB |
Output is correct |
40 |
Correct |
93 ms |
30672 KB |
Output is correct |
41 |
Correct |
115 ms |
30680 KB |
Output is correct |
42 |
Correct |
104 ms |
30580 KB |
Output is correct |
43 |
Correct |
122 ms |
48072 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
39 ms |
8276 KB |
Output is correct |
2 |
Correct |
8 ms |
8356 KB |
Output is correct |
3 |
Correct |
37 ms |
8276 KB |
Output is correct |
4 |
Correct |
37 ms |
8276 KB |
Output is correct |
5 |
Correct |
40 ms |
8276 KB |
Output is correct |
6 |
Correct |
36 ms |
8276 KB |
Output is correct |
7 |
Correct |
37 ms |
8276 KB |
Output is correct |
8 |
Correct |
36 ms |
8276 KB |
Output is correct |
9 |
Correct |
31 ms |
8276 KB |
Output is correct |
10 |
Correct |
3 ms |
8276 KB |
Output is correct |
11 |
Correct |
38 ms |
8336 KB |
Output is correct |
12 |
Correct |
38 ms |
8340 KB |
Output is correct |
13 |
Correct |
35 ms |
8328 KB |
Output is correct |
14 |
Correct |
36 ms |
8276 KB |
Output is correct |
15 |
Correct |
35 ms |
8276 KB |
Output is correct |
16 |
Correct |
36 ms |
8276 KB |
Output is correct |
17 |
Correct |
39 ms |
8344 KB |
Output is correct |
18 |
Correct |
39 ms |
8340 KB |
Output is correct |
19 |
Correct |
38 ms |
8276 KB |
Output is correct |
20 |
Correct |
25 ms |
8276 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 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 |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
0 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 |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
0 ms |
340 KB |
Output is correct |
23 |
Correct |
0 ms |
340 KB |
Output is correct |
24 |
Correct |
0 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
0 ms |
340 KB |
Output is correct |
27 |
Correct |
0 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
0 ms |
340 KB |
Output is correct |
30 |
Correct |
170 ms |
23948 KB |
Output is correct |
31 |
Correct |
178 ms |
23972 KB |
Output is correct |
32 |
Correct |
107 ms |
47396 KB |
Output is correct |
33 |
Correct |
108 ms |
40168 KB |
Output is correct |
34 |
Correct |
99 ms |
46928 KB |
Output is correct |
35 |
Correct |
99 ms |
43472 KB |
Output is correct |
36 |
Correct |
94 ms |
36684 KB |
Output is correct |
37 |
Correct |
92 ms |
31456 KB |
Output is correct |
38 |
Correct |
94 ms |
30816 KB |
Output is correct |
39 |
Correct |
118 ms |
30760 KB |
Output is correct |
40 |
Correct |
93 ms |
30672 KB |
Output is correct |
41 |
Correct |
115 ms |
30680 KB |
Output is correct |
42 |
Correct |
104 ms |
30580 KB |
Output is correct |
43 |
Correct |
122 ms |
48072 KB |
Output is correct |
44 |
Correct |
39 ms |
8276 KB |
Output is correct |
45 |
Correct |
8 ms |
8356 KB |
Output is correct |
46 |
Correct |
37 ms |
8276 KB |
Output is correct |
47 |
Correct |
37 ms |
8276 KB |
Output is correct |
48 |
Correct |
40 ms |
8276 KB |
Output is correct |
49 |
Correct |
36 ms |
8276 KB |
Output is correct |
50 |
Correct |
37 ms |
8276 KB |
Output is correct |
51 |
Correct |
36 ms |
8276 KB |
Output is correct |
52 |
Correct |
31 ms |
8276 KB |
Output is correct |
53 |
Correct |
3 ms |
8276 KB |
Output is correct |
54 |
Correct |
38 ms |
8336 KB |
Output is correct |
55 |
Correct |
38 ms |
8340 KB |
Output is correct |
56 |
Correct |
35 ms |
8328 KB |
Output is correct |
57 |
Correct |
36 ms |
8276 KB |
Output is correct |
58 |
Correct |
35 ms |
8276 KB |
Output is correct |
59 |
Correct |
36 ms |
8276 KB |
Output is correct |
60 |
Correct |
39 ms |
8344 KB |
Output is correct |
61 |
Correct |
39 ms |
8340 KB |
Output is correct |
62 |
Correct |
38 ms |
8276 KB |
Output is correct |
63 |
Correct |
25 ms |
8276 KB |
Output is correct |
64 |
Runtime error |
166 ms |
110224 KB |
Execution killed with signal 11 |
65 |
Halted |
0 ms |
0 KB |
- |