Submission #984035

# Submission time Handle Problem Language Result Execution time Memory
984035 2024-05-16T09:28:33 Z CSQ31 Ancient Books (IOI17_books) C++17
50 / 100
107 ms 28244 KB
#include "books.h"
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define fi first
#define se second
#define sz(a) (int)(a.size())
#define all(a) a.begin(),a.end()
typedef long long int ll;
typedef pair<int,int> pii;
const int MAXN = 1e6+1;
int par[MAXN];
int mn[MAXN],mx[MAXN];

int find(int x){
	if(x==par[x])return x;
	return par[x] = find(par[x]);
}

void unite(int a,int b){
	a = find(a);
	b = find(b);
	if(a==b)return;
	mx[b] = max(mx[b],mx[a]);
	mn[b] = min(mn[b],mn[a]);
	par[a] = b;
}
ll ans = 0;
int n = 0;
void solve(int l,int r){
	int r0 = r;
	int l0 = l;
	int rcost = 0;
	int lcost = 0;

	int rlast = r;
	int llast = l;
	while(r0 < n){
		if(mn[find(r0)] < l){ 
			rcost += 2*(r0 - rlast);
			break;
		}
		if(mx[find(r0)] > r0){
			rcost += 2*(r0 - rlast);
			r0 = mx[find(r0)];
			rlast = r0;
		}else r0++;
	}
	while(l0 >= 0){
		if(mx[find(l0)] > r){
			lcost += 2*(llast - l0);
			break;
		}
		if(mn[find(l0)] < l0){
			lcost += 2*(llast - l0);
			l0 = mn[find(l0)];
			llast = l0;
		}else l0--;
	}
	if(l0 == -1 && r0 == n){
		ans += lcost;
		ans += rcost;
		return;
	}

	ans += min(lcost,rcost);
	l = mn[find(l0)];
	r = mx[find(r0)];
	solve(l,r);
}
ll minimum_walk(vector<int> p, int s) {
	n = sz(p);
	for(int i=0;i<n;i++)ans+=abs(i-p[i]);
	for(int i=0;i<n;i++){
		par[i] = i;
		mn[i] = min(i,p[i]);
		mx[i] = max(i,p[i]);
	}

	for(int i=0;i<n;i++){
		int l = min(i,p[i]);
		int r = max(i,p[i]);
		if(l==r)continue;

		while(true){
			l = find(l);
			if(l >= r)break;
			unite(l,l+1);
		}
	}

	solve(s,s);
	return ans;	
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 1 ms 4540 KB Output is correct
5 Correct 1 ms 4444 KB Output is correct
6 Correct 1 ms 4488 KB Output is correct
7 Correct 1 ms 4444 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 1 ms 4440 KB Output is correct
10 Correct 1 ms 4440 KB Output is correct
11 Correct 1 ms 4444 KB Output is correct
12 Correct 1 ms 4444 KB Output is correct
13 Correct 1 ms 4444 KB Output is correct
14 Correct 1 ms 4444 KB Output is correct
15 Correct 1 ms 4444 KB Output is correct
16 Correct 1 ms 4444 KB Output is correct
17 Correct 1 ms 4440 KB Output is correct
18 Correct 1 ms 4444 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 1 ms 4540 KB Output is correct
5 Correct 1 ms 4444 KB Output is correct
6 Correct 1 ms 4488 KB Output is correct
7 Correct 1 ms 4444 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 1 ms 4440 KB Output is correct
10 Correct 1 ms 4440 KB Output is correct
11 Correct 1 ms 4444 KB Output is correct
12 Correct 1 ms 4444 KB Output is correct
13 Correct 1 ms 4444 KB Output is correct
14 Correct 1 ms 4444 KB Output is correct
15 Correct 1 ms 4444 KB Output is correct
16 Correct 1 ms 4444 KB Output is correct
17 Correct 1 ms 4440 KB Output is correct
18 Correct 1 ms 4444 KB Output is correct
19 Correct 1 ms 4444 KB Output is correct
20 Correct 1 ms 4444 KB Output is correct
21 Correct 1 ms 4444 KB Output is correct
22 Correct 1 ms 4444 KB Output is correct
23 Correct 1 ms 4444 KB Output is correct
24 Correct 1 ms 4444 KB Output is correct
25 Correct 1 ms 4444 KB Output is correct
26 Correct 1 ms 4444 KB Output is correct
27 Correct 1 ms 4444 KB Output is correct
28 Correct 1 ms 4444 KB Output is correct
29 Correct 1 ms 4444 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 1 ms 4540 KB Output is correct
5 Correct 1 ms 4444 KB Output is correct
6 Correct 1 ms 4488 KB Output is correct
7 Correct 1 ms 4444 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 1 ms 4440 KB Output is correct
10 Correct 1 ms 4440 KB Output is correct
11 Correct 1 ms 4444 KB Output is correct
12 Correct 1 ms 4444 KB Output is correct
13 Correct 1 ms 4444 KB Output is correct
14 Correct 1 ms 4444 KB Output is correct
15 Correct 1 ms 4444 KB Output is correct
16 Correct 1 ms 4444 KB Output is correct
17 Correct 1 ms 4440 KB Output is correct
18 Correct 1 ms 4444 KB Output is correct
19 Correct 1 ms 4444 KB Output is correct
20 Correct 1 ms 4444 KB Output is correct
21 Correct 1 ms 4444 KB Output is correct
22 Correct 1 ms 4444 KB Output is correct
23 Correct 1 ms 4444 KB Output is correct
24 Correct 1 ms 4444 KB Output is correct
25 Correct 1 ms 4444 KB Output is correct
26 Correct 1 ms 4444 KB Output is correct
27 Correct 1 ms 4444 KB Output is correct
28 Correct 1 ms 4444 KB Output is correct
29 Correct 1 ms 4444 KB Output is correct
30 Correct 97 ms 28244 KB Output is correct
31 Correct 97 ms 26836 KB Output is correct
32 Correct 74 ms 19960 KB Output is correct
33 Correct 85 ms 19792 KB Output is correct
34 Correct 88 ms 19964 KB Output is correct
35 Correct 94 ms 19972 KB Output is correct
36 Correct 97 ms 19972 KB Output is correct
37 Correct 90 ms 19796 KB Output is correct
38 Correct 90 ms 19880 KB Output is correct
39 Correct 102 ms 20048 KB Output is correct
40 Correct 107 ms 20228 KB Output is correct
41 Correct 98 ms 26124 KB Output is correct
42 Correct 100 ms 23028 KB Output is correct
43 Correct 93 ms 28240 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 4444 KB 3rd lines differ - on the 1st token, expected: '3304', found: '2744'
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 1 ms 4540 KB Output is correct
5 Correct 1 ms 4444 KB Output is correct
6 Correct 1 ms 4488 KB Output is correct
7 Correct 1 ms 4444 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 1 ms 4440 KB Output is correct
10 Correct 1 ms 4440 KB Output is correct
11 Correct 1 ms 4444 KB Output is correct
12 Correct 1 ms 4444 KB Output is correct
13 Correct 1 ms 4444 KB Output is correct
14 Correct 1 ms 4444 KB Output is correct
15 Correct 1 ms 4444 KB Output is correct
16 Correct 1 ms 4444 KB Output is correct
17 Correct 1 ms 4440 KB Output is correct
18 Correct 1 ms 4444 KB Output is correct
19 Correct 1 ms 4444 KB Output is correct
20 Correct 1 ms 4444 KB Output is correct
21 Correct 1 ms 4444 KB Output is correct
22 Correct 1 ms 4444 KB Output is correct
23 Correct 1 ms 4444 KB Output is correct
24 Correct 1 ms 4444 KB Output is correct
25 Correct 1 ms 4444 KB Output is correct
26 Correct 1 ms 4444 KB Output is correct
27 Correct 1 ms 4444 KB Output is correct
28 Correct 1 ms 4444 KB Output is correct
29 Correct 1 ms 4444 KB Output is correct
30 Correct 97 ms 28244 KB Output is correct
31 Correct 97 ms 26836 KB Output is correct
32 Correct 74 ms 19960 KB Output is correct
33 Correct 85 ms 19792 KB Output is correct
34 Correct 88 ms 19964 KB Output is correct
35 Correct 94 ms 19972 KB Output is correct
36 Correct 97 ms 19972 KB Output is correct
37 Correct 90 ms 19796 KB Output is correct
38 Correct 90 ms 19880 KB Output is correct
39 Correct 102 ms 20048 KB Output is correct
40 Correct 107 ms 20228 KB Output is correct
41 Correct 98 ms 26124 KB Output is correct
42 Correct 100 ms 23028 KB Output is correct
43 Correct 93 ms 28240 KB Output is correct
44 Incorrect 1 ms 4444 KB 3rd lines differ - on the 1st token, expected: '3304', found: '2744'
45 Halted 0 ms 0 KB -