# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
364733 |
2021-02-09T20:44:02 Z |
rqi |
Ancient Books (IOI17_books) |
C++14 |
|
1617 ms |
1048580 KB |
#include "books.h"
#include <bits/stdc++.h>
using namespace std;
typedef vector<int> vi;
typedef long long ll;
#define sz(x) (int)(x).size()
#define pb push_back
#define bk back()
const int MOD = 1e9+7;
const int mx = 1000005;
vi p;
int s;
int cycL[mx]; //leftmost of a cycle
int cycR[mx]; //rightmost of a cycle
void popLRSingles(){
int L = 0;
for(int i = 0; i < s; i++){
if(p[i] == i){
L = i+1;
}
else{
break;
}
}
vi newp;
for(int i = L; i < sz(p); i++){
newp.pb(p[i]);
}
p = newp;
s = s-L;
while(true){
if(sz(p)-1 == s) break;
if(p[sz(p)-1] == sz(p)-1){
p.pop_back();
}
else{
break;
}
}
}
int curL, curR;
vi Lfrees, Rfrees;
void getFrees(){
while(true){
bool updated = false;
while(sz(Lfrees)){
int node = Lfrees.bk;
if(node < curL) break;
updated = true;
Lfrees.pop_back();
curL = min(curL, cycL[node]);
curR = max(curR, cycR[node]);
}
while(sz(Rfrees)){
int node = Rfrees.bk;
if(node > curR) break;
updated = true;
Rfrees.pop_back();
curL = min(curL, cycL[node]);
curR = max(curR, cycR[node]);
}
if(!updated) break;
}
}
long long minimum_walk(vi _p, int _s) {
p = _p;
s = _s;
popLRSingles();
int n = sz(p);
for(int i = 0; i < n; i++){
cycL[i] = MOD;
cycR[i] = -1;
}
for(int i = 0; i < n; i++){
if(cycR[i] == -1){
int curnode = i;
vi cycle_nodes;
while(true){
cycle_nodes.pb(curnode);
curnode = p[curnode];
if(curnode == i) break;
}
for(auto u: cycle_nodes){
cycL[i] = min(cycL[i], u);
cycR[i] = max(cycR[i], u);
}
for(auto u: cycle_nodes){
cycL[u] = cycL[i];
cycR[u] = cycR[i];
}
}
}
ll ans = 0;
for(int i = 0; i < n; i++){
ans+=abs(p[i]-i);
}
curL = s;
curR = s;
for(int i = 0; i <= s; i++){
Lfrees.pb(i);
}
for(int i = n-1; i >= s; i--){
Rfrees.pb(i);
}
getFrees();
while(curL > 0 || curR < n-1){
int prev_curL = curL;
int prev_curR = curR;
ll L_cost = 0;
ll R_cost = 0;
bool non_boost = false;
for(int i = curL-1; i >= 0; i--){
if(i+1 == curL){
L_cost+=2;
curL--;
}
curL = min(curL, cycL[i]);
if(cycR[i] > prev_curR){
//found non boost
non_boost = true;
break;
}
}
for(int i = curR+1; i < n; i++){
if(i-1 == curR){
R_cost+=2;
curR++;
}
curR = max(curR, cycR[i]);
if(cycL[i] < prev_curL){
non_boost = true;
break;
}
}
if(!non_boost){
ans+=L_cost+R_cost;
}
else{
ans+=min(L_cost, R_cost);
}
getFrees();
}
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
0 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
0 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
0 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
0 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
1 ms |
364 KB |
Output is correct |
22 |
Correct |
1 ms |
364 KB |
Output is correct |
23 |
Correct |
1 ms |
364 KB |
Output is correct |
24 |
Correct |
1 ms |
364 KB |
Output is correct |
25 |
Correct |
1 ms |
364 KB |
Output is correct |
26 |
Correct |
1 ms |
384 KB |
Output is correct |
27 |
Correct |
1 ms |
380 KB |
Output is correct |
28 |
Correct |
1 ms |
364 KB |
Output is correct |
29 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
0 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
0 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
1 ms |
364 KB |
Output is correct |
22 |
Correct |
1 ms |
364 KB |
Output is correct |
23 |
Correct |
1 ms |
364 KB |
Output is correct |
24 |
Correct |
1 ms |
364 KB |
Output is correct |
25 |
Correct |
1 ms |
364 KB |
Output is correct |
26 |
Correct |
1 ms |
384 KB |
Output is correct |
27 |
Correct |
1 ms |
380 KB |
Output is correct |
28 |
Correct |
1 ms |
364 KB |
Output is correct |
29 |
Correct |
1 ms |
364 KB |
Output is correct |
30 |
Correct |
216 ms |
31348 KB |
Output is correct |
31 |
Correct |
221 ms |
31492 KB |
Output is correct |
32 |
Correct |
146 ms |
19804 KB |
Output is correct |
33 |
Correct |
201 ms |
31364 KB |
Output is correct |
34 |
Correct |
207 ms |
31364 KB |
Output is correct |
35 |
Correct |
220 ms |
31364 KB |
Output is correct |
36 |
Correct |
196 ms |
31364 KB |
Output is correct |
37 |
Correct |
179 ms |
31364 KB |
Output is correct |
38 |
Correct |
178 ms |
31364 KB |
Output is correct |
39 |
Correct |
183 ms |
31308 KB |
Output is correct |
40 |
Correct |
184 ms |
31308 KB |
Output is correct |
41 |
Correct |
202 ms |
31364 KB |
Output is correct |
42 |
Correct |
239 ms |
31364 KB |
Output is correct |
43 |
Correct |
203 ms |
31364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Runtime error |
1617 ms |
1048580 KB |
Execution killed with signal 9 |
11 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
0 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
0 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
1 ms |
364 KB |
Output is correct |
22 |
Correct |
1 ms |
364 KB |
Output is correct |
23 |
Correct |
1 ms |
364 KB |
Output is correct |
24 |
Correct |
1 ms |
364 KB |
Output is correct |
25 |
Correct |
1 ms |
364 KB |
Output is correct |
26 |
Correct |
1 ms |
384 KB |
Output is correct |
27 |
Correct |
1 ms |
380 KB |
Output is correct |
28 |
Correct |
1 ms |
364 KB |
Output is correct |
29 |
Correct |
1 ms |
364 KB |
Output is correct |
30 |
Correct |
216 ms |
31348 KB |
Output is correct |
31 |
Correct |
221 ms |
31492 KB |
Output is correct |
32 |
Correct |
146 ms |
19804 KB |
Output is correct |
33 |
Correct |
201 ms |
31364 KB |
Output is correct |
34 |
Correct |
207 ms |
31364 KB |
Output is correct |
35 |
Correct |
220 ms |
31364 KB |
Output is correct |
36 |
Correct |
196 ms |
31364 KB |
Output is correct |
37 |
Correct |
179 ms |
31364 KB |
Output is correct |
38 |
Correct |
178 ms |
31364 KB |
Output is correct |
39 |
Correct |
183 ms |
31308 KB |
Output is correct |
40 |
Correct |
184 ms |
31308 KB |
Output is correct |
41 |
Correct |
202 ms |
31364 KB |
Output is correct |
42 |
Correct |
239 ms |
31364 KB |
Output is correct |
43 |
Correct |
203 ms |
31364 KB |
Output is correct |
44 |
Correct |
1 ms |
364 KB |
Output is correct |
45 |
Correct |
1 ms |
364 KB |
Output is correct |
46 |
Correct |
1 ms |
364 KB |
Output is correct |
47 |
Correct |
1 ms |
364 KB |
Output is correct |
48 |
Correct |
1 ms |
364 KB |
Output is correct |
49 |
Correct |
1 ms |
364 KB |
Output is correct |
50 |
Correct |
1 ms |
364 KB |
Output is correct |
51 |
Correct |
1 ms |
364 KB |
Output is correct |
52 |
Correct |
1 ms |
364 KB |
Output is correct |
53 |
Runtime error |
1617 ms |
1048580 KB |
Execution killed with signal 9 |
54 |
Halted |
0 ms |
0 KB |
- |