Submission #288559

# Submission time Handle Problem Language Result Execution time Memory
288559 2020-09-01T16:02:35 Z user202729 Ancient Books (IOI17_books) C++17
100 / 100
582 ms 40148 KB
// moreflags=grader.cpp
// 11
// a little faster.

#include "books.h"
#include<algorithm>
#include<numeric>
#if not LOCAL
#define NDEBUG
#endif
#include<cassert>
#include<climits>


long long minimum_walk(std::vector<int> p, int s) {
	assert(s>=0); assert(s<(int)p.size());

	int64_t result{};
	for(int index=0; index<(int)p.size(); ++index){
		if(index<p[index]){
			result+=p[index]-index;
		}
	}

	struct Dsu{
		std::vector<int> data;
		void reset(int number){data.assign(number, -1);}
		int root(int node){return data[node]>=0 ? data[node]=root(data[node]): node;}
		bool join(int first, int sec){
			first=root(first); sec=root(sec);
			if(first==sec) return false;
			data[first]=sec;
			return true;
		}
	};
	Dsu dsu; dsu.reset((int)p.size());

	std::vector<int> depth(p.size(), -1); // in stack (-2: already popped)
	struct Item{
		int firstConnect; // stack[firstConnect]..=current item should be connected (or INT_MAX)
		int id;
	};
	std::vector<Item> stack;
	for(int pos=0; pos<(int)p.size(); ++pos) if(pos!=p[pos]){
		dsu.join(pos, p[pos]);
		for(auto id: {pos, p[pos]}){
			if(depth[id]<0){
				assert(depth[id]==-1);
				depth[id]=(int)stack.size();
				stack.push_back({INT_MAX, id});
			}else{
				auto const curDepth=depth[id];
				assert(stack[curDepth].id==id);
				if(stack[curDepth].firstConnect<curDepth){
					stack[curDepth-1].firstConnect=std::min(stack[curDepth-1].firstConnect,
							stack[curDepth].firstConnect);
					if(stack[curDepth-1].id>=0)
						dsu.join(stack[curDepth-1].id, id);
				}

				stack[curDepth].id=-1;
				bool const removeBack=curDepth==(int)stack.size()-1;
				depth[id]=-2;

				if(not removeBack){
					dsu.join(id, stack.back().id);
				}

				int minFirstConnect=curDepth;
				while(not stack.empty() and stack.back().id<0){
					assert(removeBack);
					assert(stack.back().id==-1);
					minFirstConnect=std::min(minFirstConnect, stack.back().firstConnect);
					stack.pop_back();
				}
				if(minFirstConnect<(int)stack.size()){
					stack.back().firstConnect=std::min(stack.back().firstConnect,
							minFirstConnect);
					dsu.join(id, stack.back().id);
				}
			}
		}
	}

	std::vector<std::array<int, 2>> rootBound(p.size(), {{INT_MAX, INT_MIN}});
	for(int index=0; index<(int)p.size(); ++index){
		auto const t=dsu.root(index);
		rootBound[t]={{
			std::min(rootBound[t][0], index),
				std::max(rootBound[t][1], index)
		}};
	}

	std::vector<int> left(p.size(), INT_MAX), right(p.size(), -1);
	for(int index=0; index<(int)p.size(); ++index) if(p[index]!=index){
		auto const t=dsu.root(index);
		left[index]=rootBound[t][0];
		right[index]=rootBound[t][1];
	}

	int left_=std::min(s, left[s]), right_=std::max(s, right[s]);
	while(true){
		int left1=left_, cost1=0;
		while(left1>=0 and right[left1]<=right_){
			if(left[left1]<=left1)
				left1=left[left1];
			assert(left[left1]>=left1);
			++cost1;
			--left1;
		}

		int right2=right_, cost2=0;
		while(right2<(int)p.size() and left[right2]>=left_){
			if(right[right2]>=right2)
				right2=right[right2];
			assert(right[right2]<=right2);
			++cost2;
			++right2;
		}

		assert((left1>=0)==(right2<(int)p.size()));
		if(left1<0){
			break;
		}

		if(cost1<cost2){
			result+=cost1;
		}else{
			result+=cost2;
		}

		assert(left[left1]==left[right2]);
		assert(right[left1]==right[right2]);
		left_=left[left1]; right_=right[right2];

		assert(left[left_]>=left_);
		assert(right[right_]<=right_);
	}

	while(true){
		assert(left[left_]>=left_);
		int left1=left_;
		while(left1>=0 and left[left1]>=left1) --left1;
		if(left1<0) break;
		assert(right[left1]<left_);
		result+=left_-left1; left_=left[left1];
	}

	while(true){
		assert(right[right_]<=right_);
		int right2=right_;
		while(right2<(int)p.size() and right[right2]<=right2) ++right2;
		if(right2==(int)p.size()) break;
		assert(left[right2]>right_);
		result+=right2-right_; right_=right[right2];
	}

	return result*2;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 256 KB Output is correct
2 Correct 0 ms 256 KB Output is correct
3 Correct 0 ms 256 KB Output is correct
4 Correct 0 ms 256 KB Output is correct
5 Correct 0 ms 256 KB Output is correct
6 Correct 1 ms 256 KB Output is correct
7 Correct 0 ms 256 KB Output is correct
8 Correct 1 ms 256 KB Output is correct
9 Correct 0 ms 320 KB Output is correct
10 Correct 0 ms 256 KB Output is correct
11 Correct 0 ms 256 KB Output is correct
12 Correct 0 ms 256 KB Output is correct
13 Correct 0 ms 256 KB Output is correct
14 Correct 0 ms 256 KB Output is correct
15 Correct 1 ms 256 KB Output is correct
16 Correct 0 ms 256 KB Output is correct
17 Correct 0 ms 256 KB Output is correct
18 Correct 0 ms 256 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 256 KB Output is correct
2 Correct 0 ms 256 KB Output is correct
3 Correct 0 ms 256 KB Output is correct
4 Correct 0 ms 256 KB Output is correct
5 Correct 0 ms 256 KB Output is correct
6 Correct 1 ms 256 KB Output is correct
7 Correct 0 ms 256 KB Output is correct
8 Correct 1 ms 256 KB Output is correct
9 Correct 0 ms 320 KB Output is correct
10 Correct 0 ms 256 KB Output is correct
11 Correct 0 ms 256 KB Output is correct
12 Correct 0 ms 256 KB Output is correct
13 Correct 0 ms 256 KB Output is correct
14 Correct 0 ms 256 KB Output is correct
15 Correct 1 ms 256 KB Output is correct
16 Correct 0 ms 256 KB Output is correct
17 Correct 0 ms 256 KB Output is correct
18 Correct 0 ms 256 KB Output is correct
19 Correct 1 ms 384 KB Output is correct
20 Correct 1 ms 384 KB Output is correct
21 Correct 1 ms 384 KB Output is correct
22 Correct 1 ms 384 KB Output is correct
23 Correct 1 ms 384 KB Output is correct
24 Correct 1 ms 384 KB Output is correct
25 Correct 1 ms 384 KB Output is correct
26 Correct 1 ms 384 KB Output is correct
27 Correct 1 ms 384 KB Output is correct
28 Correct 1 ms 384 KB Output is correct
29 Correct 1 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 256 KB Output is correct
2 Correct 0 ms 256 KB Output is correct
3 Correct 0 ms 256 KB Output is correct
4 Correct 0 ms 256 KB Output is correct
5 Correct 0 ms 256 KB Output is correct
6 Correct 1 ms 256 KB Output is correct
7 Correct 0 ms 256 KB Output is correct
8 Correct 1 ms 256 KB Output is correct
9 Correct 0 ms 320 KB Output is correct
10 Correct 0 ms 256 KB Output is correct
11 Correct 0 ms 256 KB Output is correct
12 Correct 0 ms 256 KB Output is correct
13 Correct 0 ms 256 KB Output is correct
14 Correct 0 ms 256 KB Output is correct
15 Correct 1 ms 256 KB Output is correct
16 Correct 0 ms 256 KB Output is correct
17 Correct 0 ms 256 KB Output is correct
18 Correct 0 ms 256 KB Output is correct
19 Correct 1 ms 384 KB Output is correct
20 Correct 1 ms 384 KB Output is correct
21 Correct 1 ms 384 KB Output is correct
22 Correct 1 ms 384 KB Output is correct
23 Correct 1 ms 384 KB Output is correct
24 Correct 1 ms 384 KB Output is correct
25 Correct 1 ms 384 KB Output is correct
26 Correct 1 ms 384 KB Output is correct
27 Correct 1 ms 384 KB Output is correct
28 Correct 1 ms 384 KB Output is correct
29 Correct 1 ms 384 KB Output is correct
30 Correct 582 ms 39888 KB Output is correct
31 Correct 570 ms 40148 KB Output is correct
32 Correct 170 ms 32120 KB Output is correct
33 Correct 202 ms 32120 KB Output is correct
34 Correct 211 ms 32120 KB Output is correct
35 Correct 218 ms 32124 KB Output is correct
36 Correct 241 ms 32120 KB Output is correct
37 Correct 254 ms 32120 KB Output is correct
38 Correct 264 ms 32376 KB Output is correct
39 Correct 265 ms 32376 KB Output is correct
40 Correct 282 ms 32624 KB Output is correct
41 Correct 418 ms 36320 KB Output is correct
42 Correct 355 ms 35296 KB Output is correct
43 Correct 202 ms 32248 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 1 ms 384 KB Output is correct
17 Correct 1 ms 384 KB Output is correct
18 Correct 1 ms 384 KB Output is correct
19 Correct 1 ms 384 KB Output is correct
20 Correct 1 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 256 KB Output is correct
2 Correct 0 ms 256 KB Output is correct
3 Correct 0 ms 256 KB Output is correct
4 Correct 0 ms 256 KB Output is correct
5 Correct 0 ms 256 KB Output is correct
6 Correct 1 ms 256 KB Output is correct
7 Correct 0 ms 256 KB Output is correct
8 Correct 1 ms 256 KB Output is correct
9 Correct 0 ms 320 KB Output is correct
10 Correct 0 ms 256 KB Output is correct
11 Correct 0 ms 256 KB Output is correct
12 Correct 0 ms 256 KB Output is correct
13 Correct 0 ms 256 KB Output is correct
14 Correct 0 ms 256 KB Output is correct
15 Correct 1 ms 256 KB Output is correct
16 Correct 0 ms 256 KB Output is correct
17 Correct 0 ms 256 KB Output is correct
18 Correct 0 ms 256 KB Output is correct
19 Correct 1 ms 384 KB Output is correct
20 Correct 1 ms 384 KB Output is correct
21 Correct 1 ms 384 KB Output is correct
22 Correct 1 ms 384 KB Output is correct
23 Correct 1 ms 384 KB Output is correct
24 Correct 1 ms 384 KB Output is correct
25 Correct 1 ms 384 KB Output is correct
26 Correct 1 ms 384 KB Output is correct
27 Correct 1 ms 384 KB Output is correct
28 Correct 1 ms 384 KB Output is correct
29 Correct 1 ms 384 KB Output is correct
30 Correct 582 ms 39888 KB Output is correct
31 Correct 570 ms 40148 KB Output is correct
32 Correct 170 ms 32120 KB Output is correct
33 Correct 202 ms 32120 KB Output is correct
34 Correct 211 ms 32120 KB Output is correct
35 Correct 218 ms 32124 KB Output is correct
36 Correct 241 ms 32120 KB Output is correct
37 Correct 254 ms 32120 KB Output is correct
38 Correct 264 ms 32376 KB Output is correct
39 Correct 265 ms 32376 KB Output is correct
40 Correct 282 ms 32624 KB Output is correct
41 Correct 418 ms 36320 KB Output is correct
42 Correct 355 ms 35296 KB Output is correct
43 Correct 202 ms 32248 KB Output is correct
44 Correct 1 ms 384 KB Output is correct
45 Correct 1 ms 384 KB Output is correct
46 Correct 1 ms 384 KB Output is correct
47 Correct 1 ms 384 KB Output is correct
48 Correct 1 ms 384 KB Output is correct
49 Correct 1 ms 384 KB Output is correct
50 Correct 1 ms 384 KB Output is correct
51 Correct 1 ms 384 KB Output is correct
52 Correct 1 ms 384 KB Output is correct
53 Correct 1 ms 384 KB Output is correct
54 Correct 1 ms 384 KB Output is correct
55 Correct 1 ms 384 KB Output is correct
56 Correct 1 ms 384 KB Output is correct
57 Correct 1 ms 384 KB Output is correct
58 Correct 1 ms 384 KB Output is correct
59 Correct 1 ms 384 KB Output is correct
60 Correct 1 ms 384 KB Output is correct
61 Correct 1 ms 384 KB Output is correct
62 Correct 1 ms 384 KB Output is correct
63 Correct 1 ms 384 KB Output is correct
64 Correct 193 ms 32120 KB Output is correct
65 Correct 186 ms 32120 KB Output is correct
66 Correct 256 ms 32252 KB Output is correct
67 Correct 257 ms 32376 KB Output is correct
68 Correct 24 ms 3960 KB Output is correct
69 Correct 26 ms 3960 KB Output is correct
70 Correct 23 ms 3968 KB Output is correct
71 Correct 20 ms 3968 KB Output is correct
72 Correct 18 ms 3968 KB Output is correct
73 Correct 28 ms 4348 KB Output is correct
74 Correct 206 ms 32120 KB Output is correct
75 Correct 211 ms 32120 KB Output is correct
76 Correct 201 ms 32248 KB Output is correct
77 Correct 200 ms 32120 KB Output is correct
78 Correct 210 ms 32248 KB Output is correct
79 Correct 208 ms 31992 KB Output is correct
80 Correct 172 ms 31992 KB Output is correct
81 Correct 204 ms 37332 KB Output is correct
82 Correct 206 ms 37076 KB Output is correct
83 Correct 228 ms 32496 KB Output is correct
84 Correct 240 ms 32368 KB Output is correct
85 Correct 248 ms 32180 KB Output is correct
86 Correct 257 ms 32120 KB Output is correct
87 Correct 196 ms 34400 KB Output is correct
88 Correct 205 ms 33384 KB Output is correct
89 Correct 214 ms 32620 KB Output is correct
90 Correct 259 ms 31864 KB Output is correct
91 Correct 265 ms 31864 KB Output is correct
92 Correct 279 ms 32124 KB Output is correct