답안 #797528

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
797528 2023-07-29T14:15:04 Z alvingogo 고대 책들 (IOI17_books) C++14
100 / 100
182 ms 40528 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;
ll minimum_walk(vector<int> p, int s) {
	ll ans=0;
	int n=p.size();
	vector<int> bo(n,-1),bo2(n,-1),k(n),pre(n);
	vector<pair<int,int> > d(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]){
				bo[x]=i;
				cnt++;
				f=max(f,x);
			}
			if(cnt>=2){
				k[i]=1;
			}
			pre[f]--;
			for(int x=i;bo2[x]==-1;x=p[x]){
				bo2[x]=i;
				d[x]={i,f};
			}
		}
		ans+=abs(i-p[i]);
	}
	int l=d[s].fs,r=d[s].sc,nl=s,nr=s;
	while(nl>l || nr<r){
		if(nl>l){
			nl--;
			l=min(l,d[nl].fs);
			r=max(r,d[nl].sc);
		}
		else if(nr<r){
			nr++;
			l=min(l,d[nr].fs);
			r=max(r,d[nr].sc);
		}
	}
	for(int i=1;i<n;i++){
		pre[i]+=pre[i-1];
	}
	int g=0;
	k[bo[s]]=1;
	vector<pair<int,int> > e;
	for(int i=0;i<n;i++){
		if(pre[i]){

		}
		else{
			if(g<=nl && i>=nr){
				while(nl!=g && nr!=i){
					int pl=nl,pr=nr,al=nl,ar=nr;
					int c1=0;
					while(1){
						c1++;
						int dl=nl-1,dr=nr;
						while(nl>dl || nr<dr){
							if(nl>dl){
								nl--;
								dl=min(dl,d[nl].fs);
								dr=max(dr,d[nl].sc);
							}
							else if(nr<dr){
								nr++;
								dl=min(dl,d[nr].fs);
								dr=max(dr,d[nr].sc);
							}
						}
						if(nr!=pr){
							pl=nl;
							pr=nr;
							break;
						}
					}
					nl=al;
					nr=ar;
					int c2=0;
					while(1){
						c2++;
						int dl=nl,dr=nr+1;
						while(nl>dl || nr<dr){
							if(nl>dl){
								nl--;
								dl=min(dl,d[nl].fs);
								dr=max(dr,d[nl].sc);
							}
							else if(nr<dr){
								nr++;
								dl=min(dl,d[nr].fs);
								dr=max(dr,d[nr].sc);
							}
						}
						if(nl==pl){
							break;
						}
					}
					ans+=2*min(c1,c2);
				}
			}
			if(i!=g || i==s){
				e.push_back({g,i});
			}
			g=i+1;
		}
	}
	int nw=e[0].fs;
	for(auto h:e){
		ans+=2*(h.fs-nw);
		nw=h.sc;
	}
	return ans;
}
# 결과 실행 시간 메모리 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 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 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 1 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
# 결과 실행 시간 메모리 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 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 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 1 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 0 ms 212 KB Output is correct
22 Correct 1 ms 212 KB Output is correct
23 Correct 1 ms 212 KB Output is correct
24 Correct 0 ms 212 KB Output is correct
25 Correct 0 ms 212 KB Output is correct
26 Correct 0 ms 212 KB Output is correct
27 Correct 0 ms 212 KB Output is correct
28 Correct 0 ms 212 KB Output is correct
29 Correct 0 ms 212 KB Output is correct
# 결과 실행 시간 메모리 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 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 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 1 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 0 ms 212 KB Output is correct
22 Correct 1 ms 212 KB Output is correct
23 Correct 1 ms 212 KB Output is correct
24 Correct 0 ms 212 KB Output is correct
25 Correct 0 ms 212 KB Output is correct
26 Correct 0 ms 212 KB Output is correct
27 Correct 0 ms 212 KB Output is correct
28 Correct 0 ms 212 KB Output is correct
29 Correct 0 ms 212 KB Output is correct
30 Correct 164 ms 31724 KB Output is correct
31 Correct 182 ms 31608 KB Output is correct
32 Correct 83 ms 31604 KB Output is correct
33 Correct 92 ms 33768 KB Output is correct
34 Correct 91 ms 33768 KB Output is correct
35 Correct 97 ms 33768 KB Output is correct
36 Correct 95 ms 32864 KB Output is correct
37 Correct 91 ms 31692 KB Output is correct
38 Correct 95 ms 31596 KB Output is correct
39 Correct 96 ms 31592 KB Output is correct
40 Correct 103 ms 31608 KB Output is correct
41 Correct 120 ms 31604 KB Output is correct
42 Correct 114 ms 31600 KB Output is correct
43 Correct 90 ms 31564 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 296 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 1 ms 300 KB Output is correct
7 Correct 0 ms 300 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 256 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 296 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
# 결과 실행 시간 메모리 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 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 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 1 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 0 ms 212 KB Output is correct
22 Correct 1 ms 212 KB Output is correct
23 Correct 1 ms 212 KB Output is correct
24 Correct 0 ms 212 KB Output is correct
25 Correct 0 ms 212 KB Output is correct
26 Correct 0 ms 212 KB Output is correct
27 Correct 0 ms 212 KB Output is correct
28 Correct 0 ms 212 KB Output is correct
29 Correct 0 ms 212 KB Output is correct
30 Correct 164 ms 31724 KB Output is correct
31 Correct 182 ms 31608 KB Output is correct
32 Correct 83 ms 31604 KB Output is correct
33 Correct 92 ms 33768 KB Output is correct
34 Correct 91 ms 33768 KB Output is correct
35 Correct 97 ms 33768 KB Output is correct
36 Correct 95 ms 32864 KB Output is correct
37 Correct 91 ms 31692 KB Output is correct
38 Correct 95 ms 31596 KB Output is correct
39 Correct 96 ms 31592 KB Output is correct
40 Correct 103 ms 31608 KB Output is correct
41 Correct 120 ms 31604 KB Output is correct
42 Correct 114 ms 31600 KB Output is correct
43 Correct 90 ms 31564 KB Output is correct
44 Correct 0 ms 212 KB Output is correct
45 Correct 0 ms 212 KB Output is correct
46 Correct 1 ms 212 KB Output is correct
47 Correct 1 ms 296 KB Output is correct
48 Correct 0 ms 340 KB Output is correct
49 Correct 1 ms 300 KB Output is correct
50 Correct 0 ms 300 KB Output is correct
51 Correct 1 ms 212 KB Output is correct
52 Correct 1 ms 212 KB Output is correct
53 Correct 1 ms 212 KB Output is correct
54 Correct 1 ms 212 KB Output is correct
55 Correct 1 ms 212 KB Output is correct
56 Correct 1 ms 256 KB Output is correct
57 Correct 1 ms 212 KB Output is correct
58 Correct 1 ms 212 KB Output is correct
59 Correct 1 ms 296 KB Output is correct
60 Correct 1 ms 212 KB Output is correct
61 Correct 0 ms 212 KB Output is correct
62 Correct 1 ms 212 KB Output is correct
63 Correct 1 ms 212 KB Output is correct
64 Correct 93 ms 38244 KB Output is correct
65 Correct 94 ms 38248 KB Output is correct
66 Correct 110 ms 38236 KB Output is correct
67 Correct 109 ms 38232 KB Output is correct
68 Correct 10 ms 4012 KB Output is correct
69 Correct 10 ms 3924 KB Output is correct
70 Correct 10 ms 3924 KB Output is correct
71 Correct 9 ms 4020 KB Output is correct
72 Correct 10 ms 4008 KB Output is correct
73 Correct 10 ms 4008 KB Output is correct
74 Correct 94 ms 40524 KB Output is correct
75 Correct 96 ms 40528 KB Output is correct
76 Correct 94 ms 38340 KB Output is correct
77 Correct 93 ms 38252 KB Output is correct
78 Correct 90 ms 38216 KB Output is correct
79 Correct 93 ms 38244 KB Output is correct
80 Correct 85 ms 38240 KB Output is correct
81 Correct 90 ms 38232 KB Output is correct
82 Correct 94 ms 38236 KB Output is correct
83 Correct 97 ms 38236 KB Output is correct
84 Correct 97 ms 38380 KB Output is correct
85 Correct 98 ms 38228 KB Output is correct
86 Correct 98 ms 38236 KB Output is correct
87 Correct 95 ms 38220 KB Output is correct
88 Correct 95 ms 38340 KB Output is correct
89 Correct 101 ms 38240 KB Output is correct
90 Correct 101 ms 38236 KB Output is correct
91 Correct 100 ms 38348 KB Output is correct
92 Correct 106 ms 38248 KB Output is correct