답안 #838230

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
838230 2023-08-26T11:39:01 Z Johann 고대 책들 (IOI17_books) C++14
50 / 100
203 ms 28008 KB
#include "books.h"
#include "bits/stdc++.h"
using namespace std;

typedef long long ll;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef pair<ll, ll> pii;
typedef vector<pii> vpii;
typedef vector<vpii> vvpii;
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()

const ll INF = 1LL << 60;
int N;
ll safecost;
vi vis, P;

void cycle(int i, int &mini, int &maxi, int cycleCnt)
{
	if (vis[i] != -1)
		return;
	mini = min(mini, i);
	maxi = max(maxi, i);
	vis[i] = cycleCnt;
	safecost += (ll)abs(P[i] - i);
	cycle(P[i], mini, maxi, cycleCnt);
}

long long minimum_walk(std::vector<int> p, int s)
{
	N = sz(p);
	P.resize(N);
	for (int i = 0; i < N; ++i)
		P[i] = p[i];

	vis.assign(N, -1);
	ll L = INF, R = -1;
	safecost = 0;
	vpii borders;
	for (int i = 0, mini, maxi; i < N; ++i)
	{
		if (P[i] != i)
			L = min(L, (ll)i), R = max(R, (ll)i);
		if (vis[i] == -1 && P[i] != i)
		{
			mini = maxi = i;
			cycle(i, mini, maxi, sz(borders));
			borders.push_back({mini, maxi});
		}
	}
	if (borders.empty())
		return 0;
	if (s <= L || s >= R)
	{
		if (s >= R)
		{
			s = N - s;
			for (int i = 0, l, r; i < sz(borders); ++i)
			{
				tie(l, r) = borders[i];
				borders[i] = {N - r, N - l};
			}
		}
		sort(all(borders));

		ll pos = s;
		for (pii b : borders)
		{
			safecost += 2 * max(0LL, b.first - pos);
			pos = max(pos, b.second);
		}

		return safecost;
	}

	vi maxL(N, s), maxR(N, s), cost(N, 0);
	if (vis[s] != -1)
		tie(maxL[s], maxR[s]) = borders[vis[s]];
	for (int i = s + 1; i < N; ++i)
	{
		maxL[i] = maxL[i - 1], maxR[i] = maxR[i - 1], cost[i] = cost[i - 1];
		if (maxR[i] < i)
			cost[i] += 2, maxR[i] = i;
		if (vis[i] != -1)
			maxL[i] = min(maxL[i], borders[vis[i]].first),
			maxR[i] = max(maxR[i], borders[vis[i]].second);
	}
	for (int i = s - 1; i >= 0; --i)
	{
		maxL[i] = maxL[i + 1], maxR[i] = maxR[i + 1], cost[i] = cost[i + 1];
		if (maxL[i] > i)
			cost[i] += 2, maxL[i] = i;
		if (vis[i] != -1)
			maxL[i] = min(maxL[i], borders[vis[i]].first),
			maxR[i] = max(maxR[i], borders[vis[i]].second);
	}

	ll mincost = INF;
	int l = s, r = N - 1;
	while (l >= 0 && r >= 0)
	{
		if (max(maxR[l], maxR[r]) < R || min(maxL[l], maxL[r]) > L)
			--l;
		else
		{
			ll tmp = safecost + cost[r];
			if (l < maxL[r])
				tmp += cost[l] - cost[maxL[r]];
			mincost = min(mincost, tmp);
			--r;
		}
	}

	return mincost;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 0 ms 212 KB Output is correct
23 Correct 0 ms 212 KB Output is correct
24 Correct 0 ms 212 KB Output is correct
25 Correct 0 ms 212 KB Output is correct
26 Correct 0 ms 212 KB Output is correct
27 Correct 1 ms 212 KB Output is correct
28 Correct 0 ms 212 KB Output is correct
29 Correct 1 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 0 ms 212 KB Output is correct
23 Correct 0 ms 212 KB Output is correct
24 Correct 0 ms 212 KB Output is correct
25 Correct 0 ms 212 KB Output is correct
26 Correct 0 ms 212 KB Output is correct
27 Correct 1 ms 212 KB Output is correct
28 Correct 0 ms 212 KB Output is correct
29 Correct 1 ms 212 KB Output is correct
30 Correct 203 ms 23780 KB Output is correct
31 Correct 153 ms 23800 KB Output is correct
32 Correct 86 ms 23756 KB Output is correct
33 Correct 89 ms 27988 KB Output is correct
34 Correct 90 ms 27992 KB Output is correct
35 Correct 93 ms 27996 KB Output is correct
36 Correct 106 ms 28008 KB Output is correct
37 Correct 86 ms 24424 KB Output is correct
38 Correct 91 ms 23920 KB Output is correct
39 Correct 87 ms 23932 KB Output is correct
40 Correct 103 ms 23784 KB Output is correct
41 Correct 103 ms 23788 KB Output is correct
42 Correct 128 ms 23744 KB Output is correct
43 Correct 98 ms 27992 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 300 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 304 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 300 KB Output is correct
8 Incorrect 1 ms 340 KB 3rd lines differ - on the 1st token, expected: '10478', found: '10494'
9 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 0 ms 212 KB Output is correct
23 Correct 0 ms 212 KB Output is correct
24 Correct 0 ms 212 KB Output is correct
25 Correct 0 ms 212 KB Output is correct
26 Correct 0 ms 212 KB Output is correct
27 Correct 1 ms 212 KB Output is correct
28 Correct 0 ms 212 KB Output is correct
29 Correct 1 ms 212 KB Output is correct
30 Correct 203 ms 23780 KB Output is correct
31 Correct 153 ms 23800 KB Output is correct
32 Correct 86 ms 23756 KB Output is correct
33 Correct 89 ms 27988 KB Output is correct
34 Correct 90 ms 27992 KB Output is correct
35 Correct 93 ms 27996 KB Output is correct
36 Correct 106 ms 28008 KB Output is correct
37 Correct 86 ms 24424 KB Output is correct
38 Correct 91 ms 23920 KB Output is correct
39 Correct 87 ms 23932 KB Output is correct
40 Correct 103 ms 23784 KB Output is correct
41 Correct 103 ms 23788 KB Output is correct
42 Correct 128 ms 23744 KB Output is correct
43 Correct 98 ms 27992 KB Output is correct
44 Correct 0 ms 340 KB Output is correct
45 Correct 1 ms 300 KB Output is correct
46 Correct 0 ms 340 KB Output is correct
47 Correct 1 ms 304 KB Output is correct
48 Correct 1 ms 340 KB Output is correct
49 Correct 1 ms 340 KB Output is correct
50 Correct 1 ms 300 KB Output is correct
51 Incorrect 1 ms 340 KB 3rd lines differ - on the 1st token, expected: '10478', found: '10494'
52 Halted 0 ms 0 KB -