Submission #414458

# Submission time Handle Problem Language Result Execution time Memory
414458 2021-05-30T12:50:52 Z amoo_safar Cultivation (JOI17_cultivation) C++17
100 / 100
1858 ms 26000 KB
// vaziat meshki-ghermeze !
#include <bits/stdc++.h>

#define pb push_back
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define debug(x) cerr << #x << " : " << x << '\n'
// #define int ll
using namespace std;

typedef long long ll;
typedef long double ld;
typedef string str;
typedef pair<int, int> pii;

const ll Mod = 1000000007LL;
const int N = 3e2 + 10;
const int Inf = 2'000'000'001;
const ll Log = 30;

int n, R, C;
pii A[N];

// 0, x, y -> C + R < x -> y
// 1, x, y -> C < x -> y
// 2, x, y -> R < x -> y
typedef pii con;
typedef pair<con, int> Query;
vector<Query> Q, TQ, V0, V1;
typedef pair<con, con> iff;
vector< pair<iff, int> > cons;

bool Better(con A, con B){
	if(A.F != B.F)
		return false;
	return A.S >= B.S;
}
bool Better(iff A, iff B){
	return Better(A.F, B.F) && Better(A.S, B.S);
}
bool Sat(con A, int i, int j){
	if(A.F == 0)
		return i + j < A.S;
	if(A.F == 1)
		return i < A.S;
	return j < A.S;
}
void Check(int l, int r){
	int ln = r - l - 1;
	vector<int> V;
	for(int k = 1; k <= n; k++){
		if(l < A[k].F && A[k].F < r)
			V.pb(A[k].S);
	}
	sort(all(V));

	int sz = Q.size();
	vector< pair<con, int> > tmp;
	if(!V.empty() && V[0] > 1)
		tmp.pb({{1, V[0] - 1}, ln});
	if(!V.empty() && V.back() < C)
		tmp.pb({{2, C - V.back()}, ln});
	for(int i = 0; i + 1 < (int) V.size(); i++){
		if(0 < V[i + 1] - V[i] - 1)
			tmp.pb({{0, V[i + 1] - V[i] - 1}, ln});
	}
	if(V.empty())
		tmp.pb({{1, C}, ln});
	sort(all(tmp));
	int cnt = 0;
	for(int i = 0; i < (int) tmp.size(); i++)
		if((i + 1 == (int) tmp.size()) || (!Better(tmp[i + 1].F, tmp[i].F)) )
			Q.pb(tmp[i]), cnt ++;
	// cerr << "!! " << ' ' << cnt << '\n';
	for(int i = sz; i < (int) Q.size(); i++)
		TQ.pb(Q[i]);
	// Q.resize(sz);
}

int ans[N][N]; // C R

pii Inter(pii A, pii B){ return pii(max(A.F, B.F), min(A.S, B.S)); }
pii Inter(con c, int v){
	if(c.F == 0){
		if(v < c.S)
			return pii(0, v + 1);
		return pii(v + 1, 0);
	} else if(c.F == 1){
		return pii(0, c.S);
	}
	return pii((v - c.S) + 1, v + 1);
}
pii Inter(iff c, int v){ return Inter(Inter(c.F, v), Inter(c.S, v)); }

vector<iff> act;

int max_sz = 0;
void Uni(){
	vector<iff> tmp = act;
	act.clear();
	sort(all(tmp),[&](auto e1, auto e2){
		if(e1.F.F != e2.F.F)
			return e1.F.F < e2.F.F;
		if(e1.S.F != e2.S.F)
			return e1.S.F < e2.S.F;
		return pii(e1.F.S, e1.S.S) < pii(e2.F.S, e2.S.S);
	});

	for(int i = 0; i < (int) tmp.size(); i++)
		if((i + 1 == (int) tmp.size()) || (!Better(tmp[i + 1], tmp[i])) )
			act.pb(tmp[i]);
	max_sz = max(max_sz, (int) act.size());
}
bool Valid(int v){
	// return false;
	// vector<int> mk(v + 1, 0);
	// v = min(C - 1 + C - 1, v);
	// for(int i = 0, j = v; i < C; i++, j--)
	// 	if(0 <= j && j < C && ans[i][j] == 0)
	// 		return true;
	// return false;
	
	pii seg = pii(0, C);
	if(v < C)
		seg = pii(0, v + 1);
	else
		seg = pii(v - (C - 1), C);
	// cerr << "!! " << v << ' ' << seg.F << ' ' << seg.S << '\n';

	vector<pii> Al;
	for(auto fi : act){
		pii X = Inter(fi, v);
		X = Inter(X, seg);
		// X.S = min(X.S, v + 1);
		// X.F = max(X.F, 0);
		if(X.F >= X.S) continue;
		Al.pb(X);
		// for(int i = X.F; i < X.S; i++) mk[i] = 1;
	}
	sort(all(Al));
	int nw = seg.F;
	for(auto [l, r] : Al){
		if(nw < l)
			return true;
		nw = max(nw, r);
	}
	if(nw != seg.S)
		return true;
	return false;
	// for(int i = 0; i <= v; i++){
	// 	if(i < C && v - i < C && mk[i] == 0)
	// 		return true;
	// }
	// return false;
}

int32_t main(){
	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	cin >> R >> C >> n;
	for(int i = 1; i <= n; i++){
		cin >> A[i].F >> A[i].S;
	}
	sort(A + 1, A + n + 1);
	// vector<int> V;
	// sort(all(V));
	// for(int i = 1; i <= n; i++) V.pb(A[i].S);
	// int mn_S = 0;
	// for(int i = 0; i < n; i++)
	// 	mn_S = max(mn_S, V[i + 1] - V[i] - 1);
	// Check(0, R + 1);
	// int mx = (A[1].F - 1) + (R - A[n].F);
	Check(0, R + 1);
	for(auto &[cn, sm] : Q)
		sm = Inf;

	// debug(Q.size());

	TQ.clear();
	int sz = Q.size();
	for(int i = 1; i <= n; i++)
		Check(0, A[i].F);
	Q.resize(sz);
	V0 = TQ; TQ.clear();
	for(int i = 1; i <= n; i++)
		Check(A[i].F, R + 1);
	Q.resize(sz);
	V1 = TQ; TQ.clear();
	for(auto [c1, v1] : V0)
		for(auto [c2, v2] : V1)
			cons.pb({{min(c1, c2), max(c1, c2)}, v1 + v2});
	// debug(Q.size());
	Q.resize(sz);
	// debug(Q.size());

	for(int i = 1; i <= n; i++)
		for(int j = i + 1; j <= n; j++)
			if(A[j].F - 1 > A[i].F)
				Check(A[i].F, A[j].F);
	// debug(Q.size());
	for(auto [c1, v1] : Q)
		cons.pb({{c1, c1}, v1});
	
	sort(all(cons), [&](auto e1, auto e2){
		return e1.S > e2.S;
	});
	
	// debug("X");
	// debug(cons.size());
	ll ANS = R + C;
	cons.pb({{{0, 0}, {0, 0}}, 0});
	int la = 0;
	int bs = 0;
	ll L = -1;
	ll la_bs = -1;
	for(auto [fi, v] : cons){
		// debug(v);
		// int CC = C + C;
		// for(int i = 0; i < C; i++){
		// 	for(int j = 0; j < C; j++)
		// 		if(!ans[i][j])
		// 			// CC = min(CC, i + j);
		// 			ANS = min(ANS, i + j + v);
		// }
		if(la == v){
			act.pb(fi);
			continue;
		}
		la = v;
		if(v == Inf){
			act.pb(fi);
			continue;
		}
		Uni();
		bs++;
		ll Rb = C + C - 1;
		ll mid;
		if(Valid(L + 1)){
			Rb = L + 1;
		} else {
			while(L + 1 < Rb){
				mid = (L + Rb) >> 1;
				if(Valid(mid)) Rb = mid;
				else L = mid;
			}
		}
		if(Rb != C + C - 1)
			ANS = min(ANS, 0ll + Rb + v);
		// la = v;
		act.pb(fi);
	}
	// debug(ANS);
	
	// debug(max_sz);
	// debug(bs);
	cout << ANS << '\n';
	return 0;
}

Compilation message

cultivation.cpp: In function 'int32_t main()':
cultivation.cpp:215:5: warning: unused variable 'la_bs' [-Wunused-variable]
  215 |  ll la_bs = -1;
      |     ^~~~~
# 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 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 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 1 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 1 ms 204 KB Output is correct
14 Correct 1 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 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 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 1 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 1 ms 204 KB Output is correct
14 Correct 1 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 1 ms 332 KB Output is correct
18 Correct 5 ms 780 KB Output is correct
19 Correct 2 ms 460 KB Output is correct
20 Correct 1 ms 460 KB Output is correct
21 Correct 4 ms 688 KB Output is correct
22 Correct 17 ms 2092 KB Output is correct
23 Correct 2 ms 588 KB Output is correct
24 Correct 49 ms 2444 KB Output is correct
25 Correct 28 ms 2092 KB Output is correct
26 Correct 183 ms 14488 KB Output is correct
27 Correct 173 ms 5048 KB Output is correct
28 Correct 53 ms 3720 KB Output is correct
29 Correct 134 ms 4364 KB Output is correct
30 Correct 163 ms 4508 KB Output is correct
31 Correct 155 ms 4776 KB Output is correct
32 Correct 134 ms 4380 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 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 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 1 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 1 ms 204 KB Output is correct
14 Correct 1 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 1 ms 332 KB Output is correct
18 Correct 5 ms 780 KB Output is correct
19 Correct 2 ms 460 KB Output is correct
20 Correct 1 ms 460 KB Output is correct
21 Correct 4 ms 688 KB Output is correct
22 Correct 17 ms 2092 KB Output is correct
23 Correct 2 ms 588 KB Output is correct
24 Correct 49 ms 2444 KB Output is correct
25 Correct 28 ms 2092 KB Output is correct
26 Correct 183 ms 14488 KB Output is correct
27 Correct 173 ms 5048 KB Output is correct
28 Correct 53 ms 3720 KB Output is correct
29 Correct 134 ms 4364 KB Output is correct
30 Correct 163 ms 4508 KB Output is correct
31 Correct 155 ms 4776 KB Output is correct
32 Correct 134 ms 4380 KB Output is correct
33 Correct 58 ms 14612 KB Output is correct
34 Correct 459 ms 22248 KB Output is correct
35 Correct 421 ms 22264 KB Output is correct
36 Correct 487 ms 21888 KB Output is correct
37 Correct 504 ms 21988 KB Output is correct
38 Correct 514 ms 22400 KB Output is correct
39 Correct 493 ms 20968 KB Output is correct
40 Correct 526 ms 22184 KB Output is correct
41 Correct 122 ms 18744 KB Output is correct
42 Correct 192 ms 24548 KB Output is correct
43 Correct 356 ms 12560 KB Output is correct
44 Correct 377 ms 21920 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 460 KB Output is correct
2 Correct 3 ms 580 KB Output is correct
3 Correct 3 ms 576 KB Output is correct
4 Correct 5 ms 556 KB Output is correct
5 Correct 3 ms 556 KB Output is correct
6 Correct 2 ms 556 KB Output is correct
7 Correct 2 ms 556 KB Output is correct
8 Correct 4 ms 588 KB Output is correct
9 Correct 2 ms 588 KB Output is correct
10 Correct 3 ms 556 KB Output is correct
11 Correct 3 ms 556 KB Output is correct
12 Correct 3 ms 500 KB Output is correct
13 Correct 1 ms 324 KB Output is correct
14 Correct 2 ms 444 KB Output is correct
15 Correct 3 ms 588 KB Output is correct
16 Correct 4 ms 556 KB Output is correct
17 Correct 3 ms 556 KB Output is correct
18 Correct 3 ms 556 KB Output is correct
19 Correct 3 ms 588 KB Output is correct
20 Correct 9 ms 556 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 460 KB Output is correct
2 Correct 3 ms 580 KB Output is correct
3 Correct 3 ms 576 KB Output is correct
4 Correct 5 ms 556 KB Output is correct
5 Correct 3 ms 556 KB Output is correct
6 Correct 2 ms 556 KB Output is correct
7 Correct 2 ms 556 KB Output is correct
8 Correct 4 ms 588 KB Output is correct
9 Correct 2 ms 588 KB Output is correct
10 Correct 3 ms 556 KB Output is correct
11 Correct 3 ms 556 KB Output is correct
12 Correct 3 ms 500 KB Output is correct
13 Correct 1 ms 324 KB Output is correct
14 Correct 2 ms 444 KB Output is correct
15 Correct 3 ms 588 KB Output is correct
16 Correct 4 ms 556 KB Output is correct
17 Correct 3 ms 556 KB Output is correct
18 Correct 3 ms 556 KB Output is correct
19 Correct 3 ms 588 KB Output is correct
20 Correct 9 ms 556 KB Output is correct
21 Correct 42 ms 3008 KB Output is correct
22 Correct 48 ms 3008 KB Output is correct
23 Correct 52 ms 2996 KB Output is correct
24 Correct 43 ms 3008 KB Output is correct
25 Correct 46 ms 3008 KB Output is correct
26 Correct 10 ms 776 KB Output is correct
27 Correct 62 ms 3412 KB Output is correct
28 Correct 48 ms 3008 KB Output is correct
29 Correct 46 ms 3000 KB Output is correct
30 Correct 48 ms 3008 KB Output is correct
31 Correct 49 ms 3004 KB Output is correct
32 Correct 34 ms 2104 KB Output is correct
33 Correct 339 ms 3008 KB Output is correct
34 Correct 50 ms 2996 KB Output is correct
35 Correct 42 ms 3008 KB Output is correct
36 Correct 45 ms 3008 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 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 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 1 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 1 ms 204 KB Output is correct
14 Correct 1 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 1 ms 332 KB Output is correct
18 Correct 5 ms 780 KB Output is correct
19 Correct 2 ms 460 KB Output is correct
20 Correct 1 ms 460 KB Output is correct
21 Correct 4 ms 688 KB Output is correct
22 Correct 17 ms 2092 KB Output is correct
23 Correct 2 ms 588 KB Output is correct
24 Correct 49 ms 2444 KB Output is correct
25 Correct 28 ms 2092 KB Output is correct
26 Correct 183 ms 14488 KB Output is correct
27 Correct 173 ms 5048 KB Output is correct
28 Correct 53 ms 3720 KB Output is correct
29 Correct 134 ms 4364 KB Output is correct
30 Correct 163 ms 4508 KB Output is correct
31 Correct 155 ms 4776 KB Output is correct
32 Correct 134 ms 4380 KB Output is correct
33 Correct 58 ms 14612 KB Output is correct
34 Correct 459 ms 22248 KB Output is correct
35 Correct 421 ms 22264 KB Output is correct
36 Correct 487 ms 21888 KB Output is correct
37 Correct 504 ms 21988 KB Output is correct
38 Correct 514 ms 22400 KB Output is correct
39 Correct 493 ms 20968 KB Output is correct
40 Correct 526 ms 22184 KB Output is correct
41 Correct 122 ms 18744 KB Output is correct
42 Correct 192 ms 24548 KB Output is correct
43 Correct 356 ms 12560 KB Output is correct
44 Correct 377 ms 21920 KB Output is correct
45 Correct 2 ms 460 KB Output is correct
46 Correct 3 ms 580 KB Output is correct
47 Correct 3 ms 576 KB Output is correct
48 Correct 5 ms 556 KB Output is correct
49 Correct 3 ms 556 KB Output is correct
50 Correct 2 ms 556 KB Output is correct
51 Correct 2 ms 556 KB Output is correct
52 Correct 4 ms 588 KB Output is correct
53 Correct 2 ms 588 KB Output is correct
54 Correct 3 ms 556 KB Output is correct
55 Correct 3 ms 556 KB Output is correct
56 Correct 3 ms 500 KB Output is correct
57 Correct 1 ms 324 KB Output is correct
58 Correct 2 ms 444 KB Output is correct
59 Correct 3 ms 588 KB Output is correct
60 Correct 4 ms 556 KB Output is correct
61 Correct 3 ms 556 KB Output is correct
62 Correct 3 ms 556 KB Output is correct
63 Correct 3 ms 588 KB Output is correct
64 Correct 9 ms 556 KB Output is correct
65 Correct 42 ms 3008 KB Output is correct
66 Correct 48 ms 3008 KB Output is correct
67 Correct 52 ms 2996 KB Output is correct
68 Correct 43 ms 3008 KB Output is correct
69 Correct 46 ms 3008 KB Output is correct
70 Correct 10 ms 776 KB Output is correct
71 Correct 62 ms 3412 KB Output is correct
72 Correct 48 ms 3008 KB Output is correct
73 Correct 46 ms 3000 KB Output is correct
74 Correct 48 ms 3008 KB Output is correct
75 Correct 49 ms 3004 KB Output is correct
76 Correct 34 ms 2104 KB Output is correct
77 Correct 339 ms 3008 KB Output is correct
78 Correct 50 ms 2996 KB Output is correct
79 Correct 42 ms 3008 KB Output is correct
80 Correct 45 ms 3008 KB Output is correct
81 Correct 342 ms 13816 KB Output is correct
82 Correct 341 ms 13864 KB Output is correct
83 Correct 471 ms 20956 KB Output is correct
84 Correct 1858 ms 21012 KB Output is correct
85 Correct 759 ms 25952 KB Output is correct
86 Correct 732 ms 25960 KB Output is correct
87 Correct 505 ms 12980 KB Output is correct
88 Correct 684 ms 25984 KB Output is correct
89 Correct 736 ms 25976 KB Output is correct
90 Correct 870 ms 20928 KB Output is correct
91 Correct 501 ms 8188 KB Output is correct
92 Correct 773 ms 25968 KB Output is correct
93 Correct 749 ms 25980 KB Output is correct
94 Correct 915 ms 25988 KB Output is correct
95 Correct 787 ms 25992 KB Output is correct
96 Correct 738 ms 25928 KB Output is correct
97 Correct 744 ms 26000 KB Output is correct
98 Correct 330 ms 23100 KB Output is correct