답안 #391007

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
391007 2021-04-17T14:55:26 Z alishahali1382 고대 책들 (IOI17_books) C++14
70 / 100
2000 ms 27092 KB
#include "books.h"
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
#define debug(x) {cerr<<#x<<"="<<x<<"\n";}
#define debug2(x, y) {cerr<<#x<<", "<<#y<<" = "<<x<<", "<<y<<"\n";}
#define debugp(p) {cerr<<#p<<"={"<<p.first<<", "<<p.second<<"}\n";}
#define pb push_back
#define all(x) x.begin(), x.end()

const int inf=1000000100;
const int MAXN=1000010;

ll ans; // :)
int n, m, k, s, L, R;
int P[MAXN], ps[MAXN];
int mn[MAXN], mx[MAXN];

pii fix(int l, int r){
	int ll=min({l, mn[l], mn[r]}), rr=max({r, mx[l], mx[r]});
	while (ll<l || r<rr){
		if (ll<l){
			l--;
			ll=min(ll, mn[l]);
			rr=max(rr, mx[l]);
		}
		if (r<rr){
			r++;
			ll=min(ll, mn[r]);
			rr=max(rr, mx[r]);
		}
	}
	return {l, r};
}

ll minimum_walk(vector<int> _p, int s){
	n=_p.size();
	for (int i=0; i<n; i++){
		P[i]=_p[i];
		ans+=abs(i-P[i]);
		ps[min(i, P[i])]++;
		ps[max(i, P[i])]--;
	}
	if (!ans) return 0;
	L=0;
	R=n-1;
	while (P[L]==L) L++;
	while (P[R]==R) R--;
	for (int i=1; i<=R; i++) ps[i]+=ps[i-1];
	if (s<=L || R<=s){
		for (int i=L; i+1<=R; i++) if (!ps[i]) ans+=2;
		if (s<L) ans+=2*(L-s);
		if (R<s) ans+=2*(s-R);
		return ans;
	}
	for (int i=L; i<R; i++){
		vector<int> vec;
		int v=i;
		while (1){
			vec.pb(v);
			v=P[v];
			if (v==i) break ;
		}
		mn[i]=mx[i]=i;
		for (int v:vec){
			mn[i]=min(mn[i], v);
			mx[i]=max(mx[i], v);
		}
		for (int v:vec){
			mn[v]=mn[i];
			mx[v]=mx[i];
		}
	}
	// debug2(mn[3], mx[3])
	pii p=fix(s, s);
	int tl=p.first, tr=p.second;
	for (int i=L; i<tl; i++) if (!ps[i]){
		ans+=2;
		L=i+1;
	}
	for (int i=R-1; i>=tr; i--) if (!ps[i]){
		ans+=2;
		R=i;
	}
	// debug(ans)
	// debug2(L, R)
	while (tr-tl!=R-L){
		// pii p=fix(tl, tr);
		// tl=p.first;
		// tr=p.second;
		// debug2(tl, tr)
		int l=tl, r=tr;
		int cl=0, cr=0;
		while (L<l){
			cl+=2;
			pii p=fix(--l, tr);
			l=p.first;
			if (tr<p.second) break ;
		}
		while (r<R){
			cr+=2;
			pii p=fix(tl, ++r);
			r=p.second;
			if (p.first<tl) break ;
		}
		// debug2(l, r)
		// debug2(cl, cr)
		// cerr<<"\n";
		ans+=min(cl, cr);
		pii p=fix(l, r);
		tl=p.first;
		tr=p.second;
	}
	// debugp(fix(1, 2))

	return ans;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 208 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 0 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
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 208 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 0 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 332 KB Output is correct
20 Correct 1 ms 332 KB Output is correct
21 Correct 1 ms 332 KB Output is correct
22 Correct 1 ms 204 KB Output is correct
23 Correct 1 ms 332 KB Output is correct
24 Correct 1 ms 204 KB Output is correct
25 Correct 1 ms 332 KB Output is correct
26 Correct 1 ms 332 KB Output is correct
27 Correct 1 ms 332 KB Output is correct
28 Correct 1 ms 332 KB Output is correct
29 Correct 1 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 208 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 0 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 332 KB Output is correct
20 Correct 1 ms 332 KB Output is correct
21 Correct 1 ms 332 KB Output is correct
22 Correct 1 ms 204 KB Output is correct
23 Correct 1 ms 332 KB Output is correct
24 Correct 1 ms 204 KB Output is correct
25 Correct 1 ms 332 KB Output is correct
26 Correct 1 ms 332 KB Output is correct
27 Correct 1 ms 332 KB Output is correct
28 Correct 1 ms 332 KB Output is correct
29 Correct 1 ms 332 KB Output is correct
30 Correct 156 ms 15860 KB Output is correct
31 Correct 145 ms 15872 KB Output is correct
32 Correct 134 ms 15872 KB Output is correct
33 Correct 151 ms 15876 KB Output is correct
34 Correct 142 ms 15856 KB Output is correct
35 Correct 144 ms 15940 KB Output is correct
36 Correct 139 ms 15872 KB Output is correct
37 Correct 144 ms 15860 KB Output is correct
38 Correct 140 ms 15940 KB Output is correct
39 Correct 138 ms 15856 KB Output is correct
40 Correct 139 ms 15856 KB Output is correct
41 Correct 146 ms 15936 KB Output is correct
42 Correct 143 ms 15872 KB Output is correct
43 Correct 140 ms 15956 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 308 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 308 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 332 KB Output is correct
18 Correct 1 ms 308 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 1 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 208 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 0 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 332 KB Output is correct
20 Correct 1 ms 332 KB Output is correct
21 Correct 1 ms 332 KB Output is correct
22 Correct 1 ms 204 KB Output is correct
23 Correct 1 ms 332 KB Output is correct
24 Correct 1 ms 204 KB Output is correct
25 Correct 1 ms 332 KB Output is correct
26 Correct 1 ms 332 KB Output is correct
27 Correct 1 ms 332 KB Output is correct
28 Correct 1 ms 332 KB Output is correct
29 Correct 1 ms 332 KB Output is correct
30 Correct 156 ms 15860 KB Output is correct
31 Correct 145 ms 15872 KB Output is correct
32 Correct 134 ms 15872 KB Output is correct
33 Correct 151 ms 15876 KB Output is correct
34 Correct 142 ms 15856 KB Output is correct
35 Correct 144 ms 15940 KB Output is correct
36 Correct 139 ms 15872 KB Output is correct
37 Correct 144 ms 15860 KB Output is correct
38 Correct 140 ms 15940 KB Output is correct
39 Correct 138 ms 15856 KB Output is correct
40 Correct 139 ms 15856 KB Output is correct
41 Correct 146 ms 15936 KB Output is correct
42 Correct 143 ms 15872 KB Output is correct
43 Correct 140 ms 15956 KB Output is correct
44 Correct 1 ms 332 KB Output is correct
45 Correct 1 ms 332 KB Output is correct
46 Correct 1 ms 332 KB Output is correct
47 Correct 1 ms 332 KB Output is correct
48 Correct 1 ms 308 KB Output is correct
49 Correct 1 ms 332 KB Output is correct
50 Correct 1 ms 308 KB Output is correct
51 Correct 1 ms 332 KB Output is correct
52 Correct 1 ms 332 KB Output is correct
53 Correct 1 ms 332 KB Output is correct
54 Correct 1 ms 332 KB Output is correct
55 Correct 1 ms 332 KB Output is correct
56 Correct 1 ms 332 KB Output is correct
57 Correct 1 ms 332 KB Output is correct
58 Correct 1 ms 332 KB Output is correct
59 Correct 1 ms 332 KB Output is correct
60 Correct 1 ms 332 KB Output is correct
61 Correct 1 ms 308 KB Output is correct
62 Correct 1 ms 332 KB Output is correct
63 Correct 1 ms 332 KB Output is correct
64 Correct 222 ms 27092 KB Output is correct
65 Correct 200 ms 27076 KB Output is correct
66 Execution timed out 2070 ms 24124 KB Time limit exceeded
67 Halted 0 ms 0 KB -