답안 #464895

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
464895 2021-08-14T11:53:01 Z blue 고대 책들 (IOI17_books) C++17
50 / 100
207 ms 23780 KB
#include "books.h"
#include <vector>
#include <cstdlib>
#include <algorithm>
#include <iostream>
using namespace std;

const int maxN = 1'000'000;

const int ind_R = 1'000'005;
const int ind_L = -1;

const long long INF = 1'000'000'000'000'000'000LL;

int N;
vector<int> P;

int S;

int cycle_ind = -1;
vector<int> cycle(maxN, -1);
vector<int> L(maxN, ind_R);
vector<int> R(maxN, ind_L);

long long essential_cost = 0;

void extend(int& l, int& r) //[l, r] is extended fully except for the cycles of l and r
{
	int l_new = min(L[cycle[l]], L[cycle[r]]);
	int r_new = max(R[cycle[l]], R[cycle[r]]);

	// cerr << "ln = " << l_new << ", rn = " << r_new << '\n';

	while(l_new <= l-1 || r+1 <= r_new)
	{
		if(l_new <= l-1)
		{
			l--;
			l_new = min(l_new, L[cycle[l]]);
			r_new = max(r_new, R[cycle[l]]);
		}

		if(r+1 <= r_new)
		{
			r++;
			l_new = min(l_new, L[cycle[r]]);
			r_new = max(r_new, R[cycle[r]]);
		}
	}
}

long long minimum_walk(vector<int> p, int s)
{
	N = p.size();
	P = p;
	S = s;

	int solve_L = S;
	int solve_R = S;

	for(int i = 0; i < N; i++)
	{
		essential_cost += abs(P[i] - i);

		if(P[i] != i)
		{
			solve_L = min(solve_L, i);
			solve_R = max(solve_R, i);
		}

		if(cycle[i] != -1) continue;

		cycle_ind++;

		int j = i;
		do
		{
			cycle[j] = cycle_ind;

			L[ cycle[j] ] = min(L[ cycle[j] ], j);
			R[ cycle[j] ] = max(R[ cycle[j] ], j);

			j = P[j];
		} while(j != i);
	}

	if(solve_L == S && solve_R == S) return 0;

	// for(int i = 0; i < N; i++) cerr << cycle[i] << ' ';
	// cerr << '\n';
	//
	// cerr << "solve range = " <<  solve_L << ' ' << solve_R << '\n';


	long long extra_cost = 0;

	int l = S;
	int r = S;
	// cerr << "l = " << l << ", r = " << r << '\n';
	extend(l, r);
	// cerr << "l = " << l << ", r = " << r << '\n';

	while(l != solve_L || r != solve_R)
	{
		extend(l, r);

		int new_l = -1, new_r = -1;
		for(int i = l-1; i >= solve_L; i--)
		{
			if(P[i] != i)
			{
				new_l = i;
				break;
			}
		}
		for(int i = r+1; i <= solve_R; i++)
		{
			if(P[i] != i)
			{
				new_r = i;
				break;
			}
		}

		if(new_l != -1 && new_r != -1)
		{
			if(S <= R[ cycle[new_l] ]) //&& L[ cycle[new_r] ] <= S
			{
				extra_cost += min(2*(new_r - r), 2*(l - new_l));
			}
			else
			{
				extra_cost += 2*(new_r - r) + 2*(l - new_l);
			}

			l = new_l;
			r = new_r;
			extend(l, r);
		}
		else if(new_l != -1)
		{
			extra_cost += 2*(l - new_l);
			l = new_l;
			extend(l, r);
		}
		else if(new_r != -1)
		{
			extra_cost += 2*(new_r - r);
			r = new_r;
			extend(l, r);
		}
		else break;
	}

	return essential_cost + extra_cost;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 11980 KB Output is correct
2 Correct 7 ms 11980 KB Output is correct
3 Correct 7 ms 11980 KB Output is correct
4 Correct 7 ms 11980 KB Output is correct
5 Correct 6 ms 11980 KB Output is correct
6 Correct 7 ms 11980 KB Output is correct
7 Correct 7 ms 11980 KB Output is correct
8 Correct 7 ms 11968 KB Output is correct
9 Correct 7 ms 12004 KB Output is correct
10 Correct 7 ms 11980 KB Output is correct
11 Correct 7 ms 11980 KB Output is correct
12 Correct 7 ms 11936 KB Output is correct
13 Correct 7 ms 12020 KB Output is correct
14 Correct 7 ms 11980 KB Output is correct
15 Correct 6 ms 11980 KB Output is correct
16 Correct 7 ms 11980 KB Output is correct
17 Correct 7 ms 11980 KB Output is correct
18 Correct 6 ms 11980 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 11980 KB Output is correct
2 Correct 7 ms 11980 KB Output is correct
3 Correct 7 ms 11980 KB Output is correct
4 Correct 7 ms 11980 KB Output is correct
5 Correct 6 ms 11980 KB Output is correct
6 Correct 7 ms 11980 KB Output is correct
7 Correct 7 ms 11980 KB Output is correct
8 Correct 7 ms 11968 KB Output is correct
9 Correct 7 ms 12004 KB Output is correct
10 Correct 7 ms 11980 KB Output is correct
11 Correct 7 ms 11980 KB Output is correct
12 Correct 7 ms 11936 KB Output is correct
13 Correct 7 ms 12020 KB Output is correct
14 Correct 7 ms 11980 KB Output is correct
15 Correct 6 ms 11980 KB Output is correct
16 Correct 7 ms 11980 KB Output is correct
17 Correct 7 ms 11980 KB Output is correct
18 Correct 6 ms 11980 KB Output is correct
19 Correct 7 ms 12028 KB Output is correct
20 Correct 7 ms 12088 KB Output is correct
21 Correct 7 ms 12040 KB Output is correct
22 Correct 7 ms 12060 KB Output is correct
23 Correct 7 ms 11980 KB Output is correct
24 Correct 7 ms 11976 KB Output is correct
25 Correct 7 ms 11984 KB Output is correct
26 Correct 7 ms 12144 KB Output is correct
27 Correct 8 ms 12044 KB Output is correct
28 Correct 7 ms 11980 KB Output is correct
29 Correct 7 ms 11988 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 11980 KB Output is correct
2 Correct 7 ms 11980 KB Output is correct
3 Correct 7 ms 11980 KB Output is correct
4 Correct 7 ms 11980 KB Output is correct
5 Correct 6 ms 11980 KB Output is correct
6 Correct 7 ms 11980 KB Output is correct
7 Correct 7 ms 11980 KB Output is correct
8 Correct 7 ms 11968 KB Output is correct
9 Correct 7 ms 12004 KB Output is correct
10 Correct 7 ms 11980 KB Output is correct
11 Correct 7 ms 11980 KB Output is correct
12 Correct 7 ms 11936 KB Output is correct
13 Correct 7 ms 12020 KB Output is correct
14 Correct 7 ms 11980 KB Output is correct
15 Correct 6 ms 11980 KB Output is correct
16 Correct 7 ms 11980 KB Output is correct
17 Correct 7 ms 11980 KB Output is correct
18 Correct 6 ms 11980 KB Output is correct
19 Correct 7 ms 12028 KB Output is correct
20 Correct 7 ms 12088 KB Output is correct
21 Correct 7 ms 12040 KB Output is correct
22 Correct 7 ms 12060 KB Output is correct
23 Correct 7 ms 11980 KB Output is correct
24 Correct 7 ms 11976 KB Output is correct
25 Correct 7 ms 11984 KB Output is correct
26 Correct 7 ms 12144 KB Output is correct
27 Correct 8 ms 12044 KB Output is correct
28 Correct 7 ms 11980 KB Output is correct
29 Correct 7 ms 11988 KB Output is correct
30 Correct 192 ms 23772 KB Output is correct
31 Correct 207 ms 23780 KB Output is correct
32 Correct 143 ms 23744 KB Output is correct
33 Correct 145 ms 23764 KB Output is correct
34 Correct 146 ms 23752 KB Output is correct
35 Correct 154 ms 23776 KB Output is correct
36 Correct 152 ms 23752 KB Output is correct
37 Correct 146 ms 23768 KB Output is correct
38 Correct 142 ms 23748 KB Output is correct
39 Correct 162 ms 23744 KB Output is correct
40 Correct 149 ms 23752 KB Output is correct
41 Correct 159 ms 23748 KB Output is correct
42 Correct 159 ms 23748 KB Output is correct
43 Correct 148 ms 23764 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 6 ms 11980 KB 3rd lines differ - on the 1st token, expected: '3304', found: '3974'
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 11980 KB Output is correct
2 Correct 7 ms 11980 KB Output is correct
3 Correct 7 ms 11980 KB Output is correct
4 Correct 7 ms 11980 KB Output is correct
5 Correct 6 ms 11980 KB Output is correct
6 Correct 7 ms 11980 KB Output is correct
7 Correct 7 ms 11980 KB Output is correct
8 Correct 7 ms 11968 KB Output is correct
9 Correct 7 ms 12004 KB Output is correct
10 Correct 7 ms 11980 KB Output is correct
11 Correct 7 ms 11980 KB Output is correct
12 Correct 7 ms 11936 KB Output is correct
13 Correct 7 ms 12020 KB Output is correct
14 Correct 7 ms 11980 KB Output is correct
15 Correct 6 ms 11980 KB Output is correct
16 Correct 7 ms 11980 KB Output is correct
17 Correct 7 ms 11980 KB Output is correct
18 Correct 6 ms 11980 KB Output is correct
19 Correct 7 ms 12028 KB Output is correct
20 Correct 7 ms 12088 KB Output is correct
21 Correct 7 ms 12040 KB Output is correct
22 Correct 7 ms 12060 KB Output is correct
23 Correct 7 ms 11980 KB Output is correct
24 Correct 7 ms 11976 KB Output is correct
25 Correct 7 ms 11984 KB Output is correct
26 Correct 7 ms 12144 KB Output is correct
27 Correct 8 ms 12044 KB Output is correct
28 Correct 7 ms 11980 KB Output is correct
29 Correct 7 ms 11988 KB Output is correct
30 Correct 192 ms 23772 KB Output is correct
31 Correct 207 ms 23780 KB Output is correct
32 Correct 143 ms 23744 KB Output is correct
33 Correct 145 ms 23764 KB Output is correct
34 Correct 146 ms 23752 KB Output is correct
35 Correct 154 ms 23776 KB Output is correct
36 Correct 152 ms 23752 KB Output is correct
37 Correct 146 ms 23768 KB Output is correct
38 Correct 142 ms 23748 KB Output is correct
39 Correct 162 ms 23744 KB Output is correct
40 Correct 149 ms 23752 KB Output is correct
41 Correct 159 ms 23748 KB Output is correct
42 Correct 159 ms 23748 KB Output is correct
43 Correct 148 ms 23764 KB Output is correct
44 Incorrect 6 ms 11980 KB 3rd lines differ - on the 1st token, expected: '3304', found: '3974'
45 Halted 0 ms 0 KB -