Submission #672193

# Submission time Handle Problem Language Result Execution time Memory
672193 2022-12-15T02:46:16 Z amunduzbaev Cultivation (JOI17_cultivation) C++17
80 / 100
1226 ms 1320 KB
#include "bits/stdc++.h"
using namespace std;

#define ar array
typedef long long ll;
#define int ll

signed main(){
	ios::sync_with_stdio(0); cin.tie(0);
	
	int n, m; cin >> n >> m;
	int sz; cin >> sz;
	vector<int> x(sz), y(sz), t;
	for(int i=0;i<sz;i++){
		cin >> x[i] >> y[i];
	} 
	
	if(sz <= 100){
		t = x;
		sort(t.begin(), t.end());
		t.erase(unique(t.begin(), t.end()), t.end());
		vector<int> used(t.size());
		
		vector<int> id(sz);
		for(int i=0;i<sz;i++){
			id[i] = lower_bound(t.begin(), t.end(), x[i]) - t.begin() + 1;
		}
		
		int res = 2 * (n + m);
		set<int> don;
		auto check = [&](int d){
			if(don.count(d)) return;
			don.insert(d);
			vector<ar<int, 2>> tot;
			for(int i=0;i<sz;i++){
				//~ cout<<max(1LL, y[i] - w)<<" "<<min(y[i] + e, m)<<"\n";
				tot.push_back({y[i], id[i]});
				tot.push_back({y[i] + d + 1, -id[i]});
			}
			sort(tot.begin(), tot.end());
			vector<int> v, is;
			vector<ar<int, 3>> Mn;
			for(int i=0;i<(int)tot.size();){
				int j = i;
				while(j < (int)tot.size() && tot[j][0] == tot[i][0]){
					if(tot[j][1] > 0) used[tot[j][1] - 1]++;
					else used[-tot[j][1] - 1]--;
					j++;
				}
				
				//~ cout<<tot[i][0]<<" : ";
				int mnl = 0, mn = 0;
				int last = -1;
				for(int i=0;i<(int)t.size();i++){
					if(!used[i]) continue;
					if(~last) mn = max(mn, t[i] - t[last] - 1);
					else mnl = t[i] - 1;
					last = i;
				}
				v.push_back(tot[i][0]);
				if(last == -1){
					Mn.push_back({0, 0, 0});
					is.push_back(1);
				} else {
					Mn.push_back({mnl, n - t[last], mn});
					is.push_back(0);
				}
				
				i = j;
			}
			for(int i=0;i + 1 < (int)v.size();i++){
				int mnl = 0, mnr = 0, mn = 0;
				for(int j=i;j + 1 < (int)v.size();j++){
					if(is[j]) break;
					mnl = max(mnl, Mn[j][0]);
					mnr = max(mnr, Mn[j][1]);
					mn = max(mn, Mn[j][2]);
					if(v[j + 1] - v[i] >= m){
						res = min(res, d + max(mnl + mnr, mn));
					}
				}
			}
		};
		
		for(int i=0;i<sz;i++){
			for(int j=0;j<sz;j++){
				check(y[i] - 1 + m - y[j]);
			}
			
			for(int k=0;k<sz;k++){
				if(y[i] >= y[k]) continue;
				check(y[k] - y[i] - 1);
			}
		}
		
		cout<<res<<"\n";
	} else {
		vector<int> p(sz);
		iota(p.begin(), p.end(), 0);
		sort(p.begin(), p.end(), [&](int i, int j){
			return y[i] < y[j];
		});
		
		int res = 2 * (n + m);
		for(int u=0;u<n;u++){
			for(int d=0;d<n;d++){
				vector<vector<int>> used(n + 1);
				for(auto i : p){
					int l = max(1ll, x[i] - u), r = min(n, x[i] + d);
					for(int j=l;j<=r;j++){
						used[j].push_back(y[i]);
					}
				}
				
				int mnl = 0, mnr = 0, mn = 0, ok = 1;
				for(int i=1;i<=n;i++){
					if(used[i].empty()){
						ok = 0;
						break;
					}
					mnl = max(mnl, used[i][0] - 1);
					mnr = max(mnr, m - used[i].back());
					for(int j=1;j<(int)used[i].size();j++){
						//~ cout<<i<<" "<<used[i][j]<<" "<<used[i][j-1]<<"\n";
						mn = max(mn, used[i][j] - used[i][j-1] - 1);
					}
				}
				
				if(!ok) continue;
				//~ cout<<u<<" "<<d<<" "<<mnl<<" "<<mnr<<" "<<mn<<"\n";
				res = min(res, u + d + max(mnl + mnr, mn));
			}
		}
		
		cout<<res<<"\n";
	}
}
# Verdict Execution time Memory Grader output
1 Correct 0 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 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 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
# Verdict Execution time Memory Grader output
1 Correct 0 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 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 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 1 ms 288 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 55 ms 380 KB Output is correct
23 Correct 1 ms 212 KB Output is correct
24 Correct 75 ms 432 KB Output is correct
25 Correct 66 ms 420 KB Output is correct
26 Correct 102 ms 520 KB Output is correct
27 Correct 98 ms 516 KB Output is correct
28 Correct 69 ms 340 KB Output is correct
29 Correct 85 ms 512 KB Output is correct
30 Correct 99 ms 520 KB Output is correct
31 Correct 100 ms 524 KB Output is correct
32 Correct 100 ms 512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 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 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 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 1 ms 288 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 55 ms 380 KB Output is correct
23 Correct 1 ms 212 KB Output is correct
24 Correct 75 ms 432 KB Output is correct
25 Correct 66 ms 420 KB Output is correct
26 Correct 102 ms 520 KB Output is correct
27 Correct 98 ms 516 KB Output is correct
28 Correct 69 ms 340 KB Output is correct
29 Correct 85 ms 512 KB Output is correct
30 Correct 99 ms 520 KB Output is correct
31 Correct 100 ms 524 KB Output is correct
32 Correct 100 ms 512 KB Output is correct
33 Correct 1 ms 340 KB Output is correct
34 Correct 91 ms 536 KB Output is correct
35 Correct 97 ms 528 KB Output is correct
36 Correct 100 ms 516 KB Output is correct
37 Correct 66 ms 496 KB Output is correct
38 Correct 103 ms 524 KB Output is correct
39 Correct 96 ms 524 KB Output is correct
40 Correct 100 ms 532 KB Output is correct
41 Correct 83 ms 520 KB Output is correct
42 Correct 87 ms 504 KB Output is correct
43 Correct 95 ms 512 KB Output is correct
44 Correct 99 ms 528 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 340 KB Output is correct
2 Correct 6 ms 340 KB Output is correct
3 Correct 7 ms 464 KB Output is correct
4 Correct 7 ms 340 KB Output is correct
5 Correct 7 ms 352 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 6 ms 340 KB Output is correct
8 Correct 7 ms 352 KB Output is correct
9 Correct 5 ms 340 KB Output is correct
10 Correct 10 ms 352 KB Output is correct
11 Correct 8 ms 356 KB Output is correct
12 Correct 7 ms 360 KB Output is correct
13 Correct 2 ms 212 KB Output is correct
14 Correct 4 ms 340 KB Output is correct
15 Correct 2 ms 212 KB Output is correct
16 Correct 7 ms 348 KB Output is correct
17 Correct 7 ms 340 KB Output is correct
18 Correct 7 ms 360 KB Output is correct
19 Correct 7 ms 340 KB Output is correct
20 Correct 7 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 340 KB Output is correct
2 Correct 6 ms 340 KB Output is correct
3 Correct 7 ms 464 KB Output is correct
4 Correct 7 ms 340 KB Output is correct
5 Correct 7 ms 352 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 6 ms 340 KB Output is correct
8 Correct 7 ms 352 KB Output is correct
9 Correct 5 ms 340 KB Output is correct
10 Correct 10 ms 352 KB Output is correct
11 Correct 8 ms 356 KB Output is correct
12 Correct 7 ms 360 KB Output is correct
13 Correct 2 ms 212 KB Output is correct
14 Correct 4 ms 340 KB Output is correct
15 Correct 2 ms 212 KB Output is correct
16 Correct 7 ms 348 KB Output is correct
17 Correct 7 ms 340 KB Output is correct
18 Correct 7 ms 360 KB Output is correct
19 Correct 7 ms 340 KB Output is correct
20 Correct 7 ms 340 KB Output is correct
21 Correct 696 ms 752 KB Output is correct
22 Correct 1194 ms 908 KB Output is correct
23 Correct 1103 ms 1204 KB Output is correct
24 Correct 1153 ms 1052 KB Output is correct
25 Correct 1190 ms 1020 KB Output is correct
26 Correct 203 ms 492 KB Output is correct
27 Correct 1076 ms 1080 KB Output is correct
28 Correct 1201 ms 1008 KB Output is correct
29 Correct 1219 ms 1004 KB Output is correct
30 Correct 1220 ms 1012 KB Output is correct
31 Correct 1226 ms 1036 KB Output is correct
32 Correct 1131 ms 1044 KB Output is correct
33 Correct 1053 ms 1204 KB Output is correct
34 Correct 1184 ms 1228 KB Output is correct
35 Correct 1115 ms 1216 KB Output is correct
36 Correct 1210 ms 1320 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 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 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 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 1 ms 288 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 55 ms 380 KB Output is correct
23 Correct 1 ms 212 KB Output is correct
24 Correct 75 ms 432 KB Output is correct
25 Correct 66 ms 420 KB Output is correct
26 Correct 102 ms 520 KB Output is correct
27 Correct 98 ms 516 KB Output is correct
28 Correct 69 ms 340 KB Output is correct
29 Correct 85 ms 512 KB Output is correct
30 Correct 99 ms 520 KB Output is correct
31 Correct 100 ms 524 KB Output is correct
32 Correct 100 ms 512 KB Output is correct
33 Correct 1 ms 340 KB Output is correct
34 Correct 91 ms 536 KB Output is correct
35 Correct 97 ms 528 KB Output is correct
36 Correct 100 ms 516 KB Output is correct
37 Correct 66 ms 496 KB Output is correct
38 Correct 103 ms 524 KB Output is correct
39 Correct 96 ms 524 KB Output is correct
40 Correct 100 ms 532 KB Output is correct
41 Correct 83 ms 520 KB Output is correct
42 Correct 87 ms 504 KB Output is correct
43 Correct 95 ms 512 KB Output is correct
44 Correct 99 ms 528 KB Output is correct
45 Correct 3 ms 340 KB Output is correct
46 Correct 6 ms 340 KB Output is correct
47 Correct 7 ms 464 KB Output is correct
48 Correct 7 ms 340 KB Output is correct
49 Correct 7 ms 352 KB Output is correct
50 Correct 1 ms 212 KB Output is correct
51 Correct 6 ms 340 KB Output is correct
52 Correct 7 ms 352 KB Output is correct
53 Correct 5 ms 340 KB Output is correct
54 Correct 10 ms 352 KB Output is correct
55 Correct 8 ms 356 KB Output is correct
56 Correct 7 ms 360 KB Output is correct
57 Correct 2 ms 212 KB Output is correct
58 Correct 4 ms 340 KB Output is correct
59 Correct 2 ms 212 KB Output is correct
60 Correct 7 ms 348 KB Output is correct
61 Correct 7 ms 340 KB Output is correct
62 Correct 7 ms 360 KB Output is correct
63 Correct 7 ms 340 KB Output is correct
64 Correct 7 ms 340 KB Output is correct
65 Correct 696 ms 752 KB Output is correct
66 Correct 1194 ms 908 KB Output is correct
67 Correct 1103 ms 1204 KB Output is correct
68 Correct 1153 ms 1052 KB Output is correct
69 Correct 1190 ms 1020 KB Output is correct
70 Correct 203 ms 492 KB Output is correct
71 Correct 1076 ms 1080 KB Output is correct
72 Correct 1201 ms 1008 KB Output is correct
73 Correct 1219 ms 1004 KB Output is correct
74 Correct 1220 ms 1012 KB Output is correct
75 Correct 1226 ms 1036 KB Output is correct
76 Correct 1131 ms 1044 KB Output is correct
77 Correct 1053 ms 1204 KB Output is correct
78 Correct 1184 ms 1228 KB Output is correct
79 Correct 1115 ms 1216 KB Output is correct
80 Correct 1210 ms 1320 KB Output is correct
81 Runtime error 2 ms 596 KB Execution killed with signal 6
82 Halted 0 ms 0 KB -