답안 #1001289

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1001289 2024-06-18T18:15:14 Z Tob Cultivation (JOI17_cultivation) C++14
100 / 100
836 ms 2788 KB
#include <bits/stdc++.h>
 
#pragma GCC optimize("Ofast")
 
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define pb push_back
#define FIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
 
using namespace std;
 
typedef long long ll;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
 
const int N = 307, inf = 2e9;
 
int n, m, k, siz;
int a[N], b[N];
int mem[2*N][N];
pii gr[2*N][N];
array <ll,3> vv[2*N];
ll q[6*N], dq[6*N];
 
struct MQ {
	int type, l1, r1, l2, r2;
	inline void push(ll x) {q[++r1] = x; while (l2 <= r2 && dq[r2] < x) r2--; dq[++r2] = x;}
	inline void pop() {if (dq[l2] == q[l1]) l2++; l1++;}
	inline ll get() {return dq[l2];}
	inline void reset() {l1 = l2 = type*2*N; r1 = r2 = l1-1;}
} m1, m2, m3;
 
int main () {
	FIO;
	m1.type = 0;
	m2.type = 1;
	m3.type = 2;
	cin >> n >> m >> k;
	
	vector <pii> so;
	for (int i = 0; i < k; i++) {
		cin >> a[i] >> b[i];
		a[i]--; b[i]--;
		so.pb({a[i], b[i]});
	}
	sort(all(so));
	for (int i = 0; i < k; i++) a[i] = so[i].F, b[i] = so[i].S;
	
	int d = a[0]; for (int i = 0; i < k; i++) a[i] -= d;
	d = n-a[k-1]-1; for (int i = 1; i < k; i++) d = max(d, a[i]-a[i-1]-1);
	a[k] = n-1;
	vector <ll> v; v.pb(d);
	for (int i = 0; i <= k; i++) {
		for (int j = i; j <= k; j++) {
			if (a[j]-a[i]-1 >= d) v.pb(a[j]-a[i]-1);
			if (a[j]+n-a[i]-1 >= d) v.pb(a[j]+n-a[i]-1);
			if (a[i]+n-a[j]-1 >= d) v.pb(a[i]+n-a[j]-1);
		}
		if (n-a[i]-1 >= d) v.pb(n-a[i]-1);
	}
	sort(all(v)); v.erase(unique(all(v)), v.end());
	
	//PRECOMPUTE -> O(N^2 log N)
	for (int i = 0; i < k; i++) {
		set <int> s;
		multiset <int> rj;
		rj.insert(0);
		for (int j = i; j >= 0; j--) {
			mem[i][j] = mem[i][j+1];
			gr[i][j].F = gr[i][j+1].F;
			gr[i][j].S = gr[i][j+1].S;
			if (s.find(b[j]) != s.end()) continue;
			auto p1 = s.lower_bound(b[j]);
			if (p1 == s.end()) {
				if (!s.empty()) rj.insert(b[j]-*(--p1)-1);
				s.insert(b[j]);
				gr[i][j] = {*s.begin(), m-*s.rbegin()-1};
			}
			else if (p1 == s.begin()) {
				rj.insert(*p1-b[j]-1);
				s.insert(b[j]);
				gr[i][j] = {*s.begin(), m-*s.rbegin()-1};
			}
			else {
				int x = *p1; --p1; int y = *p1;
				rj.erase(rj.find(x-y-1));
				rj.insert(x-b[j]-1);
				rj.insert(b[j]-y-1);
				s.insert(b[j]);
			}
			mem[i][j] = *rj.rbegin();
		}
	}
	
	//SOLVE O(N^3)
	ll mn = inf;
	for (auto x : v) {
		m1.reset(); m2.reset(); m3.reset(); siz = 0;
		int l = 0, r = -1, z = 0;
		for (int i = 0; i < k; i++) {
			while (a[z]+x < a[i]) {
				int gt = (x+a[z]+1 < 2LL*n) ? x+a[z]+1 : 2*n;
				if (vv[siz-1][2] != gt) {vv[siz++] = {l+1, r, gt}; l++;}
				else vv[siz-1][0]++, l++;
				z++;
			}
			if (!siz || vv[siz-1][2] != a[i]) {vv[siz++] = {l, r+1, a[i]}; r++;}
			else vv[siz-1][1]++, r++;
		}
		while (z < k) {
			int gt = (x+a[z]+1 < 2LL*n) ? x+a[z]+1 : 2*n;
			if (vv[siz-1][2] != gt) {vv[siz++] = {l+1, r, gt}; l++;}
			else vv[siz-1][0]++, l++;
			z++;
		}
		z = 0;
		#define C (x+max(m1.get(), m2.get()+m3.get()))
		
		for (int i = 0; i+1 < siz && vv[i][2] < 2*n; i++) {
			l = vv[i][0];
			r = vv[i][1];
			m1.push(mem[r][l]); m2.push(gr[r][l].F); m3.push(gr[r][l].S);
			while (vv[z][2]+n-1 < vv[i+1][2]) {
				mn = min(mn, C);
				m1.pop(); m2.pop(); m3.pop();
				z++;
			}
		}
	}
	
	cout << mn << "\n";
 
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 344 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 344 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 1 ms 344 KB Output is correct
18 Correct 1 ms 840 KB Output is correct
19 Correct 1 ms 604 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 0 ms 604 KB Output is correct
22 Correct 2 ms 1248 KB Output is correct
23 Correct 0 ms 604 KB Output is correct
24 Correct 3 ms 1752 KB Output is correct
25 Correct 3 ms 1240 KB Output is correct
26 Correct 6 ms 2596 KB Output is correct
27 Correct 7 ms 2768 KB Output is correct
28 Correct 4 ms 1752 KB Output is correct
29 Correct 9 ms 2772 KB Output is correct
30 Correct 7 ms 2768 KB Output is correct
31 Correct 7 ms 2772 KB Output is correct
32 Correct 9 ms 2772 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 344 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 1 ms 344 KB Output is correct
18 Correct 1 ms 840 KB Output is correct
19 Correct 1 ms 604 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 0 ms 604 KB Output is correct
22 Correct 2 ms 1248 KB Output is correct
23 Correct 0 ms 604 KB Output is correct
24 Correct 3 ms 1752 KB Output is correct
25 Correct 3 ms 1240 KB Output is correct
26 Correct 6 ms 2596 KB Output is correct
27 Correct 7 ms 2768 KB Output is correct
28 Correct 4 ms 1752 KB Output is correct
29 Correct 9 ms 2772 KB Output is correct
30 Correct 7 ms 2768 KB Output is correct
31 Correct 7 ms 2772 KB Output is correct
32 Correct 9 ms 2772 KB Output is correct
33 Correct 12 ms 2520 KB Output is correct
34 Correct 11 ms 2772 KB Output is correct
35 Correct 15 ms 2772 KB Output is correct
36 Correct 17 ms 2576 KB Output is correct
37 Correct 17 ms 2772 KB Output is correct
38 Correct 17 ms 2772 KB Output is correct
39 Correct 17 ms 2708 KB Output is correct
40 Correct 15 ms 2772 KB Output is correct
41 Correct 11 ms 2264 KB Output is correct
42 Correct 11 ms 2520 KB Output is correct
43 Correct 14 ms 2772 KB Output is correct
44 Correct 12 ms 2788 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 600 KB Output is correct
3 Correct 1 ms 604 KB Output is correct
4 Correct 1 ms 604 KB Output is correct
5 Correct 1 ms 604 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 0 ms 604 KB Output is correct
8 Correct 1 ms 604 KB Output is correct
9 Correct 1 ms 604 KB Output is correct
10 Correct 1 ms 604 KB Output is correct
11 Correct 1 ms 604 KB Output is correct
12 Correct 1 ms 604 KB Output is correct
13 Correct 0 ms 604 KB Output is correct
14 Correct 1 ms 604 KB Output is correct
15 Correct 1 ms 608 KB Output is correct
16 Correct 1 ms 604 KB Output is correct
17 Correct 1 ms 604 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 1 ms 604 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 600 KB Output is correct
3 Correct 1 ms 604 KB Output is correct
4 Correct 1 ms 604 KB Output is correct
5 Correct 1 ms 604 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 0 ms 604 KB Output is correct
8 Correct 1 ms 604 KB Output is correct
9 Correct 1 ms 604 KB Output is correct
10 Correct 1 ms 604 KB Output is correct
11 Correct 1 ms 604 KB Output is correct
12 Correct 1 ms 604 KB Output is correct
13 Correct 0 ms 604 KB Output is correct
14 Correct 1 ms 604 KB Output is correct
15 Correct 1 ms 608 KB Output is correct
16 Correct 1 ms 604 KB Output is correct
17 Correct 1 ms 604 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 1 ms 604 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 22 ms 1008 KB Output is correct
22 Correct 35 ms 1120 KB Output is correct
23 Correct 24 ms 860 KB Output is correct
24 Correct 23 ms 972 KB Output is correct
25 Correct 35 ms 1116 KB Output is correct
26 Correct 13 ms 860 KB Output is correct
27 Correct 31 ms 1116 KB Output is correct
28 Correct 32 ms 1116 KB Output is correct
29 Correct 32 ms 856 KB Output is correct
30 Correct 33 ms 868 KB Output is correct
31 Correct 33 ms 1096 KB Output is correct
32 Correct 29 ms 1116 KB Output is correct
33 Correct 25 ms 856 KB Output is correct
34 Correct 33 ms 1120 KB Output is correct
35 Correct 35 ms 1116 KB Output is correct
36 Correct 22 ms 1016 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 344 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 1 ms 344 KB Output is correct
18 Correct 1 ms 840 KB Output is correct
19 Correct 1 ms 604 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 0 ms 604 KB Output is correct
22 Correct 2 ms 1248 KB Output is correct
23 Correct 0 ms 604 KB Output is correct
24 Correct 3 ms 1752 KB Output is correct
25 Correct 3 ms 1240 KB Output is correct
26 Correct 6 ms 2596 KB Output is correct
27 Correct 7 ms 2768 KB Output is correct
28 Correct 4 ms 1752 KB Output is correct
29 Correct 9 ms 2772 KB Output is correct
30 Correct 7 ms 2768 KB Output is correct
31 Correct 7 ms 2772 KB Output is correct
32 Correct 9 ms 2772 KB Output is correct
33 Correct 12 ms 2520 KB Output is correct
34 Correct 11 ms 2772 KB Output is correct
35 Correct 15 ms 2772 KB Output is correct
36 Correct 17 ms 2576 KB Output is correct
37 Correct 17 ms 2772 KB Output is correct
38 Correct 17 ms 2772 KB Output is correct
39 Correct 17 ms 2708 KB Output is correct
40 Correct 15 ms 2772 KB Output is correct
41 Correct 11 ms 2264 KB Output is correct
42 Correct 11 ms 2520 KB Output is correct
43 Correct 14 ms 2772 KB Output is correct
44 Correct 12 ms 2788 KB Output is correct
45 Correct 1 ms 348 KB Output is correct
46 Correct 1 ms 600 KB Output is correct
47 Correct 1 ms 604 KB Output is correct
48 Correct 1 ms 604 KB Output is correct
49 Correct 1 ms 604 KB Output is correct
50 Correct 1 ms 604 KB Output is correct
51 Correct 0 ms 604 KB Output is correct
52 Correct 1 ms 604 KB Output is correct
53 Correct 1 ms 604 KB Output is correct
54 Correct 1 ms 604 KB Output is correct
55 Correct 1 ms 604 KB Output is correct
56 Correct 1 ms 604 KB Output is correct
57 Correct 0 ms 604 KB Output is correct
58 Correct 1 ms 604 KB Output is correct
59 Correct 1 ms 608 KB Output is correct
60 Correct 1 ms 604 KB Output is correct
61 Correct 1 ms 604 KB Output is correct
62 Correct 0 ms 348 KB Output is correct
63 Correct 1 ms 604 KB Output is correct
64 Correct 1 ms 348 KB Output is correct
65 Correct 22 ms 1008 KB Output is correct
66 Correct 35 ms 1120 KB Output is correct
67 Correct 24 ms 860 KB Output is correct
68 Correct 23 ms 972 KB Output is correct
69 Correct 35 ms 1116 KB Output is correct
70 Correct 13 ms 860 KB Output is correct
71 Correct 31 ms 1116 KB Output is correct
72 Correct 32 ms 1116 KB Output is correct
73 Correct 32 ms 856 KB Output is correct
74 Correct 33 ms 868 KB Output is correct
75 Correct 33 ms 1096 KB Output is correct
76 Correct 29 ms 1116 KB Output is correct
77 Correct 25 ms 856 KB Output is correct
78 Correct 33 ms 1120 KB Output is correct
79 Correct 35 ms 1116 KB Output is correct
80 Correct 22 ms 1016 KB Output is correct
81 Correct 371 ms 2000 KB Output is correct
82 Correct 341 ms 2000 KB Output is correct
83 Correct 378 ms 2260 KB Output is correct
84 Correct 447 ms 2256 KB Output is correct
85 Correct 825 ms 2772 KB Output is correct
86 Correct 777 ms 2760 KB Output is correct
87 Correct 514 ms 2772 KB Output is correct
88 Correct 569 ms 2264 KB Output is correct
89 Correct 636 ms 2520 KB Output is correct
90 Correct 422 ms 2004 KB Output is correct
91 Correct 750 ms 2520 KB Output is correct
92 Correct 784 ms 2768 KB Output is correct
93 Correct 618 ms 2520 KB Output is correct
94 Correct 760 ms 2772 KB Output is correct
95 Correct 803 ms 2772 KB Output is correct
96 Correct 805 ms 2520 KB Output is correct
97 Correct 836 ms 2768 KB Output is correct
98 Correct 12 ms 2260 KB Output is correct