Submission #404124

# Submission time Handle Problem Language Result Execution time Memory
404124 2021-05-13T19:59:59 Z b00n0rp Ancient Books (IOI17_books) C++17
50 / 100
194 ms 19780 KB
#include<bits/stdc++.h>
#include "books.h"
using namespace std;

#define vi vector<int>
#define REP(i,n) for(int i = 0; i < n; i ++)
#define FOR(i,a,b) for(int i = a; i < b; i ++)

const int MX = 1000005;

int n,S;
int lft,rgt,cur = 0;
int C[MX],L[MX],R[MX];

int solve(int l,int r){
	int ll = L[C[l]];
	int rr = R[C[r]];
	while(ll < l or rr > r){
		if(ll < l){
			l--;
			ll = min(ll,L[C[l]]);
			rr = max(rr,R[C[l]]);
		}
		else{
			r++;
			rr = max(rr,R[C[r]]);
			ll = min(ll,L[C[r]]);
		}
	}
	int costleft = 0,costright = 0;
	int indleft = -1,indright = -1;
	int mn = l,mx = r;
	for(int i = l-1; i >= lft; i --){
		if(R[C[i]] > S){
			indleft = i;
			break;
		}
	}
	for(int i = r+1; i <= rgt; i ++){
		if(L[C[i]] < S){
			indright = i;
			break;
		}
	}
	if(indleft == -1){
		for(int i = l-1; i >= lft; i --){
			mn = min(mn,L[C[i]]);
			if(mn == i) costleft += 2;
		}
		for(int i = r+1; i <= rgt; i ++){
			mx = max(mx,R[C[i]]);
			if(mx == i) costright += 2;
		}
		return costleft+costright;
	}
	while(ll > indleft){
		costleft += 2;
		ll--;
		while(ll < l){
			l--;
			ll = min(ll,L[C[l]]);
		}
	}
	while(rr < indright){
		costright += 2;
		rr++;
		while(rr > r){
			r++;
			rr = max(rr,R[C[r]]);
		}
	}
	return min(costleft,costright)+solve(ll,rr);
}

long long minimum_walk(vi p, int s) {
	S = s;
	long long ans = 0;
	n = p.size();
	lft = s;
	rgt = s;
	REP(i,n) C[i] = -1;
	REP(i,n){
		ans += abs(p[i]-i);
		if(C[i] == -1){
			C[i] = cur;
			L[cur] = R[cur] = i;
			int j = i;
			while(true){
				j = p[j];
				C[j] = cur;
				R[cur] = max(R[cur],j);
				if(i == j) break;
			}
			cur++;
		}
		if(i != p[i]){
			lft = min(lft,i);
			rgt = max(rgt,i);
		}
	}
	return ans+solve(s,s);
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 1 ms 204 KB Output is correct
22 Correct 1 ms 332 KB Output is correct
23 Correct 1 ms 204 KB Output is correct
24 Correct 1 ms 204 KB Output is correct
25 Correct 1 ms 204 KB Output is correct
26 Correct 1 ms 204 KB Output is correct
27 Correct 1 ms 204 KB Output is correct
28 Correct 1 ms 204 KB Output is correct
29 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 1 ms 204 KB Output is correct
22 Correct 1 ms 332 KB Output is correct
23 Correct 1 ms 204 KB Output is correct
24 Correct 1 ms 204 KB Output is correct
25 Correct 1 ms 204 KB Output is correct
26 Correct 1 ms 204 KB Output is correct
27 Correct 1 ms 204 KB Output is correct
28 Correct 1 ms 204 KB Output is correct
29 Correct 1 ms 332 KB Output is correct
30 Correct 167 ms 12100 KB Output is correct
31 Correct 194 ms 12044 KB Output is correct
32 Correct 124 ms 19780 KB Output is correct
33 Correct 136 ms 17388 KB Output is correct
34 Correct 140 ms 17384 KB Output is correct
35 Correct 133 ms 16248 KB Output is correct
36 Correct 148 ms 13928 KB Output is correct
37 Correct 129 ms 12264 KB Output is correct
38 Correct 152 ms 12016 KB Output is correct
39 Correct 156 ms 12008 KB Output is correct
40 Correct 140 ms 12016 KB Output is correct
41 Correct 167 ms 12016 KB Output is correct
42 Correct 166 ms 12012 KB Output is correct
43 Correct 149 ms 17784 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Incorrect 1 ms 332 KB 3rd lines differ - on the 1st token, expected: '9800', found: '9802'
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 1 ms 204 KB Output is correct
22 Correct 1 ms 332 KB Output is correct
23 Correct 1 ms 204 KB Output is correct
24 Correct 1 ms 204 KB Output is correct
25 Correct 1 ms 204 KB Output is correct
26 Correct 1 ms 204 KB Output is correct
27 Correct 1 ms 204 KB Output is correct
28 Correct 1 ms 204 KB Output is correct
29 Correct 1 ms 332 KB Output is correct
30 Correct 167 ms 12100 KB Output is correct
31 Correct 194 ms 12044 KB Output is correct
32 Correct 124 ms 19780 KB Output is correct
33 Correct 136 ms 17388 KB Output is correct
34 Correct 140 ms 17384 KB Output is correct
35 Correct 133 ms 16248 KB Output is correct
36 Correct 148 ms 13928 KB Output is correct
37 Correct 129 ms 12264 KB Output is correct
38 Correct 152 ms 12016 KB Output is correct
39 Correct 156 ms 12008 KB Output is correct
40 Correct 140 ms 12016 KB Output is correct
41 Correct 167 ms 12016 KB Output is correct
42 Correct 166 ms 12012 KB Output is correct
43 Correct 149 ms 17784 KB Output is correct
44 Correct 1 ms 332 KB Output is correct
45 Incorrect 1 ms 332 KB 3rd lines differ - on the 1st token, expected: '9800', found: '9802'
46 Halted 0 ms 0 KB -