Submission #49272

# Submission time Handle Problem Language Result Execution time Memory
49272 2018-05-24T17:29:58 Z Noam527 Circle selection (APIO18_circle_selection) C++17
100 / 100
2801 ms 241556 KB
#include <bits/stdc++.h>
#define endl '\n'
#define flush fflush(stdout), cout.flush()
#define fast ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define debug cout << "ok" << endl
#define finish(x) return cout << x << endl, 0
#define yesno(X) cout << ((X) ? "YES" : "NO") << endl
#define noyes(X) cout << ((X) ? "NO" : "YES") << endl
typedef long long ll;
typedef long double ldb;
const int md = 1e9 + 7, inf = 1e9 + 7;
const ll hs = 199;
const ldb eps = 1e-9, pi = acos(-1);
using namespace std;

const ll MAGIC = 2;

ll sqr(ll x) {
	return x * x;
}

struct circle {
	ll x, y, r, ind, ord;
	circle(int xx = 0, int yy = 0, int rr = 0, int ii = 0) {
		x = xx;
		y = yy;
		r = rr;
		ind = ii;
	}
	int intersect(const circle &a) const {
		if (sqr(x - a.x) + sqr(y - a.y) <= sqr(r + a.r)) return ord;
		return inf;
	}
	bool operator < (const circle &a) const {
		if (y != a.y) return y < a.y;
		return ind < a.ind;
	}
};

struct grid {
	vector<int> comp;
	vector<set<circle>> G;
	int maxr;
	grid(const vector<int> &sortedx, int R = 1) {
		maxr = R;
		comp.push_back(sortedx[0] / R);
		for (int i = 1; i < sortedx.size(); i++) {
			if (comp.back() < sortedx[i] / R)
				comp.push_back(sortedx[i] / R);
		}
		G.resize(comp.size());
	}
	int lowerbound(int x) {
		static int lo, hi, mid;
		lo = 0, hi = comp.size() - 1;
		while (lo < hi) {
			mid = (lo + hi) / 2;
			if (comp[mid] < x) lo = mid + 1;
			else hi = mid;
		}
		if (comp[lo] < x) lo++;
		return lo;
	}
	int find(const circle &a) {
		int X = a.x / maxr;
		int rtn = inf, best = inf;
		int i = lowerbound(X - MAGIC);
		set<circle>::iterator it;
		while (i < comp.size() && comp[i] <= X + MAGIC) {
			it = G[i].lower_bound(circle(0, a.y - MAGIC * maxr - 1, 0, 0));
			while (it != G[i].end() && abs(a.y - it->y) <= MAGIC * maxr + 1) {
//				cout << "found something" << endl;
//				cout << "iterator is " << it->x << " " << it->y << " " << it->r << endl;
//				cout << "while finding for " << a.x << " " << a.y << " " << a.r << endl;
				if (it->intersect(a) < best) {
					best = it->ord;
					rtn = it->ind;
				}
				it++;
			}
			i++;
		}
		return rtn;
	}
	void insert(const circle &a) {
		G[lowerbound(a.x / maxr)].insert(a);
	}
};

int n;
vector<circle> order;
vector<int> sortedx;
vector<grid> G;
int curR;
vector<int> ans;

bool ordcmp(const circle &a, const circle &b) {
	if (a.r != b.r) return a.r > b.r;
	return a.ind < b.ind;
}

int main() {
	fast;
	cin >> n;
	order.resize(n);
	sortedx.resize(n);
	ans.resize(n);
	for (int i = 0, p[3]; i < n; i++) {
		cin >> p[0] >> p[1] >> p[2];
		p[0] += 1e9, p[1] += 1e9;
		order[i] = circle(p[0], p[1], p[2], i);
		sortedx[i] = p[0];
	}
	sort(order.begin(), order.end(), ordcmp);
	for (int i = 0; i < n; i++) order[i].ord = i;
	sort(sortedx.begin(), sortedx.end());
	curR = order[0].r;
	G.push_back(grid(sortedx, curR));
	G[0].insert(order[0]);
	ans[order[0].ind] = order[0].ind;
	for (int i = 1, tmp; i < n; i++) {
		tmp = inf;
		for (int j = 0; tmp == inf && j < G.size(); j++) {
			tmp = G[j].find(order[i]);
		}
		if (tmp < inf) ans[order[i].ind] = tmp;
		else {
			ans[order[i].ind] = order[i].ind;
			if (2 * order[i].r <= curR) {
				curR = order[i].r;
				G.push_back(grid(sortedx, curR));
			}
			G.back().insert(order[i]);
		}
	}
	for (const auto &i : ans) cout << i + 1 << " "; cout << endl;
//	for (int i = 0; i < min(n, 10); i++) cout << order[i].ind << " "; cout << endl;
//	cout << order[0].intersect(order[1]) << endl;
}

Compilation message

circle_selection.cpp: In constructor 'grid::grid(const std::vector<int>&, int)':
circle_selection.cpp:47:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for (int i = 1; i < sortedx.size(); i++) {
                   ~~^~~~~~~~~~~~~~~~
circle_selection.cpp: In member function 'int grid::find(const circle&)':
circle_selection.cpp:69:12: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   while (i < comp.size() && comp[i] <= X + MAGIC) {
          ~~^~~~~~~~~~~~~
circle_selection.cpp: In function 'int main()':
circle_selection.cpp:123:35: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for (int j = 0; tmp == inf && j < G.size(); j++) {
                                 ~~^~~~~~~~~~
circle_selection.cpp:136:2: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
  for (const auto &i : ans) cout << i + 1 << " "; cout << endl;
  ^~~
circle_selection.cpp:136:50: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
  for (const auto &i : ans) cout << i + 1 << " "; cout << endl;
                                                  ^~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 248 KB Output is correct
2 Correct 2 ms 356 KB Output is correct
3 Correct 2 ms 432 KB Output is correct
4 Correct 3 ms 636 KB Output is correct
5 Correct 2 ms 636 KB Output is correct
6 Correct 2 ms 636 KB Output is correct
7 Correct 2 ms 636 KB Output is correct
8 Correct 2 ms 636 KB Output is correct
9 Correct 2 ms 636 KB Output is correct
10 Correct 2 ms 636 KB Output is correct
11 Correct 2 ms 712 KB Output is correct
12 Correct 2 ms 712 KB Output is correct
13 Correct 2 ms 712 KB Output is correct
14 Correct 2 ms 712 KB Output is correct
15 Correct 2 ms 712 KB Output is correct
16 Correct 2 ms 712 KB Output is correct
17 Correct 4 ms 712 KB Output is correct
18 Correct 3 ms 712 KB Output is correct
19 Correct 5 ms 888 KB Output is correct
20 Correct 5 ms 888 KB Output is correct
21 Correct 7 ms 904 KB Output is correct
22 Correct 12 ms 4120 KB Output is correct
23 Correct 13 ms 4120 KB Output is correct
24 Correct 12 ms 4372 KB Output is correct
25 Correct 12 ms 4372 KB Output is correct
26 Correct 14 ms 4372 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 251 ms 16836 KB Output is correct
2 Correct 251 ms 16936 KB Output is correct
3 Correct 288 ms 16936 KB Output is correct
4 Correct 272 ms 16936 KB Output is correct
5 Correct 411 ms 38316 KB Output is correct
6 Correct 531 ms 113364 KB Output is correct
7 Correct 394 ms 113364 KB Output is correct
8 Correct 413 ms 113364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 113364 KB Output is correct
2 Correct 359 ms 113364 KB Output is correct
3 Correct 1213 ms 240712 KB Output is correct
4 Correct 1010 ms 241556 KB Output is correct
5 Correct 1266 ms 241556 KB Output is correct
6 Correct 504 ms 241556 KB Output is correct
7 Correct 217 ms 241556 KB Output is correct
8 Correct 27 ms 241556 KB Output is correct
9 Correct 1378 ms 241556 KB Output is correct
10 Correct 1106 ms 241556 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 666 ms 241556 KB Output is correct
2 Correct 465 ms 241556 KB Output is correct
3 Correct 448 ms 241556 KB Output is correct
4 Correct 509 ms 241556 KB Output is correct
5 Correct 463 ms 241556 KB Output is correct
6 Correct 380 ms 241556 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 248 KB Output is correct
2 Correct 2 ms 356 KB Output is correct
3 Correct 2 ms 432 KB Output is correct
4 Correct 3 ms 636 KB Output is correct
5 Correct 2 ms 636 KB Output is correct
6 Correct 2 ms 636 KB Output is correct
7 Correct 2 ms 636 KB Output is correct
8 Correct 2 ms 636 KB Output is correct
9 Correct 2 ms 636 KB Output is correct
10 Correct 2 ms 636 KB Output is correct
11 Correct 2 ms 712 KB Output is correct
12 Correct 2 ms 712 KB Output is correct
13 Correct 2 ms 712 KB Output is correct
14 Correct 2 ms 712 KB Output is correct
15 Correct 2 ms 712 KB Output is correct
16 Correct 2 ms 712 KB Output is correct
17 Correct 4 ms 712 KB Output is correct
18 Correct 3 ms 712 KB Output is correct
19 Correct 5 ms 888 KB Output is correct
20 Correct 5 ms 888 KB Output is correct
21 Correct 7 ms 904 KB Output is correct
22 Correct 12 ms 4120 KB Output is correct
23 Correct 13 ms 4120 KB Output is correct
24 Correct 12 ms 4372 KB Output is correct
25 Correct 12 ms 4372 KB Output is correct
26 Correct 14 ms 4372 KB Output is correct
27 Correct 17 ms 241556 KB Output is correct
28 Correct 12 ms 241556 KB Output is correct
29 Correct 10 ms 241556 KB Output is correct
30 Correct 33 ms 241556 KB Output is correct
31 Correct 26 ms 241556 KB Output is correct
32 Correct 25 ms 241556 KB Output is correct
33 Correct 109 ms 241556 KB Output is correct
34 Correct 97 ms 241556 KB Output is correct
35 Correct 104 ms 241556 KB Output is correct
36 Correct 318 ms 241556 KB Output is correct
37 Correct 261 ms 241556 KB Output is correct
38 Correct 274 ms 241556 KB Output is correct
39 Correct 362 ms 241556 KB Output is correct
40 Correct 385 ms 241556 KB Output is correct
41 Correct 327 ms 241556 KB Output is correct
42 Correct 164 ms 241556 KB Output is correct
43 Correct 147 ms 241556 KB Output is correct
44 Correct 145 ms 241556 KB Output is correct
45 Correct 157 ms 241556 KB Output is correct
46 Correct 154 ms 241556 KB Output is correct
47 Correct 188 ms 241556 KB Output is correct
48 Correct 153 ms 241556 KB Output is correct
49 Correct 166 ms 241556 KB Output is correct
50 Correct 151 ms 241556 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 248 KB Output is correct
2 Correct 2 ms 356 KB Output is correct
3 Correct 2 ms 432 KB Output is correct
4 Correct 3 ms 636 KB Output is correct
5 Correct 2 ms 636 KB Output is correct
6 Correct 2 ms 636 KB Output is correct
7 Correct 2 ms 636 KB Output is correct
8 Correct 2 ms 636 KB Output is correct
9 Correct 2 ms 636 KB Output is correct
10 Correct 2 ms 636 KB Output is correct
11 Correct 2 ms 712 KB Output is correct
12 Correct 2 ms 712 KB Output is correct
13 Correct 2 ms 712 KB Output is correct
14 Correct 2 ms 712 KB Output is correct
15 Correct 2 ms 712 KB Output is correct
16 Correct 2 ms 712 KB Output is correct
17 Correct 4 ms 712 KB Output is correct
18 Correct 3 ms 712 KB Output is correct
19 Correct 5 ms 888 KB Output is correct
20 Correct 5 ms 888 KB Output is correct
21 Correct 7 ms 904 KB Output is correct
22 Correct 12 ms 4120 KB Output is correct
23 Correct 13 ms 4120 KB Output is correct
24 Correct 12 ms 4372 KB Output is correct
25 Correct 12 ms 4372 KB Output is correct
26 Correct 14 ms 4372 KB Output is correct
27 Correct 251 ms 16836 KB Output is correct
28 Correct 251 ms 16936 KB Output is correct
29 Correct 288 ms 16936 KB Output is correct
30 Correct 272 ms 16936 KB Output is correct
31 Correct 411 ms 38316 KB Output is correct
32 Correct 531 ms 113364 KB Output is correct
33 Correct 394 ms 113364 KB Output is correct
34 Correct 413 ms 113364 KB Output is correct
35 Correct 3 ms 113364 KB Output is correct
36 Correct 359 ms 113364 KB Output is correct
37 Correct 1213 ms 240712 KB Output is correct
38 Correct 1010 ms 241556 KB Output is correct
39 Correct 1266 ms 241556 KB Output is correct
40 Correct 504 ms 241556 KB Output is correct
41 Correct 217 ms 241556 KB Output is correct
42 Correct 27 ms 241556 KB Output is correct
43 Correct 1378 ms 241556 KB Output is correct
44 Correct 1106 ms 241556 KB Output is correct
45 Correct 666 ms 241556 KB Output is correct
46 Correct 465 ms 241556 KB Output is correct
47 Correct 448 ms 241556 KB Output is correct
48 Correct 509 ms 241556 KB Output is correct
49 Correct 463 ms 241556 KB Output is correct
50 Correct 380 ms 241556 KB Output is correct
51 Correct 17 ms 241556 KB Output is correct
52 Correct 12 ms 241556 KB Output is correct
53 Correct 10 ms 241556 KB Output is correct
54 Correct 33 ms 241556 KB Output is correct
55 Correct 26 ms 241556 KB Output is correct
56 Correct 25 ms 241556 KB Output is correct
57 Correct 109 ms 241556 KB Output is correct
58 Correct 97 ms 241556 KB Output is correct
59 Correct 104 ms 241556 KB Output is correct
60 Correct 318 ms 241556 KB Output is correct
61 Correct 261 ms 241556 KB Output is correct
62 Correct 274 ms 241556 KB Output is correct
63 Correct 362 ms 241556 KB Output is correct
64 Correct 385 ms 241556 KB Output is correct
65 Correct 327 ms 241556 KB Output is correct
66 Correct 164 ms 241556 KB Output is correct
67 Correct 147 ms 241556 KB Output is correct
68 Correct 145 ms 241556 KB Output is correct
69 Correct 157 ms 241556 KB Output is correct
70 Correct 154 ms 241556 KB Output is correct
71 Correct 188 ms 241556 KB Output is correct
72 Correct 153 ms 241556 KB Output is correct
73 Correct 166 ms 241556 KB Output is correct
74 Correct 151 ms 241556 KB Output is correct
75 Correct 301 ms 241556 KB Output is correct
76 Correct 286 ms 241556 KB Output is correct
77 Correct 364 ms 241556 KB Output is correct
78 Correct 326 ms 241556 KB Output is correct
79 Correct 341 ms 241556 KB Output is correct
80 Correct 281 ms 241556 KB Output is correct
81 Correct 1279 ms 241556 KB Output is correct
82 Correct 1026 ms 241556 KB Output is correct
83 Correct 1083 ms 241556 KB Output is correct
84 Correct 1336 ms 241556 KB Output is correct
85 Correct 1147 ms 241556 KB Output is correct
86 Correct 1182 ms 241556 KB Output is correct
87 Correct 2069 ms 241556 KB Output is correct
88 Correct 1566 ms 241556 KB Output is correct
89 Correct 1540 ms 241556 KB Output is correct
90 Correct 1603 ms 241556 KB Output is correct
91 Correct 1532 ms 241556 KB Output is correct
92 Correct 1577 ms 241556 KB Output is correct
93 Correct 797 ms 241556 KB Output is correct
94 Correct 1597 ms 241556 KB Output is correct
95 Correct 796 ms 241556 KB Output is correct
96 Correct 956 ms 241556 KB Output is correct
97 Correct 2801 ms 241556 KB Output is correct
98 Correct 754 ms 241556 KB Output is correct
99 Correct 791 ms 241556 KB Output is correct
100 Correct 665 ms 241556 KB Output is correct
101 Correct 756 ms 241556 KB Output is correct
102 Correct 843 ms 241556 KB Output is correct
103 Correct 2293 ms 241556 KB Output is correct
104 Correct 701 ms 241556 KB Output is correct
105 Correct 574 ms 241556 KB Output is correct
106 Correct 517 ms 241556 KB Output is correct
107 Correct 536 ms 241556 KB Output is correct
108 Correct 519 ms 241556 KB Output is correct
109 Correct 505 ms 241556 KB Output is correct
110 Correct 494 ms 241556 KB Output is correct
111 Correct 477 ms 241556 KB Output is correct
112 Correct 511 ms 241556 KB Output is correct
113 Correct 534 ms 241556 KB Output is correct
114 Correct 540 ms 241556 KB Output is correct
115 Correct 521 ms 241556 KB Output is correct
116 Correct 519 ms 241556 KB Output is correct