Submission #65783

# Submission time Handle Problem Language Result Execution time Memory
65783 2018-08-08T18:09:39 Z MatheusLealV Ancient Books (IOI17_books) C++17
50 / 100
443 ms 76848 KB
#include "books.h"
#define N 1000005
#define f first
#define ss second
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;

ll ans = 0;

ll n, maior[N], menor[N], qtd = 0, ok[N], en;

int pai[N], peso[N];

int find(int x)
{
	if(x == pai[x]) return x;

	return pai[x] = find(pai[x]);
}

void join(int a, int b)
{
	a = find(a), b = find(b);

	if(a == b) return;

	if(peso[a] > peso[b]) pai[b] = a;

	else if(peso[a] < peso[b]) pai[a] = b;

	else pai[a] = b, peso[b] ++;
}

vector<int> g;

void dfs(ll x, ll &mx, ll &mn)
{
	mx = max(mx, maior[x]);

	if(ok[x]) return;

	ok[x] = 1;

	dfs(g[x], mx, mn);
}

ll minimum_walk(vector<int> p, int s)
{
	n = p.size();

	g = p;

	for(ll i = 0; i < n; i++) ans += abs(i - p[i]);

	for(int i = 0; i < n; i++) maior[i] = menor[i] = i;

	for(ll i = n - 1; i >= 0; i--)
	{
		if(p[i] != i)
		{
			en = i;

			break;
		}
	}

	vector<pii> sweep;

	for(int i = 0; i <= en; i++)
	{
		if(ok[i]) continue;

		dfs(i, maior[i], menor[i]);

		sweep.push_back({menor[i], maior[i]});
	}

	sort(sweep.begin(), sweep.end());

	for(int i = 0; i < (int)sweep.size(); i++) pai[i] = i;

	pii a = {-1, -1};

	for(int i = 0; i < (int)sweep.size(); i++)
	{
		auto b = sweep[i];

		if(b.f <= a.f) join(i, a.ss);

		a = max(a, {b.ss, i});
	}

	set<int> q;

	for(int i = 0; i < (int)sweep.size(); i++) q.insert(find(i));

	ll qtd = q.size();

	return ans + 2*(qtd - 1);
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 3 ms 392 KB Output is correct
4 Correct 4 ms 440 KB Output is correct
5 Correct 2 ms 440 KB Output is correct
6 Correct 2 ms 440 KB Output is correct
7 Correct 2 ms 468 KB Output is correct
8 Correct 2 ms 544 KB Output is correct
9 Correct 3 ms 544 KB Output is correct
10 Correct 2 ms 544 KB Output is correct
11 Correct 2 ms 544 KB Output is correct
12 Correct 2 ms 544 KB Output is correct
13 Correct 2 ms 620 KB Output is correct
14 Correct 3 ms 620 KB Output is correct
15 Correct 2 ms 620 KB Output is correct
16 Correct 2 ms 620 KB Output is correct
17 Correct 2 ms 620 KB Output is correct
18 Correct 2 ms 620 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 3 ms 392 KB Output is correct
4 Correct 4 ms 440 KB Output is correct
5 Correct 2 ms 440 KB Output is correct
6 Correct 2 ms 440 KB Output is correct
7 Correct 2 ms 468 KB Output is correct
8 Correct 2 ms 544 KB Output is correct
9 Correct 3 ms 544 KB Output is correct
10 Correct 2 ms 544 KB Output is correct
11 Correct 2 ms 544 KB Output is correct
12 Correct 2 ms 544 KB Output is correct
13 Correct 2 ms 620 KB Output is correct
14 Correct 3 ms 620 KB Output is correct
15 Correct 2 ms 620 KB Output is correct
16 Correct 2 ms 620 KB Output is correct
17 Correct 2 ms 620 KB Output is correct
18 Correct 2 ms 620 KB Output is correct
19 Correct 2 ms 620 KB Output is correct
20 Correct 2 ms 624 KB Output is correct
21 Correct 2 ms 624 KB Output is correct
22 Correct 2 ms 624 KB Output is correct
23 Correct 3 ms 624 KB Output is correct
24 Correct 2 ms 636 KB Output is correct
25 Correct 2 ms 636 KB Output is correct
26 Correct 3 ms 636 KB Output is correct
27 Correct 3 ms 636 KB Output is correct
28 Correct 2 ms 636 KB Output is correct
29 Correct 2 ms 636 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 3 ms 392 KB Output is correct
4 Correct 4 ms 440 KB Output is correct
5 Correct 2 ms 440 KB Output is correct
6 Correct 2 ms 440 KB Output is correct
7 Correct 2 ms 468 KB Output is correct
8 Correct 2 ms 544 KB Output is correct
9 Correct 3 ms 544 KB Output is correct
10 Correct 2 ms 544 KB Output is correct
11 Correct 2 ms 544 KB Output is correct
12 Correct 2 ms 544 KB Output is correct
13 Correct 2 ms 620 KB Output is correct
14 Correct 3 ms 620 KB Output is correct
15 Correct 2 ms 620 KB Output is correct
16 Correct 2 ms 620 KB Output is correct
17 Correct 2 ms 620 KB Output is correct
18 Correct 2 ms 620 KB Output is correct
19 Correct 2 ms 620 KB Output is correct
20 Correct 2 ms 624 KB Output is correct
21 Correct 2 ms 624 KB Output is correct
22 Correct 2 ms 624 KB Output is correct
23 Correct 3 ms 624 KB Output is correct
24 Correct 2 ms 636 KB Output is correct
25 Correct 2 ms 636 KB Output is correct
26 Correct 3 ms 636 KB Output is correct
27 Correct 3 ms 636 KB Output is correct
28 Correct 2 ms 636 KB Output is correct
29 Correct 2 ms 636 KB Output is correct
30 Correct 270 ms 35836 KB Output is correct
31 Correct 301 ms 35836 KB Output is correct
32 Correct 151 ms 35836 KB Output is correct
33 Correct 437 ms 76728 KB Output is correct
34 Correct 443 ms 76848 KB Output is correct
35 Correct 375 ms 76848 KB Output is correct
36 Correct 252 ms 76848 KB Output is correct
37 Correct 180 ms 76848 KB Output is correct
38 Correct 193 ms 76848 KB Output is correct
39 Correct 183 ms 76848 KB Output is correct
40 Correct 235 ms 76848 KB Output is correct
41 Correct 221 ms 76848 KB Output is correct
42 Correct 235 ms 76848 KB Output is correct
43 Correct 220 ms 76848 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 3 ms 76848 KB 3rd lines differ - on the 1st token, expected: '3304', found: '2744'
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 3 ms 392 KB Output is correct
4 Correct 4 ms 440 KB Output is correct
5 Correct 2 ms 440 KB Output is correct
6 Correct 2 ms 440 KB Output is correct
7 Correct 2 ms 468 KB Output is correct
8 Correct 2 ms 544 KB Output is correct
9 Correct 3 ms 544 KB Output is correct
10 Correct 2 ms 544 KB Output is correct
11 Correct 2 ms 544 KB Output is correct
12 Correct 2 ms 544 KB Output is correct
13 Correct 2 ms 620 KB Output is correct
14 Correct 3 ms 620 KB Output is correct
15 Correct 2 ms 620 KB Output is correct
16 Correct 2 ms 620 KB Output is correct
17 Correct 2 ms 620 KB Output is correct
18 Correct 2 ms 620 KB Output is correct
19 Correct 2 ms 620 KB Output is correct
20 Correct 2 ms 624 KB Output is correct
21 Correct 2 ms 624 KB Output is correct
22 Correct 2 ms 624 KB Output is correct
23 Correct 3 ms 624 KB Output is correct
24 Correct 2 ms 636 KB Output is correct
25 Correct 2 ms 636 KB Output is correct
26 Correct 3 ms 636 KB Output is correct
27 Correct 3 ms 636 KB Output is correct
28 Correct 2 ms 636 KB Output is correct
29 Correct 2 ms 636 KB Output is correct
30 Correct 270 ms 35836 KB Output is correct
31 Correct 301 ms 35836 KB Output is correct
32 Correct 151 ms 35836 KB Output is correct
33 Correct 437 ms 76728 KB Output is correct
34 Correct 443 ms 76848 KB Output is correct
35 Correct 375 ms 76848 KB Output is correct
36 Correct 252 ms 76848 KB Output is correct
37 Correct 180 ms 76848 KB Output is correct
38 Correct 193 ms 76848 KB Output is correct
39 Correct 183 ms 76848 KB Output is correct
40 Correct 235 ms 76848 KB Output is correct
41 Correct 221 ms 76848 KB Output is correct
42 Correct 235 ms 76848 KB Output is correct
43 Correct 220 ms 76848 KB Output is correct
44 Incorrect 3 ms 76848 KB 3rd lines differ - on the 1st token, expected: '3304', found: '2744'
45 Halted 0 ms 0 KB -