Submission #881480

# Submission time Handle Problem Language Result Execution time Memory
881480 2023-12-01T09:38:49 Z eitanelb Cultivation (JOI17_cultivation) C++14
100 / 100
612 ms 161620 KB
#include<vector>
#include<iostream>
#include<set>
#include<algorithm>
using namespace std;
#define pi pair<int,int>
#define x first
#define y second
/*
for down - just real distances from down (optional also for left) -> O(n)
for up - just real distances, or such that together it's distance between two -> O(n)+O(n^2)
after I have up and down - O(n^2) //optional - O(nlogu), with segmentree sparse/coordination, then O(n^4 logU)

then O(n^5), or if r<=40 -> O(40*40*40*n)
which is 60 points

for 100 points, let's see that whenever you get the up bigger, it affects only one place
that can being it to O(n^3), which is 100 points
*/


int deb = 0;
pair<int, pi>* merge(pair<int, pi>* v1, pair<int, pi>* v2, int n1, int n2) {
	pair<int, pi>* v3 = new pair<int,pi>[n1+n2];
	int p1 = 0, p2 = 0, p3=0;
	while (p1 < n1 || p2 < n2) {
		if (p1 != n1 && (p2 == n2 || v1[p1].x <= v2[p2].x)) v3[p3++] = v1[p1++];
		else v3[p3++] = v2[p2++];
	}
	return v3;
}
int Time_To_Fool_The_Lab(int r, int c, int n, vector<int> X, vector<int> Y) {
	int* vdown = new int[n];
	pair<int, pi>* vup = new pair<int, pi>[n];
	//int sz = n * (n - 1) / 2;
	int sz = 0;
	for (int i = 0; i < n; i++)for (int j = 0; j < n; j++)if (X[i] > X[j])++sz;
	pair<int, pi>* vsum = new pair<int, pi>[sz];
	int t = 0;
	for (int i = 0; i < n; ++i) {
		vdown[i] = X[i] - 1;
	}
	for (int i = 0; i < n; ++i) {
		vup[i] = {r - X[i], {i, -1}};
		for (int j = 0; j < n; ++j) if (X[i] > X[j]) vsum[t++]={X[i] - X[j] - 1, {i, j}};
	}
	sort(vdown, vdown + n);
	int h = unique(vdown, vdown + n)-vdown;
	sort(vup, vup+n);
	sort(vsum, vsum + sz);
	pair<int, pi>* vnup = new pair<int, pi>[sz];
	int res = 2e9;
	bool* on = new bool[n];
	int* dr = new int[n], * dl = new int[n], * maxi = new int[n], * disl = new int[n], * disr = new int[n];
	for (int d = 0; d < h; d++) {
		int down = vdown[d];
		for (int i = 0; i < sz;i++) vnup[i] = {vsum[i].x - down,vsum[i].y};
		vnup = merge(vnup, vup, sz, n);
		for (int i = 0; i < n; i++) {
			on[i] = 0;
			if (X[i] == r) {
				dr[i] = dl[i] = 0;
				on[i] = 1;
				disl[i] = disr[i] = 0;
				continue;
			}
			dr[i] = c - Y[i];
			dl[i] = Y[i] - 1;
			disl[i] = disr[i] = c;
			for (int j = 0; j < n; j++) {
				if (j == i) continue;
				if (X[j] > X[i] && X[j] - down <= X[i] + 1) {
					if (Y[j] > Y[i]) dr[i] = min(dr[i], Y[j] - Y[i] - 1);
					else if (Y[j] < Y[i]) dl[i] = min(dl[i], Y[i] - Y[j] - 1);
					else on[i] = 1;

					disr[i] = min(disr[i], c - Y[j]);
					disl[i] = min(disl[i], Y[j]-1);
				}
			}
		}
		if (deb) {cout << "disl: "; for (int i = 0; i < n; i++) cout << disl[i] << ' '; cout << endl;}
		if (deb) { cout << "disr: "; for (int i = 0; i < n; i++) cout << disr[i] << ' '; cout << endl; }
		multiset<int> maxs, sdl, sdr;
		for (int i = 0; i < n; i++) {
			if (on[i]) maxi[i] = max(dr[i], dl[i]);
			else maxi[i] = dr[i] + dl[i] + 1;
			maxs.insert(maxi[i]);
			sdl.insert(disl[i]);
			sdr.insert(disr[i]);
		}
		if (deb) {for (int i = 0; i < n; i++) {cout << dl[i] << ' ' << dr[i] << ' ' << maxi[i] << ' ' << on[i] << endl;}}

		// row 1 ************
		int mx1 = 0;
		set<int> st;
		for (int i = 0; i < n; i++)
			if (X[i] - down <= 1)
				st.insert(Y[i]);
		sdl.insert(*st.begin() - 1);
		sdr.insert(c - *prev(st.end()));
		int fr = 0;
		for (int i : st) {
			mx1 = max(mx1, i - fr - 1);
			fr = i;
		}
		maxs.insert(mx1);

		int up = 0;

		for (int q = 0; q < (n + sz);q++) {
			if (deb) { cout << "disl: "; for (int i = 0; i < n; i++) cout << disl[i] << ' '; cout << endl; }
			if (deb) { cout << "disr: "; for (int i = 0; i < n; i++) cout << disr[i] << ' '; cout << endl; }
			pair<int, pi> pr = vnup[q];
			if (deb) { cout << pr.x << ' ' << pr.y.x << ' ' << pr.y.y << endl; }
			up = pr.x;
			if (down + up >= res) continue;
			if (up < 0) continue;
			if (pr.y.y == -1) {
				int u = pr.y.x;
				maxs.erase(maxs.find(maxi[u]));
				sdl.erase(sdl.find(disl[u]));
				sdr.erase(sdr.find(disr[u]));
			}
			else {
				int i = pr.y.x, j = pr.y.y;
				if (Y[i]== Y[j]) on[j] = 1;
				else if (Y[i] > Y[j]) dr[j] = min(dr[j], Y[i] - Y[j] - 1);
				else dl[j] = min(dl[j], Y[j] - Y[i] - 1);

				if (c - Y[i] < disr[j]) {
					sdr.erase(sdr.find(disr[j]));
					disr[j] = c - Y[i];
					sdr.insert(disr[j]);
				}
				if (Y[i] - 1 < disl[j]) {
					sdl.erase(sdl.find(disl[j]));
					disl[j] = Y[i] - 1;
					sdl.insert(disl[j]);
				}
				if (on[j]) {
					if (max(dr[j], dl[j]) < maxi[j]) {
						maxs.erase(maxs.find(maxi[j]));
						maxi[j] = max(dr[j], dl[j]);
						maxs.insert(maxi[j]);
					}
				}
				else {
					if (dr[j] + dl[j] + 1 < maxi[j]) {
						maxs.erase(maxs.find(maxi[j]));
						maxi[j] = dr[j] + dl[j] + 1;
						maxs.insert(maxi[j]);
					}
				}
			}
			int mxla=*prev(sdl.end()), mxfa = *prev(sdr.end());
			int mx = *prev(maxs.end());
			if (mxla >= c || mxfa >= c) continue;
			mx = max(mx, mxla + mxfa);
			if (mx > 2e9 - up - down) continue;
			if (deb) {cout << mx << ' ' << mxla << ' ' << mxfa << ' ' << up << ' ' << down << endl;}
			res = min(res, mx + down + up);
		}
	}
	return res;
}


#include<iostream>
#include<vector>
#include<string>
#include<fstream>
using namespace std;
int run = 1;
int main() {
	ios::sync_with_stdio(0); cin.tie(0);
	if (run == 1) {
		int R, C, N;
		cin >> R >> C >> N;
		vector<int> X(N), Y(N);
		for (int i = 0; i < N; i++) {
			cin >> X[i] >> Y[i];
		}

		int res = Time_To_Fool_The_Lab(R, C, N, X, Y);

		cout << res << endl;
	}
	if (run == 2) {
		vector<int> sizes = {0, 16, 16, 12, 20, 16, 18 };
		string name = "01-01";
		for(int j=6;j<7;j++){
			cout << "****" << j << "****\n";
			name[1] = '0' + j;
			for (int i = 1; i <= sizes[j]; i++) {
				cout << "**" << j << ' ' << i << "**\n";
				name[3] = '0' + i / 10; name[4] = '0' + i % 10;
				ifstream fin(name + ".in"), fout(name + ".out");
				int R, C, N;
				fin >> R >> C >> N;
				vector<int> X(N), Y(N);
				for (int i = 0; i < N; i++) {
					fin >> X[i] >> Y[i];
				}

				int nres, res = Time_To_Fool_The_Lab(R, C, N, X, Y);

				fout >> nres;
				if (nres != res) {
					cout << "subtask " << j << "test" << i << endl;
					cout << R << ' ' << C << endl << N << endl;
					for (int i = 0; i < N; i++) {
						cout << X[i] << ' ' << Y[i] << endl;
					}
					cout << res << ' ' << nres << endl;
					break;
				}
			}
		}
	}
}

# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 344 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 344 KB Output is correct
10 Correct 0 ms 348 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
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 344 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 344 KB Output is correct
10 Correct 0 ms 348 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 348 KB Output is correct
18 Correct 2 ms 1116 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 2 ms 860 KB Output is correct
22 Correct 8 ms 3676 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 21 ms 9808 KB Output is correct
25 Correct 13 ms 5724 KB Output is correct
26 Correct 44 ms 22192 KB Output is correct
27 Correct 50 ms 22204 KB Output is correct
28 Correct 19 ms 9708 KB Output is correct
29 Correct 43 ms 21088 KB Output is correct
30 Correct 43 ms 22080 KB Output is correct
31 Correct 41 ms 22324 KB Output is correct
32 Correct 42 ms 22052 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 344 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 344 KB Output is correct
10 Correct 0 ms 348 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 348 KB Output is correct
18 Correct 2 ms 1116 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 2 ms 860 KB Output is correct
22 Correct 8 ms 3676 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 21 ms 9808 KB Output is correct
25 Correct 13 ms 5724 KB Output is correct
26 Correct 44 ms 22192 KB Output is correct
27 Correct 50 ms 22204 KB Output is correct
28 Correct 19 ms 9708 KB Output is correct
29 Correct 43 ms 21088 KB Output is correct
30 Correct 43 ms 22080 KB Output is correct
31 Correct 41 ms 22324 KB Output is correct
32 Correct 42 ms 22052 KB Output is correct
33 Correct 3 ms 1368 KB Output is correct
34 Correct 54 ms 21804 KB Output is correct
35 Correct 53 ms 22048 KB Output is correct
36 Correct 54 ms 22092 KB Output is correct
37 Correct 49 ms 19540 KB Output is correct
38 Correct 51 ms 22352 KB Output is correct
39 Correct 53 ms 22028 KB Output is correct
40 Correct 51 ms 22292 KB Output is correct
41 Correct 23 ms 8408 KB Output is correct
42 Correct 47 ms 15708 KB Output is correct
43 Correct 53 ms 21364 KB Output is correct
44 Correct 52 ms 22100 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 344 KB Output is correct
15 Correct 1 ms 344 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 496 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 344 KB Output is correct
15 Correct 1 ms 344 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 496 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 14 ms 6236 KB Output is correct
22 Correct 19 ms 6468 KB Output is correct
23 Correct 14 ms 6492 KB Output is correct
24 Correct 12 ms 6368 KB Output is correct
25 Correct 14 ms 6492 KB Output is correct
26 Correct 10 ms 4188 KB Output is correct
27 Correct 25 ms 6488 KB Output is correct
28 Correct 14 ms 6488 KB Output is correct
29 Correct 14 ms 6488 KB Output is correct
30 Correct 15 ms 6492 KB Output is correct
31 Correct 15 ms 6492 KB Output is correct
32 Correct 14 ms 6364 KB Output is correct
33 Correct 10 ms 6492 KB Output is correct
34 Correct 19 ms 6488 KB Output is correct
35 Correct 19 ms 6492 KB Output is correct
36 Correct 12 ms 6492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 344 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 344 KB Output is correct
10 Correct 0 ms 348 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 348 KB Output is correct
18 Correct 2 ms 1116 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 2 ms 860 KB Output is correct
22 Correct 8 ms 3676 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 21 ms 9808 KB Output is correct
25 Correct 13 ms 5724 KB Output is correct
26 Correct 44 ms 22192 KB Output is correct
27 Correct 50 ms 22204 KB Output is correct
28 Correct 19 ms 9708 KB Output is correct
29 Correct 43 ms 21088 KB Output is correct
30 Correct 43 ms 22080 KB Output is correct
31 Correct 41 ms 22324 KB Output is correct
32 Correct 42 ms 22052 KB Output is correct
33 Correct 3 ms 1368 KB Output is correct
34 Correct 54 ms 21804 KB Output is correct
35 Correct 53 ms 22048 KB Output is correct
36 Correct 54 ms 22092 KB Output is correct
37 Correct 49 ms 19540 KB Output is correct
38 Correct 51 ms 22352 KB Output is correct
39 Correct 53 ms 22028 KB Output is correct
40 Correct 51 ms 22292 KB Output is correct
41 Correct 23 ms 8408 KB Output is correct
42 Correct 47 ms 15708 KB Output is correct
43 Correct 53 ms 21364 KB Output is correct
44 Correct 52 ms 22100 KB Output is correct
45 Correct 1 ms 348 KB Output is correct
46 Correct 1 ms 348 KB Output is correct
47 Correct 1 ms 348 KB Output is correct
48 Correct 1 ms 348 KB Output is correct
49 Correct 1 ms 348 KB Output is correct
50 Correct 1 ms 348 KB Output is correct
51 Correct 1 ms 348 KB Output is correct
52 Correct 1 ms 348 KB Output is correct
53 Correct 1 ms 348 KB Output is correct
54 Correct 1 ms 348 KB Output is correct
55 Correct 1 ms 348 KB Output is correct
56 Correct 1 ms 348 KB Output is correct
57 Correct 0 ms 348 KB Output is correct
58 Correct 1 ms 344 KB Output is correct
59 Correct 1 ms 344 KB Output is correct
60 Correct 1 ms 348 KB Output is correct
61 Correct 1 ms 496 KB Output is correct
62 Correct 1 ms 348 KB Output is correct
63 Correct 1 ms 348 KB Output is correct
64 Correct 1 ms 348 KB Output is correct
65 Correct 14 ms 6236 KB Output is correct
66 Correct 19 ms 6468 KB Output is correct
67 Correct 14 ms 6492 KB Output is correct
68 Correct 12 ms 6368 KB Output is correct
69 Correct 14 ms 6492 KB Output is correct
70 Correct 10 ms 4188 KB Output is correct
71 Correct 25 ms 6488 KB Output is correct
72 Correct 14 ms 6488 KB Output is correct
73 Correct 14 ms 6488 KB Output is correct
74 Correct 15 ms 6492 KB Output is correct
75 Correct 15 ms 6492 KB Output is correct
76 Correct 14 ms 6364 KB Output is correct
77 Correct 10 ms 6492 KB Output is correct
78 Correct 19 ms 6488 KB Output is correct
79 Correct 19 ms 6492 KB Output is correct
80 Correct 12 ms 6492 KB Output is correct
81 Correct 123 ms 64580 KB Output is correct
82 Correct 121 ms 64440 KB Output is correct
83 Correct 173 ms 93408 KB Output is correct
84 Correct 421 ms 93268 KB Output is correct
85 Correct 283 ms 161384 KB Output is correct
86 Correct 280 ms 161364 KB Output is correct
87 Correct 248 ms 135788 KB Output is correct
88 Correct 259 ms 161620 KB Output is correct
89 Correct 310 ms 161364 KB Output is correct
90 Correct 185 ms 117692 KB Output is correct
91 Correct 298 ms 161440 KB Output is correct
92 Correct 304 ms 161368 KB Output is correct
93 Correct 313 ms 161356 KB Output is correct
94 Correct 612 ms 161400 KB Output is correct
95 Correct 292 ms 161492 KB Output is correct
96 Correct 332 ms 161364 KB Output is correct
97 Correct 342 ms 161500 KB Output is correct
98 Correct 12 ms 6300 KB Output is correct