Submission #147837

# Submission time Handle Problem Language Result Execution time Memory
147837 2019-08-30T22:11:56 Z faremy Ancient Books (IOI17_books) C++14
100 / 100
198 ms 26660 KB
#include "books.h"

#include <cstdlib>
#include <queue>
#include <utility>


const int MAXN = 1e6;

int belongsTo[MAXN];
std::pair<int, int> cycInter[MAXN];
std::queue<std::pair<int, int>> toCheck;


void processcycle(int cycle, int &left, int &right)
{
	if (cycInter[cycle].first < left)
	{
		toCheck.emplace(cycInter[cycle].first, left);
		left = cycInter[cycle].first;
	}
	
	if (cycInter[cycle].second > right)
	{
		toCheck.emplace(right, cycInter[cycle].second);
		right = cycInter[cycle].second;
	}
}

std::pair<int, int> check(int doneLeft, int doneRight)
{
	int left = doneLeft, right = doneRight;
	while (!toCheck.empty())
	{
		std::pair<int, int> checkingInter = toCheck.front();
		toCheck.pop();

		for (int iTable = checkingInter.first; iTable < checkingInter.second; iTable++)
			processcycle(belongsTo[iTable], left, right);
	}

	return { left, right };
}

long long minimum_walk(std::vector<int> p, int s)
{
	int tables = p.size();
	std::fill_n(belongsTo, tables, -1);
	long long minWalk = 0;

	int cycles = 0;
	for (int iTable = 0; iTable < tables; iTable++)
		if (belongsTo[iTable] == -1)
		{
			int leftMost = tables, rightMost = 0;
			int curTable = iTable;

			while (belongsTo[curTable] == -1)
			{
				leftMost = std::min(leftMost, curTable);
				rightMost = std::max(rightMost, curTable);

				belongsTo[curTable] = cycles;
				minWalk += (long long)std::abs(curTable - p[curTable]);
				curTable = p[curTable];
			}

			cycInter[cycles] = { leftMost, rightMost + 1 };
			cycles++;
		}

	int endLeft = 0, endRight = tables;
	while (endLeft < s && cycInter[belongsTo[endLeft]].second - cycInter[belongsTo[endLeft]].first == 1)
		endLeft++;
	while (endRight - 1 > s && cycInter[belongsTo[endRight - 1]].second - cycInter[belongsTo[endRight - 1]].first == 1)
		endRight--;

	toCheck.emplace(s, s + 1);
	std::pair<int, int> done = check(s, s + 1);

	while (done.first != endLeft || done.second != endRight)
	{
		long long costLeft = 0;
		std::pair<int, int> moveLeft = done;

		while (moveLeft.first != endLeft && moveLeft.second == done.second)
		{
			toCheck.emplace(moveLeft.first - 1, moveLeft.first);
			costLeft += 2;
			moveLeft = check(moveLeft.first - 1, moveLeft.second);
		}

		long long costRight = 0;
		std::pair<int, int> moveRight = done;

		while (moveRight.first == done.first && moveRight.second != endRight)
		{
			toCheck.emplace(moveRight.second, moveRight.second + 1);
			costRight += 2;
			moveRight = check(moveRight.first, moveRight.second + 1);
		}

		if (moveLeft == moveRight)
		{
			minWalk += std::min(costLeft, costRight);
			done = moveLeft;
		}
		else
		{
			minWalk += costLeft + costRight;
			done = { moveLeft.first, moveRight.second };
		}
	}

	return minWalk;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 256 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 256 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 256 KB Output is correct
8 Correct 2 ms 256 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
11 Correct 2 ms 256 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 2 ms 256 KB Output is correct
14 Correct 2 ms 256 KB Output is correct
15 Correct 2 ms 376 KB Output is correct
16 Correct 2 ms 256 KB Output is correct
17 Correct 2 ms 256 KB Output is correct
18 Correct 2 ms 256 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 256 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 256 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 256 KB Output is correct
8 Correct 2 ms 256 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
11 Correct 2 ms 256 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 2 ms 256 KB Output is correct
14 Correct 2 ms 256 KB Output is correct
15 Correct 2 ms 376 KB Output is correct
16 Correct 2 ms 256 KB Output is correct
17 Correct 2 ms 256 KB Output is correct
18 Correct 2 ms 256 KB Output is correct
19 Correct 2 ms 376 KB Output is correct
20 Correct 2 ms 376 KB Output is correct
21 Correct 2 ms 256 KB Output is correct
22 Correct 2 ms 376 KB Output is correct
23 Correct 2 ms 256 KB Output is correct
24 Correct 2 ms 376 KB Output is correct
25 Correct 2 ms 376 KB Output is correct
26 Correct 2 ms 376 KB Output is correct
27 Correct 2 ms 504 KB Output is correct
28 Correct 2 ms 256 KB Output is correct
29 Correct 2 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 256 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 256 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 256 KB Output is correct
8 Correct 2 ms 256 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
11 Correct 2 ms 256 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 2 ms 256 KB Output is correct
14 Correct 2 ms 256 KB Output is correct
15 Correct 2 ms 376 KB Output is correct
16 Correct 2 ms 256 KB Output is correct
17 Correct 2 ms 256 KB Output is correct
18 Correct 2 ms 256 KB Output is correct
19 Correct 2 ms 376 KB Output is correct
20 Correct 2 ms 376 KB Output is correct
21 Correct 2 ms 256 KB Output is correct
22 Correct 2 ms 376 KB Output is correct
23 Correct 2 ms 256 KB Output is correct
24 Correct 2 ms 376 KB Output is correct
25 Correct 2 ms 376 KB Output is correct
26 Correct 2 ms 376 KB Output is correct
27 Correct 2 ms 504 KB Output is correct
28 Correct 2 ms 256 KB Output is correct
29 Correct 2 ms 376 KB Output is correct
30 Correct 198 ms 12084 KB Output is correct
31 Correct 194 ms 12092 KB Output is correct
32 Correct 145 ms 19924 KB Output is correct
33 Correct 165 ms 17500 KB Output is correct
34 Correct 165 ms 17528 KB Output is correct
35 Correct 166 ms 16376 KB Output is correct
36 Correct 157 ms 14072 KB Output is correct
37 Correct 153 ms 12280 KB Output is correct
38 Correct 158 ms 12084 KB Output is correct
39 Correct 158 ms 12152 KB Output is correct
40 Correct 165 ms 12152 KB Output is correct
41 Correct 190 ms 12024 KB Output is correct
42 Correct 185 ms 12024 KB Output is correct
43 Correct 156 ms 17876 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 256 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
11 Correct 2 ms 376 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 2 ms 376 KB Output is correct
14 Correct 2 ms 376 KB Output is correct
15 Correct 2 ms 256 KB Output is correct
16 Correct 2 ms 256 KB Output is correct
17 Correct 2 ms 376 KB Output is correct
18 Correct 2 ms 376 KB Output is correct
19 Correct 2 ms 256 KB Output is correct
20 Correct 2 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 256 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 256 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 256 KB Output is correct
8 Correct 2 ms 256 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
11 Correct 2 ms 256 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 2 ms 256 KB Output is correct
14 Correct 2 ms 256 KB Output is correct
15 Correct 2 ms 376 KB Output is correct
16 Correct 2 ms 256 KB Output is correct
17 Correct 2 ms 256 KB Output is correct
18 Correct 2 ms 256 KB Output is correct
19 Correct 2 ms 376 KB Output is correct
20 Correct 2 ms 376 KB Output is correct
21 Correct 2 ms 256 KB Output is correct
22 Correct 2 ms 376 KB Output is correct
23 Correct 2 ms 256 KB Output is correct
24 Correct 2 ms 376 KB Output is correct
25 Correct 2 ms 376 KB Output is correct
26 Correct 2 ms 376 KB Output is correct
27 Correct 2 ms 504 KB Output is correct
28 Correct 2 ms 256 KB Output is correct
29 Correct 2 ms 376 KB Output is correct
30 Correct 198 ms 12084 KB Output is correct
31 Correct 194 ms 12092 KB Output is correct
32 Correct 145 ms 19924 KB Output is correct
33 Correct 165 ms 17500 KB Output is correct
34 Correct 165 ms 17528 KB Output is correct
35 Correct 166 ms 16376 KB Output is correct
36 Correct 157 ms 14072 KB Output is correct
37 Correct 153 ms 12280 KB Output is correct
38 Correct 158 ms 12084 KB Output is correct
39 Correct 158 ms 12152 KB Output is correct
40 Correct 165 ms 12152 KB Output is correct
41 Correct 190 ms 12024 KB Output is correct
42 Correct 185 ms 12024 KB Output is correct
43 Correct 156 ms 17876 KB Output is correct
44 Correct 2 ms 376 KB Output is correct
45 Correct 2 ms 376 KB Output is correct
46 Correct 2 ms 376 KB Output is correct
47 Correct 2 ms 376 KB Output is correct
48 Correct 2 ms 376 KB Output is correct
49 Correct 2 ms 256 KB Output is correct
50 Correct 2 ms 376 KB Output is correct
51 Correct 2 ms 376 KB Output is correct
52 Correct 2 ms 376 KB Output is correct
53 Correct 2 ms 376 KB Output is correct
54 Correct 2 ms 376 KB Output is correct
55 Correct 2 ms 376 KB Output is correct
56 Correct 2 ms 376 KB Output is correct
57 Correct 2 ms 376 KB Output is correct
58 Correct 2 ms 256 KB Output is correct
59 Correct 2 ms 256 KB Output is correct
60 Correct 2 ms 376 KB Output is correct
61 Correct 2 ms 376 KB Output is correct
62 Correct 2 ms 256 KB Output is correct
63 Correct 2 ms 348 KB Output is correct
64 Correct 157 ms 24584 KB Output is correct
65 Correct 157 ms 25252 KB Output is correct
66 Correct 159 ms 19036 KB Output is correct
67 Correct 161 ms 18980 KB Output is correct
68 Correct 17 ms 2300 KB Output is correct
69 Correct 17 ms 2224 KB Output is correct
70 Correct 17 ms 2424 KB Output is correct
71 Correct 18 ms 2552 KB Output is correct
72 Correct 17 ms 2680 KB Output is correct
73 Correct 18 ms 2040 KB Output is correct
74 Correct 167 ms 24272 KB Output is correct
75 Correct 166 ms 24184 KB Output is correct
76 Correct 167 ms 24540 KB Output is correct
77 Correct 166 ms 24644 KB Output is correct
78 Correct 161 ms 23792 KB Output is correct
79 Correct 160 ms 23744 KB Output is correct
80 Correct 147 ms 26660 KB Output is correct
81 Correct 166 ms 22776 KB Output is correct
82 Correct 165 ms 22776 KB Output is correct
83 Correct 169 ms 21828 KB Output is correct
84 Correct 165 ms 21112 KB Output is correct
85 Correct 163 ms 19884 KB Output is correct
86 Correct 158 ms 19268 KB Output is correct
87 Correct 191 ms 25512 KB Output is correct
88 Correct 182 ms 24312 KB Output is correct
89 Correct 171 ms 22564 KB Output is correct
90 Correct 158 ms 18936 KB Output is correct
91 Correct 163 ms 18808 KB Output is correct
92 Correct 167 ms 18780 KB Output is correct