Submission #797388

# Submission time Handle Problem Language Result Execution time Memory
797388 2023-07-29T10:16:49 Z alvingogo Ancient Books (IOI17_books) C++14
50 / 100
240 ms 36344 KB
#include "books.h"
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#define AquA cin.tie(0);ios_base::sync_with_stdio(0);
#define fs first
#define sc second
#define p_q priority_queue
using namespace std;

typedef long long ll;
struct DSU{
	vector<int> bo;
	void init(int x){
		bo.resize(x);
		iota(bo.begin(),bo.end(),0);
	}
	int find(int x){
		return bo[x]==x?x:bo[x]=find(bo[x]);
	}
	int merge(int x,int y){
		x=find(x);
		y=find(y);
		if(x==y){
			return 0;
		}
		bo[x]=y;
		return 1;
	}
}dsu;
ll minimum_walk(vector<int> p, int s) {
	ll ans=0;
	int n=p.size();
	dsu.init(n);
	vector<int> bo(n,-1),k(n),pre(n);
	for(int i=0;i<n;i++){
		if(bo[i]==-1){
			pre[i]++;
			int cnt=0;
			int f=0;
			for(int x=i;bo[x]==-1;x=p[x]){
				dsu.merge(x,i);
				bo[x]=i;
				cnt++;
				f=max(f,x);
			}
			if(cnt>=2){
				k[i]=1;
			}
			pre[f]--;
		}
		ans+=abs(i-p[i]);
	}
	for(int i=1;i<n;i++){
		pre[i]+=pre[i-1];
	}
	k[bo[s]]=1;
	for(int i=0;i+1<n;i++){
		if(pre[i]){
			dsu.merge(i,i+1);
		}
	}
	int mn=-1;
	vector<pair<int,pair<int,int> > > e;
	for(int i=0;i<n;i++){
		if(k[bo[i]]){
			if(mn!=-1){
				e.push_back({i-mn,{i,mn}});
			}
			mn=i;
		}
	}
	sort(e.begin(),e.end());
	for(auto h:e){
		ans+=2*dsu.merge(h.sc.fs,h.sc.sc)*h.fs;
	}
	return ans;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 0 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 0 ms 212 KB Output is correct
22 Correct 1 ms 216 KB Output is correct
23 Correct 0 ms 340 KB Output is correct
24 Correct 0 ms 340 KB Output is correct
25 Correct 0 ms 340 KB Output is correct
26 Correct 0 ms 340 KB Output is correct
27 Correct 1 ms 296 KB Output is correct
28 Correct 0 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 0 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 0 ms 212 KB Output is correct
22 Correct 1 ms 216 KB Output is correct
23 Correct 0 ms 340 KB Output is correct
24 Correct 0 ms 340 KB Output is correct
25 Correct 0 ms 340 KB Output is correct
26 Correct 0 ms 340 KB Output is correct
27 Correct 1 ms 296 KB Output is correct
28 Correct 0 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 174 ms 36184 KB Output is correct
31 Correct 185 ms 36176 KB Output is correct
32 Correct 79 ms 23756 KB Output is correct
33 Correct 140 ms 30052 KB Output is correct
34 Correct 142 ms 30040 KB Output is correct
35 Correct 161 ms 36212 KB Output is correct
36 Correct 187 ms 36196 KB Output is correct
37 Correct 194 ms 36184 KB Output is correct
38 Correct 240 ms 36180 KB Output is correct
39 Correct 235 ms 36180 KB Output is correct
40 Correct 215 ms 36188 KB Output is correct
41 Correct 160 ms 36152 KB Output is correct
42 Correct 194 ms 36344 KB Output is correct
43 Correct 154 ms 30012 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 212 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 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 0 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 0 ms 212 KB Output is correct
22 Correct 1 ms 216 KB Output is correct
23 Correct 0 ms 340 KB Output is correct
24 Correct 0 ms 340 KB Output is correct
25 Correct 0 ms 340 KB Output is correct
26 Correct 0 ms 340 KB Output is correct
27 Correct 1 ms 296 KB Output is correct
28 Correct 0 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 174 ms 36184 KB Output is correct
31 Correct 185 ms 36176 KB Output is correct
32 Correct 79 ms 23756 KB Output is correct
33 Correct 140 ms 30052 KB Output is correct
34 Correct 142 ms 30040 KB Output is correct
35 Correct 161 ms 36212 KB Output is correct
36 Correct 187 ms 36196 KB Output is correct
37 Correct 194 ms 36184 KB Output is correct
38 Correct 240 ms 36180 KB Output is correct
39 Correct 235 ms 36180 KB Output is correct
40 Correct 215 ms 36188 KB Output is correct
41 Correct 160 ms 36152 KB Output is correct
42 Correct 194 ms 36344 KB Output is correct
43 Correct 154 ms 30012 KB Output is correct
44 Incorrect 0 ms 212 KB 3rd lines differ - on the 1st token, expected: '3304', found: '2744'
45 Halted 0 ms 0 KB -