답안 #41013

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
41013 2018-02-11T12:55:52 Z alenam0161 고대 책들 (IOI17_books) C++14
100 / 100
334 ms 178924 KB
#include "books.h"
#include <vector>
#include <algorithm>
#include <stack>
#include <iostream>
using namespace std;
const int N = 3e6 + 7;
int group[N];
int mx[N];
bool used[N];
int par[N],lpar[N],rpar[N];
int fpar(int v) {
	return v == par[v] ? v : par[v] = fpar(par[v]);
}
void merge(int v, int u) {
	v = fpar(v);
	u = fpar(u);
	if (rand() & 1) {
		swap(u, v);
	}
	lpar[v] = min(lpar[v], lpar[u]);
	rpar[v] = max(rpar[v], rpar[u]);
	par[u] = v;
}
long long minimum_walk(std::vector<int> p, int s) {
	long long ans = 0;
	for (int i = 0; i < p.size(); ++i)
		par[i] = lpar[i] = rpar[i] = i;
	for (int i = 0; i < p.size(); ++i) {
		ans += abs(i - p[i]);
		merge(i, p[i]);
	}
	stack<int> st;
	for (int i = 0; i < p.size(); ++i) {
		int P = fpar(i);

		if (i == lpar[P]) {
			st.push(i);
		}
		else {
			while (fpar(i) != fpar(st.top())) {
				merge(i, st.top());
				st.pop();
			}
		}
		if (i == rpar[fpar(i)])st.pop();
	}
	int L = 0, R = p.size() - 1;
	for (int i = p.size() - 1; i > 0; i--) { if (p[i] != i)break; R--; }
	for (int i = 0; i < p.size(); ++i) { if (p[i] != i)break;  L++; }
	int Main = fpar(s);
	int cl = lpar[Main];
	int cr = rpar[Main];
	while (cl > L || cr < R) {
		int howl = 0, ul = cl;
		while (ul > L) {
			ul--;
			howl++;
			int cur = fpar(ul);
			ul = lpar[cur];
			if (rpar[cur] > cr)break;
		}
		int howr = 0, ur = cr;
		while (ur < R) {
			ur++; howr++;
			int cur = fpar(ur);
			ur = rpar[cur];
			if (lpar[cur] < cl)break;
		}
		if (fpar(ul) == fpar(ur)) {
			int x = fpar(ul);
			cl = lpar[x];
			cr = rpar[x];
			ans += min(howl, howr) * 2;
		}
		else {
			cl = ul; cr = ur;
			ans += howl * 2 + howr * 2;
		}
	}

	return ans;
}

Compilation message

books.cpp: In function 'long long int minimum_walk(std::vector<int>, int)':
books.cpp:27:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (int i = 0; i < p.size(); ++i)
                    ^
books.cpp:29:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (int i = 0; i < p.size(); ++i) {
                    ^
books.cpp:34:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (int i = 0; i < p.size(); ++i) {
                    ^
books.cpp:50:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (int i = 0; i < p.size(); ++i) { if (p[i] != i)break;  L++; }
                    ^
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 248 KB Output is correct
2 Correct 2 ms 352 KB Output is correct
3 Correct 1 ms 424 KB Output is correct
4 Correct 2 ms 440 KB Output is correct
5 Correct 2 ms 444 KB Output is correct
6 Correct 2 ms 444 KB Output is correct
7 Correct 2 ms 492 KB Output is correct
8 Correct 2 ms 608 KB Output is correct
9 Correct 1 ms 608 KB Output is correct
10 Correct 2 ms 608 KB Output is correct
11 Correct 2 ms 608 KB Output is correct
12 Correct 1 ms 608 KB Output is correct
13 Correct 1 ms 608 KB Output is correct
14 Correct 2 ms 608 KB Output is correct
15 Correct 1 ms 608 KB Output is correct
16 Correct 1 ms 608 KB Output is correct
17 Correct 1 ms 608 KB Output is correct
18 Correct 1 ms 608 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 248 KB Output is correct
2 Correct 2 ms 352 KB Output is correct
3 Correct 1 ms 424 KB Output is correct
4 Correct 2 ms 440 KB Output is correct
5 Correct 2 ms 444 KB Output is correct
6 Correct 2 ms 444 KB Output is correct
7 Correct 2 ms 492 KB Output is correct
8 Correct 2 ms 608 KB Output is correct
9 Correct 1 ms 608 KB Output is correct
10 Correct 2 ms 608 KB Output is correct
11 Correct 2 ms 608 KB Output is correct
12 Correct 1 ms 608 KB Output is correct
13 Correct 1 ms 608 KB Output is correct
14 Correct 2 ms 608 KB Output is correct
15 Correct 1 ms 608 KB Output is correct
16 Correct 1 ms 608 KB Output is correct
17 Correct 1 ms 608 KB Output is correct
18 Correct 1 ms 608 KB Output is correct
19 Correct 2 ms 608 KB Output is correct
20 Correct 2 ms 608 KB Output is correct
21 Correct 2 ms 608 KB Output is correct
22 Correct 2 ms 608 KB Output is correct
23 Correct 2 ms 608 KB Output is correct
24 Correct 2 ms 608 KB Output is correct
25 Correct 2 ms 608 KB Output is correct
26 Correct 2 ms 608 KB Output is correct
27 Correct 2 ms 608 KB Output is correct
28 Correct 2 ms 608 KB Output is correct
29 Correct 2 ms 608 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 248 KB Output is correct
2 Correct 2 ms 352 KB Output is correct
3 Correct 1 ms 424 KB Output is correct
4 Correct 2 ms 440 KB Output is correct
5 Correct 2 ms 444 KB Output is correct
6 Correct 2 ms 444 KB Output is correct
7 Correct 2 ms 492 KB Output is correct
8 Correct 2 ms 608 KB Output is correct
9 Correct 1 ms 608 KB Output is correct
10 Correct 2 ms 608 KB Output is correct
11 Correct 2 ms 608 KB Output is correct
12 Correct 1 ms 608 KB Output is correct
13 Correct 1 ms 608 KB Output is correct
14 Correct 2 ms 608 KB Output is correct
15 Correct 1 ms 608 KB Output is correct
16 Correct 1 ms 608 KB Output is correct
17 Correct 1 ms 608 KB Output is correct
18 Correct 1 ms 608 KB Output is correct
19 Correct 2 ms 608 KB Output is correct
20 Correct 2 ms 608 KB Output is correct
21 Correct 2 ms 608 KB Output is correct
22 Correct 2 ms 608 KB Output is correct
23 Correct 2 ms 608 KB Output is correct
24 Correct 2 ms 608 KB Output is correct
25 Correct 2 ms 608 KB Output is correct
26 Correct 2 ms 608 KB Output is correct
27 Correct 2 ms 608 KB Output is correct
28 Correct 2 ms 608 KB Output is correct
29 Correct 2 ms 608 KB Output is correct
30 Correct 334 ms 20188 KB Output is correct
31 Correct 323 ms 20188 KB Output is correct
32 Correct 179 ms 20236 KB Output is correct
33 Correct 234 ms 20236 KB Output is correct
34 Correct 191 ms 20236 KB Output is correct
35 Correct 199 ms 20236 KB Output is correct
36 Correct 222 ms 20236 KB Output is correct
37 Correct 224 ms 20236 KB Output is correct
38 Correct 221 ms 20236 KB Output is correct
39 Correct 252 ms 20236 KB Output is correct
40 Correct 290 ms 20236 KB Output is correct
41 Correct 265 ms 20236 KB Output is correct
42 Correct 282 ms 20236 KB Output is correct
43 Correct 203 ms 20236 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 20236 KB Output is correct
2 Correct 2 ms 20236 KB Output is correct
3 Correct 2 ms 20236 KB Output is correct
4 Correct 2 ms 20236 KB Output is correct
5 Correct 2 ms 20236 KB Output is correct
6 Correct 2 ms 20236 KB Output is correct
7 Correct 2 ms 20236 KB Output is correct
8 Correct 2 ms 20236 KB Output is correct
9 Correct 2 ms 20236 KB Output is correct
10 Correct 1 ms 20236 KB Output is correct
11 Correct 2 ms 20236 KB Output is correct
12 Correct 2 ms 20236 KB Output is correct
13 Correct 2 ms 20236 KB Output is correct
14 Correct 2 ms 20236 KB Output is correct
15 Correct 2 ms 20236 KB Output is correct
16 Correct 2 ms 20236 KB Output is correct
17 Correct 2 ms 20236 KB Output is correct
18 Correct 2 ms 20236 KB Output is correct
19 Correct 2 ms 20236 KB Output is correct
20 Correct 2 ms 20236 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 248 KB Output is correct
2 Correct 2 ms 352 KB Output is correct
3 Correct 1 ms 424 KB Output is correct
4 Correct 2 ms 440 KB Output is correct
5 Correct 2 ms 444 KB Output is correct
6 Correct 2 ms 444 KB Output is correct
7 Correct 2 ms 492 KB Output is correct
8 Correct 2 ms 608 KB Output is correct
9 Correct 1 ms 608 KB Output is correct
10 Correct 2 ms 608 KB Output is correct
11 Correct 2 ms 608 KB Output is correct
12 Correct 1 ms 608 KB Output is correct
13 Correct 1 ms 608 KB Output is correct
14 Correct 2 ms 608 KB Output is correct
15 Correct 1 ms 608 KB Output is correct
16 Correct 1 ms 608 KB Output is correct
17 Correct 1 ms 608 KB Output is correct
18 Correct 1 ms 608 KB Output is correct
19 Correct 2 ms 608 KB Output is correct
20 Correct 2 ms 608 KB Output is correct
21 Correct 2 ms 608 KB Output is correct
22 Correct 2 ms 608 KB Output is correct
23 Correct 2 ms 608 KB Output is correct
24 Correct 2 ms 608 KB Output is correct
25 Correct 2 ms 608 KB Output is correct
26 Correct 2 ms 608 KB Output is correct
27 Correct 2 ms 608 KB Output is correct
28 Correct 2 ms 608 KB Output is correct
29 Correct 2 ms 608 KB Output is correct
30 Correct 334 ms 20188 KB Output is correct
31 Correct 323 ms 20188 KB Output is correct
32 Correct 179 ms 20236 KB Output is correct
33 Correct 234 ms 20236 KB Output is correct
34 Correct 191 ms 20236 KB Output is correct
35 Correct 199 ms 20236 KB Output is correct
36 Correct 222 ms 20236 KB Output is correct
37 Correct 224 ms 20236 KB Output is correct
38 Correct 221 ms 20236 KB Output is correct
39 Correct 252 ms 20236 KB Output is correct
40 Correct 290 ms 20236 KB Output is correct
41 Correct 265 ms 20236 KB Output is correct
42 Correct 282 ms 20236 KB Output is correct
43 Correct 203 ms 20236 KB Output is correct
44 Correct 2 ms 20236 KB Output is correct
45 Correct 2 ms 20236 KB Output is correct
46 Correct 2 ms 20236 KB Output is correct
47 Correct 2 ms 20236 KB Output is correct
48 Correct 2 ms 20236 KB Output is correct
49 Correct 2 ms 20236 KB Output is correct
50 Correct 2 ms 20236 KB Output is correct
51 Correct 2 ms 20236 KB Output is correct
52 Correct 2 ms 20236 KB Output is correct
53 Correct 1 ms 20236 KB Output is correct
54 Correct 2 ms 20236 KB Output is correct
55 Correct 2 ms 20236 KB Output is correct
56 Correct 2 ms 20236 KB Output is correct
57 Correct 2 ms 20236 KB Output is correct
58 Correct 2 ms 20236 KB Output is correct
59 Correct 2 ms 20236 KB Output is correct
60 Correct 2 ms 20236 KB Output is correct
61 Correct 2 ms 20236 KB Output is correct
62 Correct 2 ms 20236 KB Output is correct
63 Correct 2 ms 20236 KB Output is correct
64 Correct 181 ms 27080 KB Output is correct
65 Correct 191 ms 33780 KB Output is correct
66 Correct 232 ms 40520 KB Output is correct
67 Correct 236 ms 47376 KB Output is correct
68 Correct 25 ms 47376 KB Output is correct
69 Correct 24 ms 47376 KB Output is correct
70 Correct 22 ms 47376 KB Output is correct
71 Correct 27 ms 47376 KB Output is correct
72 Correct 18 ms 47376 KB Output is correct
73 Correct 36 ms 47376 KB Output is correct
74 Correct 197 ms 57464 KB Output is correct
75 Correct 210 ms 64292 KB Output is correct
76 Correct 189 ms 71148 KB Output is correct
77 Correct 203 ms 77828 KB Output is correct
78 Correct 211 ms 84596 KB Output is correct
79 Correct 226 ms 91252 KB Output is correct
80 Correct 173 ms 97916 KB Output is correct
81 Correct 198 ms 106068 KB Output is correct
82 Correct 276 ms 112828 KB Output is correct
83 Correct 238 ms 118344 KB Output is correct
84 Correct 207 ms 124960 KB Output is correct
85 Correct 207 ms 131764 KB Output is correct
86 Correct 228 ms 138508 KB Output is correct
87 Correct 214 ms 145740 KB Output is correct
88 Correct 205 ms 152396 KB Output is correct
89 Correct 225 ms 158904 KB Output is correct
90 Correct 216 ms 165632 KB Output is correct
91 Correct 238 ms 172192 KB Output is correct
92 Correct 230 ms 178924 KB Output is correct