Submission #560502

# Submission time Handle Problem Language Result Execution time Memory
560502 2022-05-11T14:38:09 Z amunduzbaev Cultivation (JOI17_cultivation) C++17
0 / 100
2000 ms 212 KB
#include "bits/stdc++.h"
using namespace std;

#define ar array

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 res = R + C;
	auto check = [&](int a){
		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, 4>> 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});
			
			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);
	};
	
	for(int a=0;a<R;a++){
		check(a);
	}
	
	cout<<res<<"\n";
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Incorrect 1 ms 212 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Incorrect 1 ms 212 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Incorrect 1 ms 212 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 2079 ms 212 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 2079 ms 212 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Incorrect 1 ms 212 KB Output isn't correct
3 Halted 0 ms 0 KB -