답안 #72760

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
72760 2018-08-26T22:41:40 Z FLDutchman 고대 책들 (IOI17_books) C++14
100 / 100
414 ms 182948 KB
#include "books.h"
#include <bits/stdc++.h>
using namespace std;

typedef int INT;

#define fst first
#define snd second
#define FOR(i,l,r) for(int i = (l); i < (r); i++)
#define pb push_back
#define int long long

typedef vector<int> vi;
typedef pair<int, int> ii;
typedef vector<ii> vii;

int N;
vi p;
vi color;
vii interval;
vii segments;


void expand(int &l, int &r){
	//cout << "expanding " << l << " " << r << endl; 
	int gl = l-1, gr = r+1;
	while(l != gl || r != gr){
		//cout << l << " " << r << endl;
		//cout << gl << " " << gr << endl;
		if(l != gl) {l--; gl = min(gl, interval[color[l]].fst); gr = max(gr, interval[color[l]].snd);}
		if(r != gr) {r++; gl = min(gl, interval[color[r]].fst); gr = max(gr, interval[color[r]].snd);}
	}
	//cout << l << " " << r << endl;
}

long long minimum_walk(std::vector<INT> P, INT s) {
	FOR(i, 0, P.size()) p.pb(P[i]);
	N = p.size();
	color.assign(N, -1);
	int c = 0;
	FOR(i, 0, N) if(color[i] == -1){
		int j = i;
		int l = i, r = i;
		while(color[j]  == -1){
			color[j] = c;
			j = p[j];
			r = max(j, r);
			l = min(j, r);
		}			
		interval.pb({l, r});
		c++;

	}
	if(interval.size() == 0) return 0;
	//cout << "colors\n";
	//for(int c : color) cout<<c<<" ";
	//cout<<endl;
	//for(ii p : interval) cout << p.fst << " " << p.snd << endl;
	interval.pb({-2,-1});
	int k = 0;
	while(interval[k].fst == interval[k].snd) k++;
	if(interval[k].fst == -2) return 0;
	int l = interval[k].fst, r = interval[k].snd;
	for(ii inter : interval) if(inter.fst != inter.snd){
		if(r < inter.fst) {
			segments.pb({l, r});
			l = inter.fst, r = inter.snd;
		} else r = max(r, inter.snd);	
	}

	//cout << "intervals\n";
	//FOR(i, 0, N) cout << interval[color[i]].fst << " " << interval[color[i]].snd << endl;
	segments.pb({l, r});
	int res = 0;
	FOR(i, 1, segments.size()){
		res += 2*(segments[i].fst - segments[i-1].snd);
	}

	for(auto seg : segments) if(seg.fst <= s and s <= seg.snd){
		int l = s+1, r = s-1;
		res -= 2;
		while(l != seg.fst or r != seg.snd){
			expand(l, r);
			res += 2;
		}
	}
	//cout<<res<<endl;
	//for(ii p : segments) cout << p.fst << " " << p.snd << endl;
	if(s < segments[0].fst) res += 2*(segments[0].fst-s);
	if(s > segments.back().snd) res += 2*(s - segments.back().snd);
	FOR(i, 0, N) res += abs(i-p[i]);
	return res;
}

Compilation message

books.cpp: In function 'long long int minimum_walk(std::vector<int>, INT)':
books.cpp:9:39: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
 #define FOR(i,l,r) for(int i = (l); i < (r); i++)
                                       ^
books.cpp:37:2: note: in expansion of macro 'FOR'
  FOR(i, 0, P.size()) p.pb(P[i]);
  ^~~
books.cpp:9:39: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
 #define FOR(i,l,r) for(int i = (l); i < (r); i++)
                                       ^
books.cpp:75:2: note: in expansion of macro 'FOR'
  FOR(i, 1, segments.size()){
  ^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 376 KB Output is correct
2 Correct 4 ms 616 KB Output is correct
3 Correct 3 ms 616 KB Output is correct
4 Correct 2 ms 616 KB Output is correct
5 Correct 3 ms 616 KB Output is correct
6 Correct 2 ms 616 KB Output is correct
7 Correct 2 ms 616 KB Output is correct
8 Correct 2 ms 616 KB Output is correct
9 Correct 2 ms 616 KB Output is correct
10 Correct 3 ms 616 KB Output is correct
11 Correct 3 ms 616 KB Output is correct
12 Correct 3 ms 616 KB Output is correct
13 Correct 2 ms 616 KB Output is correct
14 Correct 3 ms 616 KB Output is correct
15 Correct 3 ms 616 KB Output is correct
16 Correct 2 ms 616 KB Output is correct
17 Correct 2 ms 616 KB Output is correct
18 Correct 2 ms 616 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 376 KB Output is correct
2 Correct 4 ms 616 KB Output is correct
3 Correct 3 ms 616 KB Output is correct
4 Correct 2 ms 616 KB Output is correct
5 Correct 3 ms 616 KB Output is correct
6 Correct 2 ms 616 KB Output is correct
7 Correct 2 ms 616 KB Output is correct
8 Correct 2 ms 616 KB Output is correct
9 Correct 2 ms 616 KB Output is correct
10 Correct 3 ms 616 KB Output is correct
11 Correct 3 ms 616 KB Output is correct
12 Correct 3 ms 616 KB Output is correct
13 Correct 2 ms 616 KB Output is correct
14 Correct 3 ms 616 KB Output is correct
15 Correct 3 ms 616 KB Output is correct
16 Correct 2 ms 616 KB Output is correct
17 Correct 2 ms 616 KB Output is correct
18 Correct 2 ms 616 KB Output is correct
19 Correct 2 ms 616 KB Output is correct
20 Correct 3 ms 616 KB Output is correct
21 Correct 3 ms 620 KB Output is correct
22 Correct 2 ms 620 KB Output is correct
23 Correct 3 ms 620 KB Output is correct
24 Correct 3 ms 620 KB Output is correct
25 Correct 3 ms 620 KB Output is correct
26 Correct 2 ms 620 KB Output is correct
27 Correct 2 ms 620 KB Output is correct
28 Correct 3 ms 620 KB Output is correct
29 Correct 2 ms 620 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 376 KB Output is correct
2 Correct 4 ms 616 KB Output is correct
3 Correct 3 ms 616 KB Output is correct
4 Correct 2 ms 616 KB Output is correct
5 Correct 3 ms 616 KB Output is correct
6 Correct 2 ms 616 KB Output is correct
7 Correct 2 ms 616 KB Output is correct
8 Correct 2 ms 616 KB Output is correct
9 Correct 2 ms 616 KB Output is correct
10 Correct 3 ms 616 KB Output is correct
11 Correct 3 ms 616 KB Output is correct
12 Correct 3 ms 616 KB Output is correct
13 Correct 2 ms 616 KB Output is correct
14 Correct 3 ms 616 KB Output is correct
15 Correct 3 ms 616 KB Output is correct
16 Correct 2 ms 616 KB Output is correct
17 Correct 2 ms 616 KB Output is correct
18 Correct 2 ms 616 KB Output is correct
19 Correct 2 ms 616 KB Output is correct
20 Correct 3 ms 616 KB Output is correct
21 Correct 3 ms 620 KB Output is correct
22 Correct 2 ms 620 KB Output is correct
23 Correct 3 ms 620 KB Output is correct
24 Correct 3 ms 620 KB Output is correct
25 Correct 3 ms 620 KB Output is correct
26 Correct 2 ms 620 KB Output is correct
27 Correct 2 ms 620 KB Output is correct
28 Correct 3 ms 620 KB Output is correct
29 Correct 2 ms 620 KB Output is correct
30 Correct 414 ms 24268 KB Output is correct
31 Correct 410 ms 24268 KB Output is correct
32 Correct 223 ms 40632 KB Output is correct
33 Correct 218 ms 41940 KB Output is correct
34 Correct 224 ms 41940 KB Output is correct
35 Correct 233 ms 41940 KB Output is correct
36 Correct 202 ms 41940 KB Output is correct
37 Correct 206 ms 41940 KB Output is correct
38 Correct 220 ms 41940 KB Output is correct
39 Correct 251 ms 41940 KB Output is correct
40 Correct 211 ms 41940 KB Output is correct
41 Correct 305 ms 41940 KB Output is correct
42 Correct 286 ms 41940 KB Output is correct
43 Correct 194 ms 41940 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 41940 KB Output is correct
2 Correct 2 ms 41940 KB Output is correct
3 Correct 2 ms 41940 KB Output is correct
4 Correct 3 ms 41940 KB Output is correct
5 Correct 3 ms 41940 KB Output is correct
6 Correct 3 ms 41940 KB Output is correct
7 Correct 3 ms 41940 KB Output is correct
8 Correct 4 ms 41940 KB Output is correct
9 Correct 3 ms 41940 KB Output is correct
10 Correct 2 ms 41940 KB Output is correct
11 Correct 3 ms 41940 KB Output is correct
12 Correct 3 ms 41940 KB Output is correct
13 Correct 3 ms 41940 KB Output is correct
14 Correct 2 ms 41940 KB Output is correct
15 Correct 3 ms 41940 KB Output is correct
16 Correct 3 ms 41940 KB Output is correct
17 Correct 3 ms 41940 KB Output is correct
18 Correct 2 ms 41940 KB Output is correct
19 Correct 3 ms 41940 KB Output is correct
20 Correct 4 ms 41940 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 376 KB Output is correct
2 Correct 4 ms 616 KB Output is correct
3 Correct 3 ms 616 KB Output is correct
4 Correct 2 ms 616 KB Output is correct
5 Correct 3 ms 616 KB Output is correct
6 Correct 2 ms 616 KB Output is correct
7 Correct 2 ms 616 KB Output is correct
8 Correct 2 ms 616 KB Output is correct
9 Correct 2 ms 616 KB Output is correct
10 Correct 3 ms 616 KB Output is correct
11 Correct 3 ms 616 KB Output is correct
12 Correct 3 ms 616 KB Output is correct
13 Correct 2 ms 616 KB Output is correct
14 Correct 3 ms 616 KB Output is correct
15 Correct 3 ms 616 KB Output is correct
16 Correct 2 ms 616 KB Output is correct
17 Correct 2 ms 616 KB Output is correct
18 Correct 2 ms 616 KB Output is correct
19 Correct 2 ms 616 KB Output is correct
20 Correct 3 ms 616 KB Output is correct
21 Correct 3 ms 620 KB Output is correct
22 Correct 2 ms 620 KB Output is correct
23 Correct 3 ms 620 KB Output is correct
24 Correct 3 ms 620 KB Output is correct
25 Correct 3 ms 620 KB Output is correct
26 Correct 2 ms 620 KB Output is correct
27 Correct 2 ms 620 KB Output is correct
28 Correct 3 ms 620 KB Output is correct
29 Correct 2 ms 620 KB Output is correct
30 Correct 414 ms 24268 KB Output is correct
31 Correct 410 ms 24268 KB Output is correct
32 Correct 223 ms 40632 KB Output is correct
33 Correct 218 ms 41940 KB Output is correct
34 Correct 224 ms 41940 KB Output is correct
35 Correct 233 ms 41940 KB Output is correct
36 Correct 202 ms 41940 KB Output is correct
37 Correct 206 ms 41940 KB Output is correct
38 Correct 220 ms 41940 KB Output is correct
39 Correct 251 ms 41940 KB Output is correct
40 Correct 211 ms 41940 KB Output is correct
41 Correct 305 ms 41940 KB Output is correct
42 Correct 286 ms 41940 KB Output is correct
43 Correct 194 ms 41940 KB Output is correct
44 Correct 3 ms 41940 KB Output is correct
45 Correct 2 ms 41940 KB Output is correct
46 Correct 2 ms 41940 KB Output is correct
47 Correct 3 ms 41940 KB Output is correct
48 Correct 3 ms 41940 KB Output is correct
49 Correct 3 ms 41940 KB Output is correct
50 Correct 3 ms 41940 KB Output is correct
51 Correct 4 ms 41940 KB Output is correct
52 Correct 3 ms 41940 KB Output is correct
53 Correct 2 ms 41940 KB Output is correct
54 Correct 3 ms 41940 KB Output is correct
55 Correct 3 ms 41940 KB Output is correct
56 Correct 3 ms 41940 KB Output is correct
57 Correct 2 ms 41940 KB Output is correct
58 Correct 3 ms 41940 KB Output is correct
59 Correct 3 ms 41940 KB Output is correct
60 Correct 3 ms 41940 KB Output is correct
61 Correct 2 ms 41940 KB Output is correct
62 Correct 3 ms 41940 KB Output is correct
63 Correct 4 ms 41940 KB Output is correct
64 Correct 214 ms 47456 KB Output is correct
65 Correct 185 ms 54364 KB Output is correct
66 Correct 192 ms 54364 KB Output is correct
67 Correct 186 ms 54364 KB Output is correct
68 Correct 25 ms 54364 KB Output is correct
69 Correct 20 ms 54364 KB Output is correct
70 Correct 24 ms 54364 KB Output is correct
71 Correct 29 ms 54364 KB Output is correct
72 Correct 21 ms 54364 KB Output is correct
73 Correct 20 ms 54364 KB Output is correct
74 Correct 187 ms 79188 KB Output is correct
75 Correct 187 ms 86000 KB Output is correct
76 Correct 285 ms 91480 KB Output is correct
77 Correct 229 ms 98156 KB Output is correct
78 Correct 245 ms 104956 KB Output is correct
79 Correct 240 ms 111644 KB Output is correct
80 Correct 197 ms 118516 KB Output is correct
81 Correct 181 ms 121088 KB Output is correct
82 Correct 180 ms 127780 KB Output is correct
83 Correct 224 ms 134452 KB Output is correct
84 Correct 190 ms 141196 KB Output is correct
85 Correct 200 ms 141924 KB Output is correct
86 Correct 186 ms 144276 KB Output is correct
87 Correct 220 ms 165652 KB Output is correct
88 Correct 235 ms 172448 KB Output is correct
89 Correct 219 ms 174860 KB Output is correct
90 Correct 199 ms 174860 KB Output is correct
91 Correct 186 ms 176444 KB Output is correct
92 Correct 209 ms 182948 KB Output is correct