답안 #152382

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
152382 2019-09-07T20:37:15 Z qkxwsm 고대 책들 (IOI17_books) C++14
100 / 100
253 ms 30712 KB
#include <bits/stdc++.h>
 
using namespace std;
 
template<class T, class U>
void ckmin(T &a, U b)
{
	if (a > b) a = b;
}
template<class T, class U>
void ckmax(T &a, U b)
{
	if (a < b) a = b;
}
 
#define MP make_pair
#define PB push_back
#define LB lower_bound
#define UB upper_bound
#define fi first
#define se second
#define FOR(i, a, b) for (auto i = (a); i < (b); i++)
#define FORD(i, a, b) for (auto i = (a) - 1; i >= (b); i--)
#define SZ(x) ((int) ((x).size()))
#define ALL(x) (x).begin(), (x).end()
#define MAXN 1000013
 
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vpi;
typedef vector<pll> vpl;
 
int N, S;
pii need = {-1, -1}, cur;
int dsu[MAXN];
pii range[MAXN];
vi arr;
ll ans;
 
int get(int u)
{
	return (u == dsu[u] ? u : dsu[u] = get(dsu[u]));
}
bool merge(int u, int v)
{
	u = get(u); v = get(v);
	if (u == v) return false;
	dsu[v] = u;
	ckmin(range[u].fi, range[v].fi);
	ckmax(range[u].se, range[v].se);
	return true;
}
 
ll minimum_walk(vi perm, int s)
{
	//mark all the blocks
	N = SZ(perm); S = s; arr = perm;
	FOR(i, 0, N)
	{
		dsu[i] = i;
		range[i] = {i, i};
	}
	FOR(i, 0, N)
	{
		merge(i, arr[i]);
	}
	//so we have access to (l..r).
	//what do we do? we constantly try to "expand" acess until we covered the entire needy range
	FOR(i, 0, N)
	{
		if (arr[i] != i)
		{
			need.fi = i;
			break;
		}
	}
	FORD(i, N, 0)
	{
		if (arr[i] != i)
		{
			need.se = i;
			break;
		}
	}
	if (need.fi == -1) return 0ll;
	ckmin(need.fi, S);
	ckmax(need.se, S);
	//implement the extend function? so for (l..r) you wanna be able to say basically the entire range that you get for free.
	int lt = range[get(S)].fi, rt = range[get(S)].se;
	cur = {S, S};
	while(cur.fi > lt || cur.se < rt)
	{
		for (int i = cur.fi - 1; i >= lt; i--)
		{
			ckmin(lt, range[get(i)].fi);
			ckmax(rt, range[get(i)].se);
		}
		cur.fi = lt;
		for (int i = cur.se + 1; i <= rt; i++)
		{
			ckmin(lt, range[get(i)].fi);
			ckmax(rt, range[get(i)].se);
		}
		cur.se = rt;
	}
	// cerr << cur.fi << ' ' << cur.se << ' ' << need.fi << ' ' << need.se << endl;
	while(cur != need)
	{
		//you need to expand the rnage at minimum cost.
		//find the first range you get
		ll lc = 0, rc = 0;
		int bound = cur.fi;
		for (lt = cur.fi - 1; lt >= need.fi; lt--)
		{
			//see if you get a new guy.
			if (bound == lt + 1) lc += 2;
			pii p = range[get(lt)];
			if (p.se > cur.se) break;
			ckmin(bound, p.fi);
		}
		bound = cur.se;
		for (rt = cur.se + 1; rt <= need.se; rt++)
		{
			if (bound == rt - 1) rc += 2;
			pii p = range[get(rt)];
			if (p.fi < cur.fi) break;
			ckmax(bound, p.se);
		}
		// cerr << "lc " << lc << " rc " << rc << endl;
		if (lt == need.fi - 1)
		{
			assert(rt == need.se + 1);
			ans += lc;
			ans += rc;
			cur = need;
			break;
		}
		ans += min(lc, rc);
		//ok now expand the range.
		while(cur.fi > lt || cur.se < rt)
		{
			for (int i = cur.fi - 1; i >= lt; i--)
			{
				ckmin(lt, range[get(i)].fi);
				ckmax(rt, range[get(i)].se);
			}
			cur.fi = lt;
			for (int i = cur.se + 1; i <= rt; i++)
			{
				ckmin(lt, range[get(i)].fi);
				ckmax(rt, range[get(i)].se);
			}
			cur.se = rt;
		}
	}
	FOR(i, 0, N)
	{
		ans += abs(arr[i] - i);
	}
	return ans;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 128 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 376 KB Output is correct
7 Correct 2 ms 380 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 252 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 376 KB Output is correct
17 Correct 2 ms 376 KB Output is correct
18 Correct 2 ms 376 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 128 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 376 KB Output is correct
7 Correct 2 ms 380 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 252 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 376 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 376 KB Output is correct
20 Correct 2 ms 376 KB Output is correct
21 Correct 2 ms 380 KB Output is correct
22 Correct 2 ms 504 KB Output is correct
23 Correct 2 ms 376 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 3 ms 376 KB Output is correct
28 Correct 2 ms 380 KB Output is correct
29 Correct 3 ms 376 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 128 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 376 KB Output is correct
7 Correct 2 ms 380 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 252 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 376 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 376 KB Output is correct
20 Correct 2 ms 376 KB Output is correct
21 Correct 2 ms 380 KB Output is correct
22 Correct 2 ms 504 KB Output is correct
23 Correct 2 ms 376 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 3 ms 376 KB Output is correct
28 Correct 2 ms 380 KB Output is correct
29 Correct 3 ms 376 KB Output is correct
30 Correct 252 ms 25736 KB Output is correct
31 Correct 253 ms 25592 KB Output is correct
32 Correct 146 ms 25464 KB Output is correct
33 Correct 162 ms 25564 KB Output is correct
34 Correct 162 ms 25416 KB Output is correct
35 Correct 167 ms 25596 KB Output is correct
36 Correct 174 ms 25416 KB Output is correct
37 Correct 185 ms 25592 KB Output is correct
38 Correct 198 ms 25468 KB Output is correct
39 Correct 192 ms 25488 KB Output is correct
40 Correct 204 ms 25420 KB Output is correct
41 Correct 232 ms 25564 KB Output is correct
42 Correct 229 ms 25676 KB Output is correct
43 Correct 164 ms 25464 KB Output is correct
# 결과 실행 시간 메모리 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 376 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 376 KB Output is correct
16 Correct 2 ms 376 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 376 KB Output is correct
20 Correct 2 ms 376 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 128 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 376 KB Output is correct
7 Correct 2 ms 380 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 252 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 376 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 376 KB Output is correct
20 Correct 2 ms 376 KB Output is correct
21 Correct 2 ms 380 KB Output is correct
22 Correct 2 ms 504 KB Output is correct
23 Correct 2 ms 376 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 3 ms 376 KB Output is correct
28 Correct 2 ms 380 KB Output is correct
29 Correct 3 ms 376 KB Output is correct
30 Correct 252 ms 25736 KB Output is correct
31 Correct 253 ms 25592 KB Output is correct
32 Correct 146 ms 25464 KB Output is correct
33 Correct 162 ms 25564 KB Output is correct
34 Correct 162 ms 25416 KB Output is correct
35 Correct 167 ms 25596 KB Output is correct
36 Correct 174 ms 25416 KB Output is correct
37 Correct 185 ms 25592 KB Output is correct
38 Correct 198 ms 25468 KB Output is correct
39 Correct 192 ms 25488 KB Output is correct
40 Correct 204 ms 25420 KB Output is correct
41 Correct 232 ms 25564 KB Output is correct
42 Correct 229 ms 25676 KB Output is correct
43 Correct 164 ms 25464 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 376 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 376 KB Output is correct
59 Correct 2 ms 376 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 376 KB Output is correct
63 Correct 2 ms 376 KB Output is correct
64 Correct 172 ms 30512 KB Output is correct
65 Correct 165 ms 30584 KB Output is correct
66 Correct 202 ms 30608 KB Output is correct
67 Correct 194 ms 30456 KB Output is correct
68 Correct 20 ms 3192 KB Output is correct
69 Correct 21 ms 3320 KB Output is correct
70 Correct 19 ms 3192 KB Output is correct
71 Correct 18 ms 3196 KB Output is correct
72 Correct 17 ms 3192 KB Output is correct
73 Correct 21 ms 3192 KB Output is correct
74 Correct 160 ms 30548 KB Output is correct
75 Correct 161 ms 30548 KB Output is correct
76 Correct 165 ms 30456 KB Output is correct
77 Correct 165 ms 30456 KB Output is correct
78 Correct 173 ms 30508 KB Output is correct
79 Correct 173 ms 30712 KB Output is correct
80 Correct 147 ms 30508 KB Output is correct
81 Correct 159 ms 30584 KB Output is correct
82 Correct 158 ms 30548 KB Output is correct
83 Correct 180 ms 30588 KB Output is correct
84 Correct 181 ms 30584 KB Output is correct
85 Correct 184 ms 30584 KB Output is correct
86 Correct 222 ms 30584 KB Output is correct
87 Correct 201 ms 30628 KB Output is correct
88 Correct 206 ms 30560 KB Output is correct
89 Correct 211 ms 30576 KB Output is correct
90 Correct 247 ms 30588 KB Output is correct
91 Correct 242 ms 30608 KB Output is correct
92 Correct 231 ms 30592 KB Output is correct