답안 #131692

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
131692 2019-07-17T12:59:22 Z youngyojun Cultivation (JOI17_cultivation) C++11
100 / 100
160 ms 2428 KB
#include <bits/stdc++.h>
#define eb emplace_back
#define sz(V) ((int)(V).size())
#define allv(V) ((V).begin()),((V).end())
#define sorv(V) sort(allv(V))
#define univ(V) (V).erase(unique(allv(V)),(V).end())
#define upmin(a,b) (a)=min((a),(b))
#define upmax(a,b) (a)=max((a),(b))
#define INF (0x3f3f3f3f)
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;

const int MAXL = 90055;

vector<int> CHX[305], CHU[305], CHD[305], CHS[305];
int DU[MAXL], DD[MAXL], DS[MAXL];

vector<int> HLX, BulX, XV;
int A[305], B[305];

int H, W, N, K, L, Ans = INF*2;

void makeChain(vector<pii> &EV, vector<int> &X, vector<int> &U, vector<int> &D, vector<int> &S) {
	sorv(EV);
	multiset<int> YV, DV;
	YV.insert(0); YV.insert(H+1);
	DV.insert(H+1);
	for(int s = 0, e, n = sz(EV), px; s < n; s = e) {
		px = EV[s].first;
		for(e = s+1; e < n && EV[e].first == px; e++);
		for(int oi = s, y; oi < e; oi++) {
			y = EV[oi].second;
			auto it = YV.insert(y);
			if(YV.begin() != it && YV.end() != next(it))
				DV.erase(DV.find(*next(it) - *prev(it)));
			if(YV.begin() != it) DV.insert(y - *prev(it));
			if(YV.end() != next(it)) DV.insert(*next(it) - y);
		}
		X.eb(px);
		S.eb(*prev(DV.end())-1);
		U.eb(*next(YV.begin())-1);
		D.eb(H-*prev(prev(YV.end())));
	}
}

int main() {
	ios::sync_with_stdio(false);

	cin >> H >> W >> N;
	for(int i = 1; i <= N; i++) {
		cin >> A[i] >> B[i];
		HLX.eb(B[i]-1);
		if(1 < B[i]) BulX.eb(B[i]-1);
	}
	sorv(HLX); univ(HLX);
	sorv(BulX); univ(BulX);

	K = sz(BulX); BulX.eb(W);
	for(int idx = 0, x; idx <= K; idx++) {
		x = BulX[idx];
		vector<pii> EV;
		for(int i = 1, t; i <= N; i++) {
			t = x - B[i];
			if(0 <= t) {
				EV.eb(t, A[i]);
				XV.eb(t);
			}
		}
		makeChain(EV, CHX[idx], CHU[idx], CHD[idx], CHS[idx]);
	}

	sorv(XV); univ(XV); L = sz(XV);
	for(int hlxi = sz(HLX), buli = K-1, lx; hlxi--;) {
		lx = HLX[hlxi];
		for(; 0 <= buli && lx < BulX[buli]; buli--) {
			for(int i = 0, j = 0, n = sz(CHX[buli]), x; i < L; i++) {
				x = XV[i];
				if(j < n && CHX[buli][j] <= x) j++;
				if(j) {
					upmax(DU[i], CHU[buli][j-1]);
					upmax(DD[i], CHD[buli][j-1]);
					upmax(DS[i], CHS[buli][j-1]);
				} else DU[i] = DD[i] = DS[i] = INF;
			}
		}
		for(int i = 0, j = 0, n = sz(CHX[K]), rx, du, dd, ds; i < L || j < n;) {
			rx = INF;
			if(i < L) rx = XV[i]-lx;
			if(j < n) upmin(rx, CHX[K][j]);
			if(rx < 0) { i++; continue; }
			if(i < L && XV[i]-lx == rx) i++;
			if(j < n && CHX[K][j] == rx) j++;
			if(!i || !j) continue;
			du = max(DU[i-1], CHU[K][j-1]);
			dd = max(DD[i-1], CHD[K][j-1]);
			ds = max(DS[i-1], CHS[K][j-1]);
			ll ret = ll(max(ds, du + dd)) + lx + rx;
			if(ret < Ans) Ans = ret;
		}
	}

	cout << Ans << endl;
	return 0;
}
# 결과 실행 시간 메모리 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
# 결과 실행 시간 메모리 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 380 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 376 KB Output is correct
22 Correct 4 ms 376 KB Output is correct
23 Correct 2 ms 376 KB Output is correct
24 Correct 5 ms 504 KB Output is correct
25 Correct 4 ms 380 KB Output is correct
26 Correct 6 ms 504 KB Output is correct
27 Correct 6 ms 504 KB Output is correct
28 Correct 5 ms 504 KB Output is correct
29 Correct 6 ms 504 KB Output is correct
30 Correct 6 ms 504 KB Output is correct
31 Correct 6 ms 504 KB Output is correct
32 Correct 6 ms 504 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 380 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 376 KB Output is correct
22 Correct 4 ms 376 KB Output is correct
23 Correct 2 ms 376 KB Output is correct
24 Correct 5 ms 504 KB Output is correct
25 Correct 4 ms 380 KB Output is correct
26 Correct 6 ms 504 KB Output is correct
27 Correct 6 ms 504 KB Output is correct
28 Correct 5 ms 504 KB Output is correct
29 Correct 6 ms 504 KB Output is correct
30 Correct 6 ms 504 KB Output is correct
31 Correct 6 ms 504 KB Output is correct
32 Correct 6 ms 504 KB Output is correct
33 Correct 147 ms 2328 KB Output is correct
34 Correct 148 ms 2260 KB Output is correct
35 Correct 29 ms 1272 KB Output is correct
36 Correct 155 ms 2292 KB Output is correct
37 Correct 155 ms 2360 KB Output is correct
38 Correct 156 ms 2376 KB Output is correct
39 Correct 128 ms 2284 KB Output is correct
40 Correct 154 ms 2248 KB Output is correct
41 Correct 132 ms 2136 KB Output is correct
42 Correct 31 ms 1528 KB Output is correct
43 Correct 160 ms 2312 KB Output is correct
44 Correct 39 ms 1144 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 452 KB Output is correct
15 Correct 3 ms 376 KB Output is correct
16 Correct 3 ms 376 KB Output is correct
17 Correct 3 ms 376 KB Output is correct
18 Correct 2 ms 376 KB Output is correct
19 Correct 2 ms 380 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 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 452 KB Output is correct
15 Correct 3 ms 376 KB Output is correct
16 Correct 3 ms 376 KB Output is correct
17 Correct 3 ms 376 KB Output is correct
18 Correct 2 ms 376 KB Output is correct
19 Correct 2 ms 380 KB Output is correct
20 Correct 2 ms 376 KB Output is correct
21 Correct 9 ms 632 KB Output is correct
22 Correct 11 ms 632 KB Output is correct
23 Correct 10 ms 636 KB Output is correct
24 Correct 9 ms 632 KB Output is correct
25 Correct 11 ms 632 KB Output is correct
26 Correct 5 ms 504 KB Output is correct
27 Correct 10 ms 632 KB Output is correct
28 Correct 11 ms 632 KB Output is correct
29 Correct 11 ms 632 KB Output is correct
30 Correct 11 ms 632 KB Output is correct
31 Correct 10 ms 632 KB Output is correct
32 Correct 10 ms 632 KB Output is correct
33 Correct 10 ms 632 KB Output is correct
34 Correct 10 ms 632 KB Output is correct
35 Correct 9 ms 632 KB Output is correct
36 Correct 11 ms 636 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 380 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 376 KB Output is correct
22 Correct 4 ms 376 KB Output is correct
23 Correct 2 ms 376 KB Output is correct
24 Correct 5 ms 504 KB Output is correct
25 Correct 4 ms 380 KB Output is correct
26 Correct 6 ms 504 KB Output is correct
27 Correct 6 ms 504 KB Output is correct
28 Correct 5 ms 504 KB Output is correct
29 Correct 6 ms 504 KB Output is correct
30 Correct 6 ms 504 KB Output is correct
31 Correct 6 ms 504 KB Output is correct
32 Correct 6 ms 504 KB Output is correct
33 Correct 147 ms 2328 KB Output is correct
34 Correct 148 ms 2260 KB Output is correct
35 Correct 29 ms 1272 KB Output is correct
36 Correct 155 ms 2292 KB Output is correct
37 Correct 155 ms 2360 KB Output is correct
38 Correct 156 ms 2376 KB Output is correct
39 Correct 128 ms 2284 KB Output is correct
40 Correct 154 ms 2248 KB Output is correct
41 Correct 132 ms 2136 KB Output is correct
42 Correct 31 ms 1528 KB Output is correct
43 Correct 160 ms 2312 KB Output is correct
44 Correct 39 ms 1144 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 452 KB Output is correct
59 Correct 3 ms 376 KB Output is correct
60 Correct 3 ms 376 KB Output is correct
61 Correct 3 ms 376 KB Output is correct
62 Correct 2 ms 376 KB Output is correct
63 Correct 2 ms 380 KB Output is correct
64 Correct 2 ms 376 KB Output is correct
65 Correct 9 ms 632 KB Output is correct
66 Correct 11 ms 632 KB Output is correct
67 Correct 10 ms 636 KB Output is correct
68 Correct 9 ms 632 KB Output is correct
69 Correct 11 ms 632 KB Output is correct
70 Correct 5 ms 504 KB Output is correct
71 Correct 10 ms 632 KB Output is correct
72 Correct 11 ms 632 KB Output is correct
73 Correct 11 ms 632 KB Output is correct
74 Correct 11 ms 632 KB Output is correct
75 Correct 10 ms 632 KB Output is correct
76 Correct 10 ms 632 KB Output is correct
77 Correct 10 ms 632 KB Output is correct
78 Correct 10 ms 632 KB Output is correct
79 Correct 9 ms 632 KB Output is correct
80 Correct 11 ms 636 KB Output is correct
81 Correct 68 ms 1656 KB Output is correct
82 Correct 68 ms 1400 KB Output is correct
83 Correct 88 ms 1656 KB Output is correct
84 Correct 93 ms 1784 KB Output is correct
85 Correct 158 ms 2420 KB Output is correct
86 Correct 156 ms 2292 KB Output is correct
87 Correct 110 ms 1840 KB Output is correct
88 Correct 153 ms 2292 KB Output is correct
89 Correct 140 ms 2420 KB Output is correct
90 Correct 108 ms 1908 KB Output is correct
91 Correct 133 ms 2036 KB Output is correct
92 Correct 159 ms 2420 KB Output is correct
93 Correct 141 ms 2388 KB Output is correct
94 Correct 133 ms 2292 KB Output is correct
95 Correct 158 ms 2420 KB Output is correct
96 Correct 149 ms 2428 KB Output is correct
97 Correct 104 ms 2232 KB Output is correct
98 Correct 42 ms 1908 KB Output is correct