답안 #838232

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
838232 2023-08-26T11:41:41 Z Johann 고대 책들 (IOI17_books) C++14
50 / 100
153 ms 28028 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)
	{
		int virtualL = min(maxL[l], maxL[maxL[r]]);
		if (max(maxR[l], maxR[r]) < R || virtualL > 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 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 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 1 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 1 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 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 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 1 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 1 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 0 ms 212 KB Output is correct
22 Correct 1 ms 212 KB Output is correct
23 Correct 1 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 0 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 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 1 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 1 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 0 ms 212 KB Output is correct
22 Correct 1 ms 212 KB Output is correct
23 Correct 1 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 0 ms 212 KB Output is correct
30 Correct 153 ms 23776 KB Output is correct
31 Correct 144 ms 23776 KB Output is correct
32 Correct 82 ms 23668 KB Output is correct
33 Correct 91 ms 27992 KB Output is correct
34 Correct 98 ms 28028 KB Output is correct
35 Correct 107 ms 27996 KB Output is correct
36 Correct 91 ms 27996 KB Output is correct
37 Correct 90 ms 24392 KB Output is correct
38 Correct 90 ms 23924 KB Output is correct
39 Correct 95 ms 23884 KB Output is correct
40 Correct 124 ms 23792 KB Output is correct
41 Correct 109 ms 23668 KB Output is correct
42 Correct 101 ms 23672 KB Output is correct
43 Correct 97 ms 28012 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Incorrect 0 ms 340 KB 3rd lines differ - on the 1st token, expected: '10478', found: '10494'
9 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 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 1 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 1 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 0 ms 212 KB Output is correct
22 Correct 1 ms 212 KB Output is correct
23 Correct 1 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 0 ms 212 KB Output is correct
30 Correct 153 ms 23776 KB Output is correct
31 Correct 144 ms 23776 KB Output is correct
32 Correct 82 ms 23668 KB Output is correct
33 Correct 91 ms 27992 KB Output is correct
34 Correct 98 ms 28028 KB Output is correct
35 Correct 107 ms 27996 KB Output is correct
36 Correct 91 ms 27996 KB Output is correct
37 Correct 90 ms 24392 KB Output is correct
38 Correct 90 ms 23924 KB Output is correct
39 Correct 95 ms 23884 KB Output is correct
40 Correct 124 ms 23792 KB Output is correct
41 Correct 109 ms 23668 KB Output is correct
42 Correct 101 ms 23672 KB Output is correct
43 Correct 97 ms 28012 KB Output is correct
44 Correct 0 ms 340 KB Output is correct
45 Correct 0 ms 340 KB Output is correct
46 Correct 0 ms 340 KB Output is correct
47 Correct 0 ms 340 KB Output is correct
48 Correct 1 ms 340 KB Output is correct
49 Correct 1 ms 340 KB Output is correct
50 Correct 0 ms 340 KB Output is correct
51 Incorrect 0 ms 340 KB 3rd lines differ - on the 1st token, expected: '10478', found: '10494'
52 Halted 0 ms 0 KB -