Quoit Design
Problem Description
Have you ever played quoit in a playground? Quoit is a game in which
flat rings are pitched at some toys, with all the toys encircled
awarded. In the field of Cyberground, the position of each toy is fixed,
and the ring is carefully designed so it can only encircle one toy at a
time. On the other hand, to make the game look more attractive, the ring
is designed to have the largest radius. Given a configuration of the
field, you are supposed to find the radius of such a ring.
Assume that all the toys are points on a plane. A point is encircled
by the ring if the distance between the point and the center of the ring
is strictly less than the radius of the ring. If two toys are placed at
the same point, the radius of the ring is considered to be 0.
The input consists of several test cases. For each case, the first
line contains an integer N (2 <= N <= 100,000), the total number
of toys in the field. Then N lines follow, each contains a pair of (x,
y) which are the coordinates of a toy. The input is terminated by N =
0.
Output
For each test case, print in one line the radius of the ring required
by the Cyberground manager, accurate up to 2 decimal places.
2
0 0
1 1
2
1 1
1 1
3
-1.5 0
0 0
0 1.5
0
Sample Output
Author
CHEN, Yue
Source
ZJCPC2004
Code
暴力算法超时 using namespace std;
int main ( ) {
int n;
cin>> n;
while ( n!= 0 ) {
double t[ n] [ 2 ] ;
for ( int i= 0 ; i< n; i++ ) {
cin>> t[ i] [ 0 ] >> t[ i] [ 1 ] ;
}
double dis= 10000000.0 ;
for ( int i= 0 ; i< n; i++ ) {
for ( int j= i+ 1 ; j< n; j++ ) {
double dis_temp= sqrt ( pow ( t[ i] [ 0 ] - t[ j] [ 0 ] , 2 ) + pow ( t[ i] [ 1 ] - t[ j] [ 1 ] , 2 ) ) ;
if ( dis_temp/ 2 < dis)
dis= dis_temp/ 2 ;
}
}
cout<< fixed<< setprecision ( 2 ) << dis<< endl;
cin>> n;
}
return 0 ;
}
Code
分治法 # include <bits/stdc++.h>
using namespace std;
struct point {
double x;
double y;
} ;
bool cmpx ( const point& a, const point& b) {
return a. x < b. x;
}
bool cmpy ( const point& a, const point& b) {
return a. y < b. y;
}
double dist ( const point& a, const point& b) {
return sqrt ( ( a. x- b. x) * ( a. x- b. x) + ( a. y- b. y) * ( a. y- b. y) ) ;
}
double closest ( vector< point> & points, int left, int right) {
if ( right- left<= 3 ) {
double d= DBL_MAX;
for ( int i= left; i<= right; i++ ) {
for ( int j= i+ 1 ; j<= right; j++ ) {
d= min ( d, dist ( points[ i] , points[ j] ) ) ;
}
}
return d;
}
int mid= ( left+ right) / 2 ;
double dl= closest ( points, left, mid) ;
double dr= closest ( points, mid+ 1 , right) ;
double d= min ( dl, dr) ;
vector< point> strip;
for ( int i= left; i<= right; i++ ) {
if ( abs ( points[ i] . x- mid) < d) {
strip. push_back ( points[ i] ) ;
}
}
sort ( strip. begin ( ) , strip. end ( ) , cmpy) ;
for ( int i= 0 ; i< strip. size ( ) ; i++ ) {
for ( int j= i+ 1 ; j< strip. size ( ) ; j++ ) {
if ( strip[ j] . y- strip[ i] . y> d) {
break ;
}
if ( dist ( strip[ i] , strip[ j] ) < d) {
d= dist ( strip[ i] , strip[ j] ) ;
}
}
}
return d;
}
int main ( ) {
int n;
cin>> n;
while ( n) {
vector< point> points ( n) ;
for ( int i= 0 ; i< n; i++ ) {
cin>> points[ i] . x>> points[ i] . y;
}
sort ( points. begin ( ) , points. end ( ) , cmpx) ;
cout<< fixed<< setprecision ( 2 ) << closest ( points, 0 , n- 1 ) / 2 << endl;
cin>> n;
}
return 0 ;
}
Code
分治法优化不适用vector,屏蔽io同步 # include <bits/stdc++.h>
using namespace std;
struct point {
double x;
double y;
} ;
bool cmpx ( const point& a, const point& b) {
return a. x < b. x;
}
bool cmpy ( const point& a, const point& b) {
return a. y < b. y;
}
double dist ( const point& a, const point& b) {
return sqrt ( ( a. x- b. x) * ( a. x- b. x) + ( a. y- b. y) * ( a. y- b. y) ) ;
}
double dist2 ( const point& a, const point& b) {
return ( a. x- b. x) * ( a. x- b. x) + ( a. y- b. y) * ( a. y- b. y) ;
}
double closest ( point* points, int left, int right) {
if ( right- left<= 3 ) {
double d= DBL_MAX;
for ( int i= left; i<= right; i++ ) {
for ( int j= i+ 1 ; j<= right; j++ ) {
d= min ( d, dist2 ( points[ i] , points[ j] ) ) ;
}
}
return d;
}
int mid= ( left+ right) / 2 ;
double dl= closest ( points, left, mid) ;
double dr= closest ( points, mid+ 1 , right) ;
double d= min ( dl, dr) ;
vector< point> strip;
for ( int i= left; i<= right; i++ ) {
if ( abs ( points[ i] . x- points[ mid] . x) < d) {
strip. push_back ( points[ i] ) ;
}
}
sort ( strip. begin ( ) , strip. end ( ) , cmpy) ;
for ( size_t i= 0 ; i< strip. size ( ) ; i++ ) {
for ( size_t j= i+ 1 ; j< min ( strip. size ( ) , i+ 7 ) && ( strip[ j] . y - strip[ i] . y) < d; j++ ) {
d= min ( d, dist ( strip[ i] , strip[ j] ) ) ;
}
}
return d;
}
int main ( ) {
ios_base:: sync_with_stdio ( false ) ;
cin. tie ( NULL ) ;
int n;
cin>> n;
while ( n) {
point points[ n] ;
for ( int i= 0 ; i< n; i++ ) {
cin>> points[ i] . x>> points[ i] . y;
}
sort ( points, points+ n, cmpx) ;
cout<< fixed<< setprecision ( 2 ) << sqrt ( closest ( points, 0 , n- 1 ) ) / 2 << endl;
cin>> n;
}
return 0 ;
}
Code
优化点: - 不使用min函数,用?:表达式 - 使用全局数组 -
使用scanf和printf
但是上述的不足以AC,我也不知道为什么我改了个头文件就AC了哭哭哭啊啊啊啊啊啊啊啊
# include <cstdio>
# include <algorithm>
# include <cmath>
using namespace std;
struct point {
double x;
double y;
} ;
point points[ 100000 ] ;
int strip[ 100000 ] , cur= 0 ;
bool cmpx ( const point& a, const point& b) {
return a. x < b. x;
}
bool cmp_y ( int a, int b) {
return points[ a] . y< points[ b] . y;
}
double dist2 ( const point& a, const point& b) {
return ( a. x- b. x) * ( a. x- b. x) + ( a. y- b. y) * ( a. y- b. y) ;
}
double closest ( int left, int right) {
if ( right- left== 1 ) {
return dist2 ( points[ right] , points[ left] ) ;
}
if ( right- left== 2 ) {
double d1= dist2 ( points[ left] , points[ left+ 1 ] ) ;
double d2= dist2 ( points[ left+ 1 ] , points[ left+ 2 ] ) ;
double d3= dist2 ( points[ left] , points[ left+ 2 ] ) ;
return d3< ( ( d1< d2) ? d1: d2) ? d3: ( ( d1< d2) ? d1: d2) ;
}
int mid= ( left+ right) / 2 ;
double dl= closest ( left, mid) ;
double dr= closest ( mid+ 1 , right) ;
double d= ( dl< dr) ? dl: dr;
cur= 0 ;
for ( int i= left; i<= right; i++ ) {
if ( abs ( points[ i] . x- points[ mid] . x) < d) {
strip[ cur++ ] = i;
}
}
sort ( strip, strip+ cur, cmp_y) ;
for ( int i= 0 ; i< cur; i++ ) {
for ( int j= i+ 1 ; j< cur && j<= i+ 7 ; j++ ) {
if ( points[ strip[ j] ] . y- points[ strip[ i] ] . y> d) {
break ;
}
d= min ( d, dist2 ( points[ strip[ i] ] , points[ strip[ j] ] ) ) ;
}
}
return d;
}
int main ( ) {
int n;
while ( scanf ( "%d" , & n) != EOF && n!= 0 ) {
for ( int i= 0 ; i< n; i++ ) {
scanf ( "%lf%lf" , & points[ i] . x, & points[ i] . y) ;
}
sort ( points, points+ n, cmpx) ;
printf ( "%.2lf\n" , sqrt ( closest ( 0 , n- 1 ) ) / 2 ) ;
}
return 0 ;
}