답안 #338227

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
338227 2020-12-22T18:53:36 Z pit4h 고대 책들 (IOI17_books) C++14
100 / 100
295 ms 34780 KB
#include "books.h"
#include "bits/stdc++.h"
#define st first
#define nd second
#define mp make_pair
using namespace std;
using ll = long long;
using pii = pair<int, int>;
const int N = 1e6+1;
struct DSU {
	int f[N], cnt[N], l[N], r[N];
	DSU() {
		for(int i=0; i<N; ++i) {
			f[i] = l[i] = r[i] = i;
			cnt[i] = 1;
		}
	}
	int Find(int x) {
		if(f[x]==x) return x;
		f[x] = Find(f[x]);
		return f[x];
	}
	void Union(int x, int y) {
		x = Find(x), y = Find(y);
		if(x==y) return;
		if(cnt[x] < cnt[y]) swap(x, y);
		cnt[x] += cnt[y];
		l[x] = min(l[x], l[y]);
		r[x] = max(r[x], r[y]);
		f[y] = x;
	}
	int getl(int x) { return l[Find(x)]; }
	int getr(int x) { return r[Find(x)]; }
};
DSU dsu;

ll minimum_walk(vector<int> p, int s) {
	int n = p.size();
	ll ans = 0;
	for(int i=0; i<n; ++i) {
		ans += abs(p[i] - i);
		dsu.Union(i, p[i]);
	}
	//cerr<<ans<<'\n';

	vector<int> vec;
	for(int i=n-1; i>=0; --i) {
		while(vec.size() && dsu.getr(i) >= vec.back()) {
			if(dsu.getl(vec.back()) < i) {
				dsu.Union(i, vec.back());
			}
			vec.pop_back();
		}
		vec.push_back(i);
	}
	
	int le=0, ri=n-1;
	while(le<s && dsu.getl(le)==dsu.getr(le)) {
		le++;
	}
	while(ri>s && dsu.getl(ri)==dsu.getr(ri)) {
		ri--;
	}
	
	while(true) {
		int l=le, r=ri, cur=-1;
		int cost1=0, cost2=0;
		for(int i=dsu.getl(s)-1; i>=le; --i) {
			if(dsu.getr(i) > s) {
				l = i;	
				cur = dsu.Find(i);
				break;
			}
		}
		for(int i=dsu.getr(s)+1; i<=ri; ++i) {
			if(dsu.getl(i) < s) {
				r = i;
				break;
			}
		}
		if(cur!=-1) l++, cost1++;
		for(int i=l; i<dsu.getl(s); ++i) {
			cost1++;
			i = dsu.getr(i);
		}
		if(cur!=-1) r--, cost2++;
		for(int i=r; i>dsu.getr(s); --i) {
			cost2++;
			i = dsu.getl(i);
		}
		if(cur==-1) {
			ans += (cost1+cost2)*2;	
			break;
		}
		ans += min(cost1, cost2)*2;
		dsu.Union(cur, s);
	}
	return ans;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 15980 KB Output is correct
2 Correct 10 ms 15980 KB Output is correct
3 Correct 10 ms 15980 KB Output is correct
4 Correct 11 ms 16000 KB Output is correct
5 Correct 10 ms 15980 KB Output is correct
6 Correct 11 ms 16128 KB Output is correct
7 Correct 10 ms 15980 KB Output is correct
8 Correct 10 ms 15980 KB Output is correct
9 Correct 10 ms 15980 KB Output is correct
10 Correct 10 ms 15980 KB Output is correct
11 Correct 11 ms 15980 KB Output is correct
12 Correct 10 ms 15980 KB Output is correct
13 Correct 10 ms 15980 KB Output is correct
14 Correct 10 ms 15980 KB Output is correct
15 Correct 10 ms 15980 KB Output is correct
16 Correct 12 ms 15980 KB Output is correct
17 Correct 10 ms 15980 KB Output is correct
18 Correct 10 ms 15980 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 15980 KB Output is correct
2 Correct 10 ms 15980 KB Output is correct
3 Correct 10 ms 15980 KB Output is correct
4 Correct 11 ms 16000 KB Output is correct
5 Correct 10 ms 15980 KB Output is correct
6 Correct 11 ms 16128 KB Output is correct
7 Correct 10 ms 15980 KB Output is correct
8 Correct 10 ms 15980 KB Output is correct
9 Correct 10 ms 15980 KB Output is correct
10 Correct 10 ms 15980 KB Output is correct
11 Correct 11 ms 15980 KB Output is correct
12 Correct 10 ms 15980 KB Output is correct
13 Correct 10 ms 15980 KB Output is correct
14 Correct 10 ms 15980 KB Output is correct
15 Correct 10 ms 15980 KB Output is correct
16 Correct 12 ms 15980 KB Output is correct
17 Correct 10 ms 15980 KB Output is correct
18 Correct 10 ms 15980 KB Output is correct
19 Correct 11 ms 15980 KB Output is correct
20 Correct 11 ms 15980 KB Output is correct
21 Correct 11 ms 15980 KB Output is correct
22 Correct 10 ms 15980 KB Output is correct
23 Correct 10 ms 15980 KB Output is correct
24 Correct 10 ms 15980 KB Output is correct
25 Correct 11 ms 16108 KB Output is correct
26 Correct 11 ms 15980 KB Output is correct
27 Correct 11 ms 16000 KB Output is correct
28 Correct 10 ms 15980 KB Output is correct
29 Correct 10 ms 15980 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 15980 KB Output is correct
2 Correct 10 ms 15980 KB Output is correct
3 Correct 10 ms 15980 KB Output is correct
4 Correct 11 ms 16000 KB Output is correct
5 Correct 10 ms 15980 KB Output is correct
6 Correct 11 ms 16128 KB Output is correct
7 Correct 10 ms 15980 KB Output is correct
8 Correct 10 ms 15980 KB Output is correct
9 Correct 10 ms 15980 KB Output is correct
10 Correct 10 ms 15980 KB Output is correct
11 Correct 11 ms 15980 KB Output is correct
12 Correct 10 ms 15980 KB Output is correct
13 Correct 10 ms 15980 KB Output is correct
14 Correct 10 ms 15980 KB Output is correct
15 Correct 10 ms 15980 KB Output is correct
16 Correct 12 ms 15980 KB Output is correct
17 Correct 10 ms 15980 KB Output is correct
18 Correct 10 ms 15980 KB Output is correct
19 Correct 11 ms 15980 KB Output is correct
20 Correct 11 ms 15980 KB Output is correct
21 Correct 11 ms 15980 KB Output is correct
22 Correct 10 ms 15980 KB Output is correct
23 Correct 10 ms 15980 KB Output is correct
24 Correct 10 ms 15980 KB Output is correct
25 Correct 11 ms 16108 KB Output is correct
26 Correct 11 ms 15980 KB Output is correct
27 Correct 11 ms 16000 KB Output is correct
28 Correct 10 ms 15980 KB Output is correct
29 Correct 10 ms 15980 KB Output is correct
30 Correct 291 ms 23796 KB Output is correct
31 Correct 295 ms 23832 KB Output is correct
32 Correct 145 ms 27996 KB Output is correct
33 Correct 159 ms 27996 KB Output is correct
34 Correct 166 ms 27996 KB Output is correct
35 Correct 167 ms 25952 KB Output is correct
36 Correct 170 ms 24932 KB Output is correct
37 Correct 173 ms 23936 KB Output is correct
38 Correct 180 ms 23788 KB Output is correct
39 Correct 175 ms 23788 KB Output is correct
40 Correct 184 ms 23916 KB Output is correct
41 Correct 226 ms 23784 KB Output is correct
42 Correct 233 ms 23916 KB Output is correct
43 Correct 182 ms 23912 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 15980 KB Output is correct
2 Correct 11 ms 15980 KB Output is correct
3 Correct 11 ms 15980 KB Output is correct
4 Correct 11 ms 15980 KB Output is correct
5 Correct 11 ms 15980 KB Output is correct
6 Correct 11 ms 15980 KB Output is correct
7 Correct 11 ms 15980 KB Output is correct
8 Correct 11 ms 15980 KB Output is correct
9 Correct 11 ms 15980 KB Output is correct
10 Correct 11 ms 15980 KB Output is correct
11 Correct 11 ms 15980 KB Output is correct
12 Correct 11 ms 15980 KB Output is correct
13 Correct 11 ms 15980 KB Output is correct
14 Correct 12 ms 15980 KB Output is correct
15 Correct 11 ms 15980 KB Output is correct
16 Correct 11 ms 15980 KB Output is correct
17 Correct 11 ms 15980 KB Output is correct
18 Correct 11 ms 15980 KB Output is correct
19 Correct 11 ms 15980 KB Output is correct
20 Correct 11 ms 15980 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 15980 KB Output is correct
2 Correct 10 ms 15980 KB Output is correct
3 Correct 10 ms 15980 KB Output is correct
4 Correct 11 ms 16000 KB Output is correct
5 Correct 10 ms 15980 KB Output is correct
6 Correct 11 ms 16128 KB Output is correct
7 Correct 10 ms 15980 KB Output is correct
8 Correct 10 ms 15980 KB Output is correct
9 Correct 10 ms 15980 KB Output is correct
10 Correct 10 ms 15980 KB Output is correct
11 Correct 11 ms 15980 KB Output is correct
12 Correct 10 ms 15980 KB Output is correct
13 Correct 10 ms 15980 KB Output is correct
14 Correct 10 ms 15980 KB Output is correct
15 Correct 10 ms 15980 KB Output is correct
16 Correct 12 ms 15980 KB Output is correct
17 Correct 10 ms 15980 KB Output is correct
18 Correct 10 ms 15980 KB Output is correct
19 Correct 11 ms 15980 KB Output is correct
20 Correct 11 ms 15980 KB Output is correct
21 Correct 11 ms 15980 KB Output is correct
22 Correct 10 ms 15980 KB Output is correct
23 Correct 10 ms 15980 KB Output is correct
24 Correct 10 ms 15980 KB Output is correct
25 Correct 11 ms 16108 KB Output is correct
26 Correct 11 ms 15980 KB Output is correct
27 Correct 11 ms 16000 KB Output is correct
28 Correct 10 ms 15980 KB Output is correct
29 Correct 10 ms 15980 KB Output is correct
30 Correct 291 ms 23796 KB Output is correct
31 Correct 295 ms 23832 KB Output is correct
32 Correct 145 ms 27996 KB Output is correct
33 Correct 159 ms 27996 KB Output is correct
34 Correct 166 ms 27996 KB Output is correct
35 Correct 167 ms 25952 KB Output is correct
36 Correct 170 ms 24932 KB Output is correct
37 Correct 173 ms 23936 KB Output is correct
38 Correct 180 ms 23788 KB Output is correct
39 Correct 175 ms 23788 KB Output is correct
40 Correct 184 ms 23916 KB Output is correct
41 Correct 226 ms 23784 KB Output is correct
42 Correct 233 ms 23916 KB Output is correct
43 Correct 182 ms 23912 KB Output is correct
44 Correct 11 ms 15980 KB Output is correct
45 Correct 11 ms 15980 KB Output is correct
46 Correct 11 ms 15980 KB Output is correct
47 Correct 11 ms 15980 KB Output is correct
48 Correct 11 ms 15980 KB Output is correct
49 Correct 11 ms 15980 KB Output is correct
50 Correct 11 ms 15980 KB Output is correct
51 Correct 11 ms 15980 KB Output is correct
52 Correct 11 ms 15980 KB Output is correct
53 Correct 11 ms 15980 KB Output is correct
54 Correct 11 ms 15980 KB Output is correct
55 Correct 11 ms 15980 KB Output is correct
56 Correct 11 ms 15980 KB Output is correct
57 Correct 12 ms 15980 KB Output is correct
58 Correct 11 ms 15980 KB Output is correct
59 Correct 11 ms 15980 KB Output is correct
60 Correct 11 ms 15980 KB Output is correct
61 Correct 11 ms 15980 KB Output is correct
62 Correct 11 ms 15980 KB Output is correct
63 Correct 11 ms 15980 KB Output is correct
64 Correct 155 ms 30700 KB Output is correct
65 Correct 163 ms 30824 KB Output is correct
66 Correct 185 ms 30572 KB Output is correct
67 Correct 186 ms 30572 KB Output is correct
68 Correct 26 ms 17388 KB Output is correct
69 Correct 29 ms 17388 KB Output is correct
70 Correct 26 ms 17388 KB Output is correct
71 Correct 26 ms 17388 KB Output is correct
72 Correct 24 ms 17388 KB Output is correct
73 Correct 29 ms 17388 KB Output is correct
74 Correct 162 ms 34776 KB Output is correct
75 Correct 160 ms 34780 KB Output is correct
76 Correct 158 ms 30572 KB Output is correct
77 Correct 153 ms 30572 KB Output is correct
78 Correct 166 ms 30572 KB Output is correct
79 Correct 164 ms 30572 KB Output is correct
80 Correct 141 ms 34780 KB Output is correct
81 Correct 155 ms 32864 KB Output is correct
82 Correct 155 ms 32736 KB Output is correct
83 Correct 172 ms 31208 KB Output is correct
84 Correct 177 ms 31272 KB Output is correct
85 Correct 179 ms 30824 KB Output is correct
86 Correct 178 ms 30588 KB Output is correct
87 Correct 158 ms 32736 KB Output is correct
88 Correct 160 ms 32736 KB Output is correct
89 Correct 168 ms 31716 KB Output is correct
90 Correct 175 ms 30568 KB Output is correct
91 Correct 182 ms 30700 KB Output is correct
92 Correct 181 ms 30572 KB Output is correct