Submission #418882

# Submission time Handle Problem Language Result Execution time Memory
418882 2021-06-06T06:59:15 Z Kevin_Zhang_TW Cultivation (JOI17_cultivation) C++17
55 / 100
2000 ms 796 KB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define pb emplace_back
#define AI(i) begin(i), end(i)
template<class T> bool chmin(T &a, T b) { return b < a && (a = b, true); }
template<class T> bool chmax(T &a, T b) { return a < b && (a = b, true); }
#ifdef KEV
#define DE(args...) kout("[ " + string(#args) + " ] = ", args)
void kout() { cerr << endl; }
template<class T, class ...U> void kout(T a, U ...b) { cerr << a << ' ', kout(b...); }
template<class T> void debug(T l, T r) { while (l != r) cerr << *l << " \n"[next(l)==r], ++l; }
#else
#define DE(...) 0
#define debug(...) 0
#endif

const int MAX_N = 305;

#define int ll
const int inf = 1ll << 55;
int R, C, n;
vector<pair<int,int>> loc;

bool can_cover(int ul, int dl, int al, int bl) {

	vector<vector<int>> pf_sum(50, vector<int> (50) );
	for (auto [x, y] : loc) {
		int A = y - ul, B = y + dl;
		int C = x - bl, D = x + al;
		chmax(A, 1ll);
		chmax(C, 1ll);
		chmin(D, R);
		chmin(B, ::C);
		++pf_sum[C][A];
		--pf_sum[D+1][A];
		--pf_sum[C][B+1];
		++pf_sum[D+1][B+1];
	}

	for (int i = 1;i <= R;++i) {
		for (int j = 1;j <= C;++j) {
			pf_sum[i][j] += pf_sum[i-1][j] + pf_sum[i][j-1] - pf_sum[i-1][j-1];
			if (pf_sum[i][j] == 0) return false;
		}
	}
	return true;
}

int cal_up(int ul) {
	int dnmn = 0;
	{
		vector<int> ys;
		for (auto [x, y] : loc)
			ys.pb(y - ul);
		sort(AI(ys));
		int lst_end = 0;
		for (int y : ys) {
			chmax(dnmn, y - 1 - lst_end);
			lst_end = y + ul;
		}
		chmax(dnmn, C - lst_end);
	}

	vector<vector<tuple<int,int,int>>> op2, must;
	vector<int> sep;

	DE(ul);
	auto all_op = [&]() {
		op2.pb();
		must.pb();
		sort(AI(loc));

		for (int i = 0;i < n;++i) {
			auto [x, y] = loc[i];

			auto segmx = [&](int l, int r, int v) {
				chmax(l, dnmn);
				chmin(r, C);
				if (l > r) return;

				sep.pb(l), sep.pb(r+1);
				op2.back().pb(l, r+1, v - x - 1);
			};
			auto allmx = [&](int l, int r, int v) {
				chmax(l, dnmn);
				chmin(r, C);
				if (l > r) return;

				sep.pb(l), sep.pb(r+1);
				must.back().pb(l, r+1, v - x - 1);
			};

			set<int> ys;
			ys.insert(-inf);
			ys.insert(inf);

			int nl = 1, nr = C;
			for (int j = i+1;j < n;++j) {
				auto [cx, cy] = loc[j];
				if (cx == x) continue;

				if (cy <= y) chmax(nl, cy + 1);
				if (cy >= y) chmin(nr, cy - ul - 1);

				auto it = ys.lower_bound(cy);
				int ly = *prev(it), ry = *it;  ys.insert(cy);
				int s = 0, e = min(C - ly - 1, (ry - ul - 1) - ly - 1);
				if (cy >= y) chmax(s, cy - ul - y);
				else chmax(s, y - ul - cy);
				if (y <= ly || ry <= y || s > e || ry == cy || ry - ul <= 1) continue;
				segmx(s, e, cx);
			}

			if (nl > nr) continue;
			if (nl == 1) allmx(0, C, R + 1);
			else if (nl <= nr) allmx(0, nr - nl, R + 1);

		}
   	};

	all_op();
	for (auto &[x, y] : loc) 
		x = R - x + 1;

	all_op();
	for (auto &[x, y] : loc) 
		x = R - x + 1;

	sep.pb(C+1);

	sort(AI(sep)); sep.erase(unique(AI(sep)), end(sep));

	priority_queue<pair<int,int>> pq[2], pq2[2];

	sort(AI(op2[0]));
	sort(AI(op2[1]));

	vector<tuple<int,int,int>> bigop;
	for (auto i : op2[0]) bigop.pb(i);
	for (auto i : op2[1]) bigop.pb(i);
	sort(AI(bigop));


	sort(AI(must[0]));
	sort(AI(must[1]));

	int h[3]{}, h2[2]{};

	int res = inf;
	for (int i = 0;i+1 < sep.size();++i) {
		int M = sep[i];
		auto add_in = [&](auto &pq, auto &op, int &i) {
			for (;i < op.size();++i) {
				auto [l, r, v] = op[i];
				if (l > M) break;
				pq.emplace(v, r);
			}
		};
		auto pops = [&](auto &pq) -> ll{
			while (pq.size() && pq.top().second <= sep[i])
				pq.pop();
			return pq.size() ? pq.top().first : 0;
		};

		//add_in(pq[0], bigop, h[0]);
//		add_in(pq[0], op2[0], h[0]);
//		add_in(pq[0], op2[1], h[1]);
		add_in(pq[0], bigop, h[2]);
		//add_in(pq[1], op2[1], h[1]);
		add_in(pq2[0], must[0], h2[0]);
		add_in(pq2[1], must[1], h2[1]);


		//int ld = pops(pq2[0]), rd = ;
		//max(pops(pq2[1]), max(pops(pq[0]), pops(pq[1])) - ld), dl = sep[i];

		chmin(res, sep[i] + ul + 
				max(max(pops(pq[0]), pops(pq[1])), pops(pq2[0]) + pops(pq2[1])));
		//chmin(res, sep[i] + ul + ld + rd);

	}
	return res;
}
int brute() {
	int res = inf;
	for (int ul = 0;ul <= C;++ul) for (int dl = 0;dl <= C;++dl) {
		int A = R, B = R;
		while (A > 0 && can_cover(ul, dl, A-1, B)) --A;
		while (B > 0 && can_cover(ul, dl, A, B-1)) --B;
		if (can_cover(ul, dl, A, B)) {
			if (chmin(res, ul + dl + A + B))
				DE(ul, dl, A, B);
		}
	}
	return res;
}
int32_t main() {
	ios_base::sync_with_stdio(0), cin.tie(0);
	cin >> R >> C >> n; loc.resize(n);
	for (auto &[x, y] : loc) {
		cin >> x >> y;
	}
	sort(AI(loc));

	int res = inf;

	vector<int> ys;
	for (auto [x, y] : loc) ys.pb(y);



	for (int y : ys) {
		chmin(res, cal_up(y - 1));
	}

	cout << res << '\n';
}

Compilation message

cultivation.cpp: In function 'll cal_up(ll)':
cultivation.cpp:14:17: warning: statement has no effect [-Wunused-value]
   14 | #define DE(...) 0
      |                 ^
cultivation.cpp:68:2: note: in expansion of macro 'DE'
   68 |  DE(ul);
      |  ^~
cultivation.cpp:151:21: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  151 |  for (int i = 0;i+1 < sep.size();++i) {
      |                 ~~~~^~~~~~~~~~~~
cultivation.cpp: In instantiation of 'cal_up(ll)::<lambda(auto:23&, auto:24&, ll&)> [with auto:23 = std::priority_queue<std::pair<long long int, long long int> >; auto:24 = std::vector<std::tuple<long long int, long long int, long long int> >; ll = long long int]':
cultivation.cpp:169:28:   required from here
cultivation.cpp:154:12: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::tuple<long long int, long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  154 |    for (;i < op.size();++i) {
      |          ~~^~~~~~~~~~~
cultivation.cpp: In function 'll brute()':
cultivation.cpp:14:17: warning: statement has no effect [-Wunused-value]
   14 | #define DE(...) 0
      |                 ^
cultivation.cpp:193:5: note: in expansion of macro 'DE'
  193 |     DE(ul, dl, A, B);
      |     ^~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 0 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 0 ms 204 KB Output is correct
14 Correct 0 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 0 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 0 ms 204 KB Output is correct
14 Correct 0 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 2 ms 332 KB Output is correct
18 Correct 34 ms 408 KB Output is correct
19 Correct 5 ms 332 KB Output is correct
20 Correct 2 ms 204 KB Output is correct
21 Correct 18 ms 372 KB Output is correct
22 Correct 202 ms 492 KB Output is correct
23 Correct 6 ms 332 KB Output is correct
24 Correct 810 ms 796 KB Output is correct
25 Correct 398 ms 616 KB Output is correct
26 Execution timed out 2072 ms 796 KB Time limit exceeded
27 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 0 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 0 ms 204 KB Output is correct
14 Correct 0 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 2 ms 332 KB Output is correct
18 Correct 34 ms 408 KB Output is correct
19 Correct 5 ms 332 KB Output is correct
20 Correct 2 ms 204 KB Output is correct
21 Correct 18 ms 372 KB Output is correct
22 Correct 202 ms 492 KB Output is correct
23 Correct 6 ms 332 KB Output is correct
24 Correct 810 ms 796 KB Output is correct
25 Correct 398 ms 616 KB Output is correct
26 Execution timed out 2072 ms 796 KB Time limit exceeded
27 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 332 KB Output is correct
2 Correct 5 ms 332 KB Output is correct
3 Correct 4 ms 332 KB Output is correct
4 Correct 3 ms 204 KB Output is correct
5 Correct 3 ms 344 KB Output is correct
6 Correct 3 ms 344 KB Output is correct
7 Correct 3 ms 332 KB Output is correct
8 Correct 3 ms 204 KB Output is correct
9 Correct 3 ms 204 KB Output is correct
10 Correct 3 ms 332 KB Output is correct
11 Correct 3 ms 332 KB Output is correct
12 Correct 3 ms 332 KB Output is correct
13 Correct 2 ms 204 KB Output is correct
14 Correct 3 ms 332 KB Output is correct
15 Correct 3 ms 204 KB Output is correct
16 Correct 4 ms 332 KB Output is correct
17 Correct 3 ms 332 KB Output is correct
18 Correct 3 ms 332 KB Output is correct
19 Correct 3 ms 332 KB Output is correct
20 Correct 2 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 332 KB Output is correct
2 Correct 5 ms 332 KB Output is correct
3 Correct 4 ms 332 KB Output is correct
4 Correct 3 ms 204 KB Output is correct
5 Correct 3 ms 344 KB Output is correct
6 Correct 3 ms 344 KB Output is correct
7 Correct 3 ms 332 KB Output is correct
8 Correct 3 ms 204 KB Output is correct
9 Correct 3 ms 204 KB Output is correct
10 Correct 3 ms 332 KB Output is correct
11 Correct 3 ms 332 KB Output is correct
12 Correct 3 ms 332 KB Output is correct
13 Correct 2 ms 204 KB Output is correct
14 Correct 3 ms 332 KB Output is correct
15 Correct 3 ms 204 KB Output is correct
16 Correct 4 ms 332 KB Output is correct
17 Correct 3 ms 332 KB Output is correct
18 Correct 3 ms 332 KB Output is correct
19 Correct 3 ms 332 KB Output is correct
20 Correct 2 ms 204 KB Output is correct
21 Correct 156 ms 460 KB Output is correct
22 Correct 171 ms 480 KB Output is correct
23 Correct 159 ms 420 KB Output is correct
24 Correct 149 ms 376 KB Output is correct
25 Correct 186 ms 480 KB Output is correct
26 Correct 108 ms 432 KB Output is correct
27 Correct 125 ms 376 KB Output is correct
28 Correct 167 ms 520 KB Output is correct
29 Correct 159 ms 580 KB Output is correct
30 Correct 160 ms 484 KB Output is correct
31 Correct 165 ms 480 KB Output is correct
32 Correct 157 ms 480 KB Output is correct
33 Correct 105 ms 356 KB Output is correct
34 Correct 148 ms 536 KB Output is correct
35 Correct 129 ms 332 KB Output is correct
36 Correct 160 ms 468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 0 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 0 ms 204 KB Output is correct
14 Correct 0 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 2 ms 332 KB Output is correct
18 Correct 34 ms 408 KB Output is correct
19 Correct 5 ms 332 KB Output is correct
20 Correct 2 ms 204 KB Output is correct
21 Correct 18 ms 372 KB Output is correct
22 Correct 202 ms 492 KB Output is correct
23 Correct 6 ms 332 KB Output is correct
24 Correct 810 ms 796 KB Output is correct
25 Correct 398 ms 616 KB Output is correct
26 Execution timed out 2072 ms 796 KB Time limit exceeded
27 Halted 0 ms 0 KB -