Submission #356826

# Submission time Handle Problem Language Result Execution time Memory
356826 2021-01-23T18:56:50 Z CaroLinda Circle selection (APIO18_circle_selection) C++14
100 / 100
1233 ms 39740 KB
/*
Proof of complexity: https://en.wikipedia.org/wiki/Circle_packing_in_a_square
*/
#include <bits/stdc++.h>
 
#define debug printf
#define ll long long
#define sz(x) (int)(x.size() )
#define all(x) x.begin(),x.end()
 
const int MAXN = 3e5+10 ;
 
using namespace std ;
 
struct Circle
{
	ll x , y , r ;
 
	Circle(ll x = 0 , ll y = 0 , ll r = 0 ) : x(x), y(y) , r(r) {}
 
} c[MAXN] ;
 
int N , idx ;
int ans[MAXN] , myBlock[MAXN] ;
ll mn[MAXN] , mx[MAXN] , meaning[MAXN] ;
ll G ;
vector<int> byY , byX ;
vector< pair<ll,int> > v[MAXN] ;
 
bool test(int i, int j )
{
	ll dx = c[i].x - c[j].x ;
	ll dy = c[i].y - c[j].y ;
	ll dr = c[i].r + c[j].r ;
	return dx*dx + dy*dy <= dr * dr ;
}
 
void rescale()
{
	for(int i = 0 ; i <= idx ; i++ ) v[i].clear() ;
	
	idx = -1 ;
 
	for(int e : byX )
	{
		if(ans[e] != -1 ) continue ;
		
		if(idx == -1  || (c[e].x/G) > meaning[idx] )
		{
			myBlock[e] = ++idx ;
			meaning[idx] = (c[e].x/G) ;
			mn[idx] = c[e].x ;
		}
		else myBlock[e] = idx ;		

		mx[idx] = c[e].x ;
	}	
 
	for(int e : byY )
	{
		if(ans[e] != -1 ) continue ;
		v[ myBlock[e] ].push_back(make_pair(c[e].y, e) ) ;
	}
 
	/*debug("Printing rescale with radio %d\n", G) ;
	for(int i= 0 ; i <= idx ; i++ )
	{
		debug("In the x %d: ", meaning[i] ) ;
		for(auto e : v[i] ) debug("%d %d, ", e.first, e.second ) ;
		debug("\n") ;
	} */
 
}
 
int main()
{
 
	scanf("%d", &N ) ;
	for(int i = 0 ; i < N ; i++ ) 
	{
		scanf("%lld %lld %lld", &c[i].x, &c[i].y, &c[i].r) ;
		ans[i] = -1 ;
	}
 
	vector<int> byRadio(N) ; iota(all(byRadio), 0 );
	sort(all(byRadio), [&](int i, int j) 
	{
		if( c[i].r != c[j].r ) return c[i].r > c[j].r ;
		return i< j ;
	} ) ;
 
	byY.resize(N) ; iota(all(byY),0) ;
	sort(all(byY), [&](int i, int j ) { return c[i].y < c[j].y ; } ) ;
 
	byX.resize(N) ; iota(all(byX), 0) ;
	sort(all(byX), [&](int i, int j ) { return c[i].x < c[j].x ; } ) ;
 
	G = c[ byRadio[0] ].r * 2LL + 1 ;
 
	for(auto e : byRadio )
	{
		if( ans[e] != -1 ) continue ;
 
		if( c[e].r*2LL < G )
		{
			G = c[e].r ;
			rescale() ;
		}		
 
		int l = myBlock[e] ; 
		while(l > 0 && c[e].x - mx[l] <= 2LL*c[e].r ) { l-- ; }
 
		int r = myBlock[e] ; 
		while( r < idx && mn[r+1]-c[e].x <= 2LL*c[e].r ) { r++ ; } 

		ll lo = c[e].y-2LL*c[e].r ;
		ll hi = c[e].y+2LL*c[e].r ;
 
		for(int i = l ; i <= r ; i++ )
		{
			int t = lower_bound(all(v[i]), make_pair(lo, -1 ) ) - v[i].begin() ;
			for(; t < sz(v[i]) && v[i][t].first <= hi ; t++ )
			{
				if( ans[ v[i][t].second ] != -1 ) continue ;
				if( test(v[i][t].second, e ) ) ans[ v[i][t].second ] = e ;
			}
		}
 
	}
	
	for(int i = 0 ; i < N ; i++ ) printf("%d ", ans[i]+1 ) ;
	printf("\n") ;	 
}

Compilation message

circle_selection.cpp: In function 'int main()':
circle_selection.cpp:78:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   78 |  scanf("%d", &N ) ;
      |  ~~~~~^~~~~~~~~~~
circle_selection.cpp:81:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   81 |   scanf("%lld %lld %lld", &c[i].x, &c[i].y, &c[i].r) ;
      |   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 8 ms 14444 KB Output is correct
2 Correct 8 ms 14444 KB Output is correct
3 Correct 8 ms 14444 KB Output is correct
4 Correct 8 ms 14444 KB Output is correct
5 Correct 8 ms 14444 KB Output is correct
6 Correct 8 ms 14444 KB Output is correct
7 Correct 8 ms 14580 KB Output is correct
8 Correct 8 ms 14444 KB Output is correct
9 Correct 8 ms 14444 KB Output is correct
10 Correct 8 ms 14444 KB Output is correct
11 Correct 8 ms 14444 KB Output is correct
12 Correct 8 ms 14444 KB Output is correct
13 Correct 8 ms 14444 KB Output is correct
14 Correct 8 ms 14464 KB Output is correct
15 Correct 8 ms 14444 KB Output is correct
16 Correct 9 ms 14444 KB Output is correct
17 Correct 9 ms 14444 KB Output is correct
18 Correct 9 ms 14464 KB Output is correct
19 Correct 12 ms 14848 KB Output is correct
20 Correct 12 ms 14828 KB Output is correct
21 Correct 12 ms 14720 KB Output is correct
22 Correct 20 ms 14804 KB Output is correct
23 Correct 27 ms 14700 KB Output is correct
24 Correct 14 ms 14828 KB Output is correct
25 Correct 14 ms 14848 KB Output is correct
26 Correct 14 ms 14828 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 281 ms 28632 KB Output is correct
2 Correct 286 ms 29544 KB Output is correct
3 Correct 289 ms 27080 KB Output is correct
4 Correct 283 ms 28524 KB Output is correct
5 Correct 284 ms 29824 KB Output is correct
6 Correct 499 ms 33692 KB Output is correct
7 Correct 385 ms 30772 KB Output is correct
8 Correct 324 ms 31084 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 14444 KB Output is correct
2 Correct 161 ms 20460 KB Output is correct
3 Correct 656 ms 32492 KB Output is correct
4 Correct 640 ms 32620 KB Output is correct
5 Correct 558 ms 33552 KB Output is correct
6 Correct 229 ms 23848 KB Output is correct
7 Correct 107 ms 19308 KB Output is correct
8 Correct 26 ms 15468 KB Output is correct
9 Correct 626 ms 35680 KB Output is correct
10 Correct 511 ms 35052 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 503 ms 31980 KB Output is correct
2 Correct 445 ms 38704 KB Output is correct
3 Correct 348 ms 28652 KB Output is correct
4 Correct 457 ms 37868 KB Output is correct
5 Correct 457 ms 38516 KB Output is correct
6 Correct 312 ms 27116 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 14444 KB Output is correct
2 Correct 8 ms 14444 KB Output is correct
3 Correct 8 ms 14444 KB Output is correct
4 Correct 8 ms 14444 KB Output is correct
5 Correct 8 ms 14444 KB Output is correct
6 Correct 8 ms 14444 KB Output is correct
7 Correct 8 ms 14580 KB Output is correct
8 Correct 8 ms 14444 KB Output is correct
9 Correct 8 ms 14444 KB Output is correct
10 Correct 8 ms 14444 KB Output is correct
11 Correct 8 ms 14444 KB Output is correct
12 Correct 8 ms 14444 KB Output is correct
13 Correct 8 ms 14444 KB Output is correct
14 Correct 8 ms 14464 KB Output is correct
15 Correct 8 ms 14444 KB Output is correct
16 Correct 9 ms 14444 KB Output is correct
17 Correct 9 ms 14444 KB Output is correct
18 Correct 9 ms 14464 KB Output is correct
19 Correct 12 ms 14848 KB Output is correct
20 Correct 12 ms 14828 KB Output is correct
21 Correct 12 ms 14720 KB Output is correct
22 Correct 20 ms 14804 KB Output is correct
23 Correct 27 ms 14700 KB Output is correct
24 Correct 14 ms 14828 KB Output is correct
25 Correct 14 ms 14848 KB Output is correct
26 Correct 14 ms 14828 KB Output is correct
27 Correct 18 ms 14956 KB Output is correct
28 Correct 19 ms 14956 KB Output is correct
29 Correct 18 ms 14956 KB Output is correct
30 Correct 20 ms 14956 KB Output is correct
31 Correct 20 ms 15084 KB Output is correct
32 Correct 20 ms 15084 KB Output is correct
33 Correct 107 ms 18708 KB Output is correct
34 Correct 104 ms 19168 KB Output is correct
35 Correct 115 ms 18664 KB Output is correct
36 Correct 179 ms 20608 KB Output is correct
37 Correct 147 ms 20588 KB Output is correct
38 Correct 150 ms 20480 KB Output is correct
39 Correct 216 ms 22676 KB Output is correct
40 Correct 208 ms 22620 KB Output is correct
41 Correct 207 ms 22620 KB Output is correct
42 Correct 87 ms 19820 KB Output is correct
43 Correct 112 ms 22380 KB Output is correct
44 Correct 112 ms 22440 KB Output is correct
45 Correct 112 ms 22380 KB Output is correct
46 Correct 116 ms 22252 KB Output is correct
47 Correct 120 ms 22380 KB Output is correct
48 Correct 114 ms 22380 KB Output is correct
49 Correct 136 ms 22380 KB Output is correct
50 Correct 111 ms 22400 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 14444 KB Output is correct
2 Correct 8 ms 14444 KB Output is correct
3 Correct 8 ms 14444 KB Output is correct
4 Correct 8 ms 14444 KB Output is correct
5 Correct 8 ms 14444 KB Output is correct
6 Correct 8 ms 14444 KB Output is correct
7 Correct 8 ms 14580 KB Output is correct
8 Correct 8 ms 14444 KB Output is correct
9 Correct 8 ms 14444 KB Output is correct
10 Correct 8 ms 14444 KB Output is correct
11 Correct 8 ms 14444 KB Output is correct
12 Correct 8 ms 14444 KB Output is correct
13 Correct 8 ms 14444 KB Output is correct
14 Correct 8 ms 14464 KB Output is correct
15 Correct 8 ms 14444 KB Output is correct
16 Correct 9 ms 14444 KB Output is correct
17 Correct 9 ms 14444 KB Output is correct
18 Correct 9 ms 14464 KB Output is correct
19 Correct 12 ms 14848 KB Output is correct
20 Correct 12 ms 14828 KB Output is correct
21 Correct 12 ms 14720 KB Output is correct
22 Correct 20 ms 14804 KB Output is correct
23 Correct 27 ms 14700 KB Output is correct
24 Correct 14 ms 14828 KB Output is correct
25 Correct 14 ms 14848 KB Output is correct
26 Correct 14 ms 14828 KB Output is correct
27 Correct 281 ms 28632 KB Output is correct
28 Correct 286 ms 29544 KB Output is correct
29 Correct 289 ms 27080 KB Output is correct
30 Correct 283 ms 28524 KB Output is correct
31 Correct 284 ms 29824 KB Output is correct
32 Correct 499 ms 33692 KB Output is correct
33 Correct 385 ms 30772 KB Output is correct
34 Correct 324 ms 31084 KB Output is correct
35 Correct 9 ms 14444 KB Output is correct
36 Correct 161 ms 20460 KB Output is correct
37 Correct 656 ms 32492 KB Output is correct
38 Correct 640 ms 32620 KB Output is correct
39 Correct 558 ms 33552 KB Output is correct
40 Correct 229 ms 23848 KB Output is correct
41 Correct 107 ms 19308 KB Output is correct
42 Correct 26 ms 15468 KB Output is correct
43 Correct 626 ms 35680 KB Output is correct
44 Correct 511 ms 35052 KB Output is correct
45 Correct 503 ms 31980 KB Output is correct
46 Correct 445 ms 38704 KB Output is correct
47 Correct 348 ms 28652 KB Output is correct
48 Correct 457 ms 37868 KB Output is correct
49 Correct 457 ms 38516 KB Output is correct
50 Correct 312 ms 27116 KB Output is correct
51 Correct 18 ms 14956 KB Output is correct
52 Correct 19 ms 14956 KB Output is correct
53 Correct 18 ms 14956 KB Output is correct
54 Correct 20 ms 14956 KB Output is correct
55 Correct 20 ms 15084 KB Output is correct
56 Correct 20 ms 15084 KB Output is correct
57 Correct 107 ms 18708 KB Output is correct
58 Correct 104 ms 19168 KB Output is correct
59 Correct 115 ms 18664 KB Output is correct
60 Correct 179 ms 20608 KB Output is correct
61 Correct 147 ms 20588 KB Output is correct
62 Correct 150 ms 20480 KB Output is correct
63 Correct 216 ms 22676 KB Output is correct
64 Correct 208 ms 22620 KB Output is correct
65 Correct 207 ms 22620 KB Output is correct
66 Correct 87 ms 19820 KB Output is correct
67 Correct 112 ms 22380 KB Output is correct
68 Correct 112 ms 22440 KB Output is correct
69 Correct 112 ms 22380 KB Output is correct
70 Correct 116 ms 22252 KB Output is correct
71 Correct 120 ms 22380 KB Output is correct
72 Correct 114 ms 22380 KB Output is correct
73 Correct 136 ms 22380 KB Output is correct
74 Correct 111 ms 22400 KB Output is correct
75 Correct 368 ms 27276 KB Output is correct
76 Correct 371 ms 27824 KB Output is correct
77 Correct 363 ms 28632 KB Output is correct
78 Correct 347 ms 28632 KB Output is correct
79 Correct 390 ms 27236 KB Output is correct
80 Correct 353 ms 28632 KB Output is correct
81 Correct 602 ms 32304 KB Output is correct
82 Correct 593 ms 32748 KB Output is correct
83 Correct 604 ms 32888 KB Output is correct
84 Correct 576 ms 32176 KB Output is correct
85 Correct 592 ms 32492 KB Output is correct
86 Correct 603 ms 32748 KB Output is correct
87 Correct 573 ms 34060 KB Output is correct
88 Correct 1199 ms 39644 KB Output is correct
89 Correct 1207 ms 39648 KB Output is correct
90 Correct 1233 ms 39700 KB Output is correct
91 Correct 1190 ms 39740 KB Output is correct
92 Correct 1204 ms 39660 KB Output is correct
93 Correct 503 ms 38544 KB Output is correct
94 Correct 456 ms 30280 KB Output is correct
95 Correct 515 ms 38636 KB Output is correct
96 Correct 515 ms 38380 KB Output is correct
97 Correct 545 ms 30796 KB Output is correct
98 Correct 470 ms 33408 KB Output is correct
99 Correct 524 ms 38764 KB Output is correct
100 Correct 452 ms 38764 KB Output is correct
101 Correct 482 ms 31468 KB Output is correct
102 Correct 537 ms 38508 KB Output is correct
103 Correct 558 ms 30000 KB Output is correct
104 Correct 506 ms 38636 KB Output is correct
105 Correct 291 ms 30828 KB Output is correct
106 Correct 386 ms 36168 KB Output is correct
107 Correct 383 ms 36204 KB Output is correct
108 Correct 400 ms 36396 KB Output is correct
109 Correct 379 ms 36204 KB Output is correct
110 Correct 385 ms 36168 KB Output is correct
111 Correct 385 ms 36204 KB Output is correct
112 Correct 382 ms 36192 KB Output is correct
113 Correct 382 ms 36252 KB Output is correct
114 Correct 381 ms 36192 KB Output is correct
115 Correct 376 ms 36204 KB Output is correct
116 Correct 404 ms 37816 KB Output is correct