답안 #390729

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
390729 2021-04-16T19:34:19 Z alireza_kaviani 고대 책들 (IOI17_books) C++11
100 / 100
489 ms 92228 KB
#include <bits/stdc++.h>
#include "books.h"
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

#define sep			' '
#define X			first
#define Y			second
#define all(x)		(x).begin() , (x).end()
#define SZ(x)		int(x.size())

const int MAXN = 1e6 + 10;

int n , mark[MAXN] , mx[MAXN] , mn[MAXN] , par[MAXN] , sz[MAXN];
ll ans , ps[MAXN];
vector<int> comp[MAXN];

int Find(int v){
	return (par[v] == -1 ? v : par[v] = Find(par[v]));
}

void Union(int v , int u){
	v = Find(v); u = Find(u);
	if(v == u)	return;
	if(sz[v] < sz[u])	swap(v , u);
	par[u] = v;
	sz[v] += sz[u];
	mx[v] = max(mx[v] , mx[u]);
	mn[v] = min(mn[v] , mn[u]);
}

int count(int l , int r , int x){
	int ans = 0;
	for(int i = l ; i < r ; i++){
		ans += (ps[i] == x);
	}
	return ans;
}

ll minimum_walk(vector<int> p, int s) {
	fill(mx , mx + MAXN , -1);
	fill(mn , mn + MAXN , MAXN);
	fill(par , par + MAXN , -1);
	fill(sz , sz + MAXN , 1);
	n = p.size();
	vector<pii> vec;
	for(int i = 0 ; i < n ; i++){
		ans += abs(i - p[i]);
		if(mark[i])	continue;
		int x = i , flag = 0;
		vector<int> v;
		while(!mark[x]){
			mark[x] = 1; mn[x] = mx[x] = x;
			v.push_back(x);
			x = p[x];
		}
		sort(all(v));
		for(int j = 0 ; j + 1 < SZ(v) ; j++){
			Union(v[j] , v[j + 1]);
			vec.push_back({v[j] , v[j + 1]});
		}
	}
	vector<int> stk;
	sort(all(vec));
	for(int i = 0 ; i < SZ(vec) ; i++){
		while(SZ(stk) > 0 && vec[i].Y > vec[stk.back()].Y){
			if(vec[i].X < vec[stk.back()].Y)	Union(vec[i].X , vec[stk.back()].X);
			stk.pop_back();
		}
		stk.push_back(i);
	}
	vector<int> v;
	for(int i = 0 ; i < n ; i++){
		comp[Find(i)].push_back(i);
		if(Find(i) == i){
			ps[mn[i]]++; ps[mx[i]]--;
			if(mn[i] <= s && s <= mx[i]){
				v.push_back(i);
			}
		}
	}
	partial_sum(ps , ps + MAXN , ps);
	int l = 0 , r = n;
	while(l < s && ps[l] == 0)	l++;
	while(s <= r && ps[r] == 0)	r--;
	ans += 2 * count(l , r + 1 , 0);
	for(int i = 0 ; i + 1 < SZ(v) ; i++){
		int l2 = mn[v[i + 1]] , r2 = mx[v[i + 1]];
		int l1 = (*prev(lower_bound(all(comp[v[i]]) , l2)));
		int r1 = (*lower_bound(all(comp[v[i]]) , r2));
//		cout << l1 << sep << l2 << sep << r1 << sep << r2 << endl;
		ans += min(count(l1 , l2 , i + 1) , count(r2 , r1 , i + 1)) * 2;
	}
	return ans;
}

Compilation message

books.cpp: In function 'll minimum_walk(std::vector<int>, int)':
books.cpp:52:15: warning: unused variable 'flag' [-Wunused-variable]
   52 |   int x = i , flag = 0;
      |               ^~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 28 ms 47180 KB Output is correct
2 Correct 29 ms 47276 KB Output is correct
3 Correct 28 ms 47268 KB Output is correct
4 Correct 28 ms 47220 KB Output is correct
5 Correct 28 ms 47168 KB Output is correct
6 Correct 28 ms 47284 KB Output is correct
7 Correct 31 ms 47300 KB Output is correct
8 Correct 28 ms 47216 KB Output is correct
9 Correct 28 ms 47180 KB Output is correct
10 Correct 30 ms 47208 KB Output is correct
11 Correct 28 ms 47180 KB Output is correct
12 Correct 29 ms 47232 KB Output is correct
13 Correct 31 ms 47232 KB Output is correct
14 Correct 31 ms 47204 KB Output is correct
15 Correct 32 ms 47244 KB Output is correct
16 Correct 28 ms 47284 KB Output is correct
17 Correct 29 ms 47264 KB Output is correct
18 Correct 31 ms 47288 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 28 ms 47180 KB Output is correct
2 Correct 29 ms 47276 KB Output is correct
3 Correct 28 ms 47268 KB Output is correct
4 Correct 28 ms 47220 KB Output is correct
5 Correct 28 ms 47168 KB Output is correct
6 Correct 28 ms 47284 KB Output is correct
7 Correct 31 ms 47300 KB Output is correct
8 Correct 28 ms 47216 KB Output is correct
9 Correct 28 ms 47180 KB Output is correct
10 Correct 30 ms 47208 KB Output is correct
11 Correct 28 ms 47180 KB Output is correct
12 Correct 29 ms 47232 KB Output is correct
13 Correct 31 ms 47232 KB Output is correct
14 Correct 31 ms 47204 KB Output is correct
15 Correct 32 ms 47244 KB Output is correct
16 Correct 28 ms 47284 KB Output is correct
17 Correct 29 ms 47264 KB Output is correct
18 Correct 31 ms 47288 KB Output is correct
19 Correct 28 ms 47196 KB Output is correct
20 Correct 29 ms 47280 KB Output is correct
21 Correct 31 ms 47240 KB Output is correct
22 Correct 28 ms 47240 KB Output is correct
23 Correct 46 ms 47224 KB Output is correct
24 Correct 58 ms 47196 KB Output is correct
25 Correct 29 ms 47300 KB Output is correct
26 Correct 55 ms 47172 KB Output is correct
27 Correct 28 ms 47280 KB Output is correct
28 Correct 30 ms 47268 KB Output is correct
29 Correct 28 ms 47316 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 28 ms 47180 KB Output is correct
2 Correct 29 ms 47276 KB Output is correct
3 Correct 28 ms 47268 KB Output is correct
4 Correct 28 ms 47220 KB Output is correct
5 Correct 28 ms 47168 KB Output is correct
6 Correct 28 ms 47284 KB Output is correct
7 Correct 31 ms 47300 KB Output is correct
8 Correct 28 ms 47216 KB Output is correct
9 Correct 28 ms 47180 KB Output is correct
10 Correct 30 ms 47208 KB Output is correct
11 Correct 28 ms 47180 KB Output is correct
12 Correct 29 ms 47232 KB Output is correct
13 Correct 31 ms 47232 KB Output is correct
14 Correct 31 ms 47204 KB Output is correct
15 Correct 32 ms 47244 KB Output is correct
16 Correct 28 ms 47284 KB Output is correct
17 Correct 29 ms 47264 KB Output is correct
18 Correct 31 ms 47288 KB Output is correct
19 Correct 28 ms 47196 KB Output is correct
20 Correct 29 ms 47280 KB Output is correct
21 Correct 31 ms 47240 KB Output is correct
22 Correct 28 ms 47240 KB Output is correct
23 Correct 46 ms 47224 KB Output is correct
24 Correct 58 ms 47196 KB Output is correct
25 Correct 29 ms 47300 KB Output is correct
26 Correct 55 ms 47172 KB Output is correct
27 Correct 28 ms 47280 KB Output is correct
28 Correct 30 ms 47268 KB Output is correct
29 Correct 28 ms 47316 KB Output is correct
30 Correct 489 ms 76376 KB Output is correct
31 Correct 481 ms 76684 KB Output is correct
32 Correct 243 ms 92228 KB Output is correct
33 Correct 298 ms 84940 KB Output is correct
34 Correct 275 ms 84912 KB Output is correct
35 Correct 279 ms 81680 KB Output is correct
36 Correct 277 ms 76204 KB Output is correct
37 Correct 316 ms 74312 KB Output is correct
38 Correct 334 ms 73960 KB Output is correct
39 Correct 306 ms 73788 KB Output is correct
40 Correct 319 ms 73472 KB Output is correct
41 Correct 381 ms 73660 KB Output is correct
42 Correct 383 ms 74016 KB Output is correct
43 Correct 326 ms 86120 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 33 ms 47184 KB Output is correct
2 Correct 28 ms 47308 KB Output is correct
3 Correct 29 ms 47308 KB Output is correct
4 Correct 28 ms 47232 KB Output is correct
5 Correct 29 ms 47228 KB Output is correct
6 Correct 30 ms 47308 KB Output is correct
7 Correct 28 ms 47308 KB Output is correct
8 Correct 28 ms 47264 KB Output is correct
9 Correct 31 ms 47176 KB Output is correct
10 Correct 33 ms 47204 KB Output is correct
11 Correct 40 ms 47244 KB Output is correct
12 Correct 42 ms 47288 KB Output is correct
13 Correct 29 ms 47184 KB Output is correct
14 Correct 30 ms 47280 KB Output is correct
15 Correct 29 ms 47232 KB Output is correct
16 Correct 28 ms 47216 KB Output is correct
17 Correct 29 ms 47272 KB Output is correct
18 Correct 40 ms 47172 KB Output is correct
19 Correct 28 ms 47288 KB Output is correct
20 Correct 27 ms 47280 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 28 ms 47180 KB Output is correct
2 Correct 29 ms 47276 KB Output is correct
3 Correct 28 ms 47268 KB Output is correct
4 Correct 28 ms 47220 KB Output is correct
5 Correct 28 ms 47168 KB Output is correct
6 Correct 28 ms 47284 KB Output is correct
7 Correct 31 ms 47300 KB Output is correct
8 Correct 28 ms 47216 KB Output is correct
9 Correct 28 ms 47180 KB Output is correct
10 Correct 30 ms 47208 KB Output is correct
11 Correct 28 ms 47180 KB Output is correct
12 Correct 29 ms 47232 KB Output is correct
13 Correct 31 ms 47232 KB Output is correct
14 Correct 31 ms 47204 KB Output is correct
15 Correct 32 ms 47244 KB Output is correct
16 Correct 28 ms 47284 KB Output is correct
17 Correct 29 ms 47264 KB Output is correct
18 Correct 31 ms 47288 KB Output is correct
19 Correct 28 ms 47196 KB Output is correct
20 Correct 29 ms 47280 KB Output is correct
21 Correct 31 ms 47240 KB Output is correct
22 Correct 28 ms 47240 KB Output is correct
23 Correct 46 ms 47224 KB Output is correct
24 Correct 58 ms 47196 KB Output is correct
25 Correct 29 ms 47300 KB Output is correct
26 Correct 55 ms 47172 KB Output is correct
27 Correct 28 ms 47280 KB Output is correct
28 Correct 30 ms 47268 KB Output is correct
29 Correct 28 ms 47316 KB Output is correct
30 Correct 489 ms 76376 KB Output is correct
31 Correct 481 ms 76684 KB Output is correct
32 Correct 243 ms 92228 KB Output is correct
33 Correct 298 ms 84940 KB Output is correct
34 Correct 275 ms 84912 KB Output is correct
35 Correct 279 ms 81680 KB Output is correct
36 Correct 277 ms 76204 KB Output is correct
37 Correct 316 ms 74312 KB Output is correct
38 Correct 334 ms 73960 KB Output is correct
39 Correct 306 ms 73788 KB Output is correct
40 Correct 319 ms 73472 KB Output is correct
41 Correct 381 ms 73660 KB Output is correct
42 Correct 383 ms 74016 KB Output is correct
43 Correct 326 ms 86120 KB Output is correct
44 Correct 33 ms 47184 KB Output is correct
45 Correct 28 ms 47308 KB Output is correct
46 Correct 29 ms 47308 KB Output is correct
47 Correct 28 ms 47232 KB Output is correct
48 Correct 29 ms 47228 KB Output is correct
49 Correct 30 ms 47308 KB Output is correct
50 Correct 28 ms 47308 KB Output is correct
51 Correct 28 ms 47264 KB Output is correct
52 Correct 31 ms 47176 KB Output is correct
53 Correct 33 ms 47204 KB Output is correct
54 Correct 40 ms 47244 KB Output is correct
55 Correct 42 ms 47288 KB Output is correct
56 Correct 29 ms 47184 KB Output is correct
57 Correct 30 ms 47280 KB Output is correct
58 Correct 29 ms 47232 KB Output is correct
59 Correct 28 ms 47216 KB Output is correct
60 Correct 29 ms 47272 KB Output is correct
61 Correct 40 ms 47172 KB Output is correct
62 Correct 28 ms 47288 KB Output is correct
63 Correct 27 ms 47280 KB Output is correct
64 Correct 290 ms 84740 KB Output is correct
65 Correct 306 ms 86368 KB Output is correct
66 Correct 299 ms 72460 KB Output is correct
67 Correct 309 ms 72088 KB Output is correct
68 Correct 56 ms 50516 KB Output is correct
69 Correct 54 ms 50324 KB Output is correct
70 Correct 54 ms 50628 KB Output is correct
71 Correct 54 ms 51248 KB Output is correct
72 Correct 52 ms 51680 KB Output is correct
73 Correct 55 ms 50228 KB Output is correct
74 Correct 273 ms 83328 KB Output is correct
75 Correct 270 ms 83444 KB Output is correct
76 Correct 297 ms 84304 KB Output is correct
77 Correct 307 ms 84376 KB Output is correct
78 Correct 314 ms 81848 KB Output is correct
79 Correct 288 ms 81868 KB Output is correct
80 Correct 265 ms 90596 KB Output is correct
81 Correct 313 ms 81388 KB Output is correct
82 Correct 280 ms 81996 KB Output is correct
83 Correct 369 ms 76868 KB Output is correct
84 Correct 286 ms 76012 KB Output is correct
85 Correct 305 ms 72556 KB Output is correct
86 Correct 320 ms 72712 KB Output is correct
87 Correct 260 ms 88656 KB Output is correct
88 Correct 289 ms 84860 KB Output is correct
89 Correct 293 ms 78780 KB Output is correct
90 Correct 296 ms 73628 KB Output is correct
91 Correct 312 ms 71556 KB Output is correct
92 Correct 337 ms 72140 KB Output is correct