답안 #560741

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
560741 2022-05-12T02:04:00 Z amunduzbaev Cultivation (JOI17_cultivation) C++17
15 / 100
2000 ms 1492 KB
#include "bits/stdc++.h"
using namespace std;
 
#define ar array
#define int long long
 
signed main(){
	ios::sync_with_stdio(0); cin.tie(0);
	
	int R, C; cin>>R>>C;
	int n; cin>>n;
	vector<int> x(n), y(n);
	for(int i=0;i<n;i++){
		cin>>x[i]>>y[i];
	}
	
	int mn_pos = -1;
	{
		auto check = [&](int m){
			vector<ar<int, 2>> s(n);
			for(int i=0;i<n;i++) s[i] = {x[i], x[i] + m - 1};
			sort(s.begin(), s.end());
			int l = -1, r = -1, mx = 0;
			for(auto x : s){
				if(r + 1 < x[0]){
					mx = max(mx, r - l + 1);
					l = x[0], r = x[1];
				} else {
					r = max(r, x[1]);
				}
			} mx = max(mx, r - l + 1);
			return (mx >= R);
		};
		
		int l = 1, r = R;
		while(l < r){
			int m = (l + r) >> 1;
			if(check(m)) r = m;
			else l = m + 1;
		}
		
		mn_pos = l - 1;
	}
	
	vector<int> tot;
	tot.push_back(mn_pos);
	tot.push_back(R - 1);
	for(int i=0;i<n;i++){
		for(int j=0;j<n;j++){
			if(abs(x[i] - x[j]) - 1 >= mn_pos) tot.push_back(abs(x[i] - x[j]) - 1);
		}
	}
	
	sort(tot.begin(), tot.end());
	tot.erase(unique(tot.begin(), tot.end()), tot.end());
	int res = R + C - 2;
	auto check = [&](int a, int add){
		vector<ar<int, 2>> s;
		for(int i=0;i<n;i++){
			s.push_back({x[i], y[i]});
			s.push_back({x[i] + a + 1, -y[i]});
		}
		
		multiset<int> ss, bet;
		sort(s.begin(), s.end());
		//~ for(auto x : s) cout<<x[0]<<" "<<x[1]<<endl;
		//~ cout<<endl;
		int m = s.size();
		vector<ar<int, 5>> tot;
		
		//~ cout<<"here"<<endl;
		for(int i=0;i<m;){
			int j = i;
			while(j<m && s[j][0] == s[i][0]){
				if(s[j][1] > 0){
					auto rx = ss.lower_bound(s[j][1]);
					auto lx = rx;
					if(lx != ss.begin() && rx != ss.end()){
						lx--;
						bet.erase(bet.find(*rx - *lx - 1));
						lx++;
					}
					
					if(rx != ss.end()){
						bet.insert(*rx - s[j][1] - 1);
					} if(lx != ss.begin()){
						lx--;
						bet.insert(s[j][1] - *lx - 1);
						lx++;
					}
					
					ss.insert(s[j][1]);
				} else {
					s[j][1] *= -1;
					ss.erase(ss.find(s[j][1]));
					auto rx = ss.lower_bound(s[j][1]);
					auto lx = rx;
					if(rx != ss.end()){
						bet.erase(bet.find(*rx - s[j][1] - 1));
					} if(lx != ss.begin()){ lx--;
						bet.erase(bet.find(s[j][1] - *lx - 1));
						lx++;
					}
					if(lx != ss.begin() && rx != ss.end()){
						lx--;
						bet.insert(*rx - *lx - 1);
						lx++;
					}
				}
				
				j++;
			}
			int t = 0;
			if(j < m){
				if((s[i][1] > 0) && (s[j][1] < 0)) t = 1;
				if((s[i][1] < 0) && (s[j][1] > 0)) t = -1;
			}
			int left = 0, right = 0, l_r = 0;
			if(!ss.empty()) left = *ss.begin() - 1;
			if(!ss.empty()) right = C - *--ss.end();
			if(!bet.empty()) l_r = *--bet.end();
			//~ for(auto x : ss) cout<<x<<" ";
			//~ cout<<endl;
			//~ for(auto x : bet) cout<<x<<" ";
			//~ cout<<endl;
			//~ cout<<s[i][0]<<" "<<left<<" "<<right<<" "<<l_r<<endl;
			tot.push_back({s[i][0], left, right, l_r, t});
			
			i = j;
		}
		
		//~ for(auto x : tot){
			//~ cout<<x[0]<<" "<<x[1]<<" "<<x[2]<<" "<<x[3]<<" "<<x[4]<<"\n";
		//~ }
		
		//~ int r = 0, mn = C; 
		m = tot.size();
		for(int i=0;i<m;i++){
			int A = 0, B = 0, C = 0, cc = 0;
			for(int j=i+1;j<m;j++){
				A = max(A, tot[j-1][1]);
				B = max(B, tot[j-1][2]);
				C = max(C, tot[j-1][3]);
				cc += tot[j-1][4];
				if(tot[j][0] - tot[i][0] >= R){
					res = min(res, a + max(A + B, C));
				} else if(cc * add >= R - (tot[j][0] - tot[i][0])){
					res = min(res, a + max(A + B, C) + (R - (tot[j][0] - tot[i][0]) + cc - 1) / cc);
				}
				
				//~ if(add) assert(cc < 2);
			}
		}
		
		//~ multiset<int> A, B, C;
		//~ for(int l=0;l<m;l++){
			//~ while(r<m && tot[r][0] - tot[l][0] < R){
				//~ A.insert(tot[r][1]);
				//~ B.insert(tot[r][2]);
				//~ C.insert(tot[r][3]);
				//~ r++;
			//~ } if(r == m) break;
			//~ mn = min(mn, max(*--A.end() + *--B.end(), *--C.end()));
			//~ A.erase(A.find(tot[l][1]));
			//~ B.erase(B.find(tot[l][2]));
			//~ C.erase(C.find(tot[l][3]));
		//~ }
		
		//~ res = min(res, a + mn);
	};
	
	//~ cout<<"here"<<endl;
	//~ for(int i=0;i<(int)tot.size();i++){
		//~ check(tot[i], (i+1 < (int)tot.size() ? tot[i+1] - tot[i] - 1 : R));
	//~ }
	for(int i=mn_pos;i<R;i++){
		check(i, 0);
	}
	cout<<res<<"\n";
}
 
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 320 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 1 ms 324 KB Output is correct
6 Correct 1 ms 316 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 320 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 320 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 1 ms 324 KB Output is correct
6 Correct 1 ms 316 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 320 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 320 KB Output is correct
18 Correct 2 ms 340 KB Output is correct
19 Correct 1 ms 320 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 2 ms 340 KB Output is correct
22 Correct 5 ms 460 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 9 ms 980 KB Output is correct
25 Correct 7 ms 744 KB Output is correct
26 Correct 16 ms 1492 KB Output is correct
27 Correct 14 ms 1492 KB Output is correct
28 Correct 9 ms 980 KB Output is correct
29 Correct 15 ms 1492 KB Output is correct
30 Correct 15 ms 1492 KB Output is correct
31 Correct 14 ms 1476 KB Output is correct
32 Correct 14 ms 1492 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 320 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 1 ms 324 KB Output is correct
6 Correct 1 ms 316 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 320 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 320 KB Output is correct
18 Correct 2 ms 340 KB Output is correct
19 Correct 1 ms 320 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 2 ms 340 KB Output is correct
22 Correct 5 ms 460 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 9 ms 980 KB Output is correct
25 Correct 7 ms 744 KB Output is correct
26 Correct 16 ms 1492 KB Output is correct
27 Correct 14 ms 1492 KB Output is correct
28 Correct 9 ms 980 KB Output is correct
29 Correct 15 ms 1492 KB Output is correct
30 Correct 15 ms 1492 KB Output is correct
31 Correct 14 ms 1476 KB Output is correct
32 Correct 14 ms 1492 KB Output is correct
33 Incorrect 2 ms 984 KB Output isn't correct
34 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 2068 ms 212 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 2068 ms 212 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 320 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 1 ms 324 KB Output is correct
6 Correct 1 ms 316 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 320 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 320 KB Output is correct
18 Correct 2 ms 340 KB Output is correct
19 Correct 1 ms 320 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 2 ms 340 KB Output is correct
22 Correct 5 ms 460 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 9 ms 980 KB Output is correct
25 Correct 7 ms 744 KB Output is correct
26 Correct 16 ms 1492 KB Output is correct
27 Correct 14 ms 1492 KB Output is correct
28 Correct 9 ms 980 KB Output is correct
29 Correct 15 ms 1492 KB Output is correct
30 Correct 15 ms 1492 KB Output is correct
31 Correct 14 ms 1476 KB Output is correct
32 Correct 14 ms 1492 KB Output is correct
33 Incorrect 2 ms 984 KB Output isn't correct
34 Halted 0 ms 0 KB -