Submission #131518

#TimeUsernameProblemLanguageResultExecution timeMemory
131518gs14004Cultivation (JOI17_cultivation)C++17
30 / 100
510 ms888 KiB
#include <bits/stdc++.h>
using namespace std;
typedef long long lint;
typedef long double llf;
typedef pair<int, int> pi;
const int MAXN = 305;

int n, r, c;
pi a[MAXN];

struct sweep{
	int pos, val, mode;
	bool operator<(const sweep &s)const{
		return pi(pos, -mode) < pi(s.pos, -s.mode);
	}
};

lint Do(int i, int j){
	vector<sweep> sw;
	for(int k=0; k<n; k++){
		int le = max(1, a[k].first - i);
		int ri = min(r, a[k].first + j);
		int v = a[k].second;
		sw.push_back({le, v, +1});
		sw.push_back({ri + 1, v, -1});
	}
	vector<int> v = {1, r + 1};
	for(int i=0; i<sw.size(); i++){ 
		v.push_back(sw[i].pos);
	}
	sort(sw.begin(), sw.end());
	sort(v.begin(), v.end());
	v.resize(unique(v.begin(), v.end()) - v.begin());
	multiset<int> s;
	int ptr = 0;
	int lv = 0, rv = 0, sum = 0;
	for(int i=0; i+1<v.size(); i++){
		while(ptr < sw.size() && sw[ptr].pos == v[i]){
			if(sw[ptr].mode == 1) s.insert(sw[ptr].val);
			else s.erase(s.find(sw[ptr].val));
			ptr++;
		}
		if(s.size() == 0) return 1e18;
		lv = max(lv, *s.begin() - 1);
		rv = max(rv, c - *s.rbegin());
		auto it = s.begin();
		while(next(it) != s.end()){
			auto in = next(it);
			sum = max(*in - *it - 1, sum);
			it = in;
		}
	}
	return (1ll * max(lv + rv, sum)) + i + j;
}

lint solve(){
	lint ans = 1e18;
	sort(a, a+n);
	vector<int> ev;
	for(int i=0; i<n; i++){
		ev.push_back(a[i].first - 1);
	}
	sort(ev.begin(), ev.end());
	ev.resize(unique(ev.begin(), ev.end()) - ev.begin());
	for(auto &l : ev){
		vector<int> v;
		for(int j=0; j<n; j++){
			for(int k=j; k<n; k++){
				if(a[k].first - a[j].first >= l) v.push_back(a[k].first - a[j].first - l);
			}
			v.push_back(r - a[j].first);
		}
		sort(v.begin(), v.end());
		v.resize(unique(v.begin(), v.end()) - v.begin());
		for(auto &j : v){
			ans = min(ans, Do(l, j));
		}
	}
	return ans;
}

int main(){
	cin >> r >> c >> n;
	for(int i=0; i<n; i++){
		cin >> a[i].first >> a[i].second;
	}
	lint ans = solve();
	for(int i=0; i<n; i++) a[i].first = r + 1 - a[i].first;
	ans = min(ans, solve());
	cout << ans << endl;
}

Compilation message (stderr)

cultivation.cpp: In function 'lint Do(int, int)':
cultivation.cpp:28:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0; i<sw.size(); i++){ 
               ~^~~~~~~~~~
cultivation.cpp:37:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0; i+1<v.size(); i++){
               ~~~^~~~~~~~~
cultivation.cpp:38:13: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   while(ptr < sw.size() && sw[ptr].pos == v[i]){
         ~~~~^~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...