POJ2187 Beauty Contest
Source Code
Problem: 2187 User: Snpilola
Memory: 1700K Time: 297MS
Language: C++ Result: Accepted
Source Code
/*---------------------------------
*File name: A.cpp
*Creation date: 2020-12-10 16:57
*Link:
*-------------------------------*/
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<map>
#include<set>
#include<deque>
#include<queue>
#include<stack>
#include<bitset>
#include<string>
#include<fstream>
#include<iostream>
#include<algorithm>
#define fi first
#define se second
#define pb push_back
#define LL long long
#define PII pair<int, int>
using namespace std;
typedef int readType;
const double eps = 1e-8;
const double inf = 1e20;
const double pi = acos(-1.0);
const int maxp = 5e4;
int sgn(double x){
if(fabs(x) < eps)return 0;
if(x < 0)return -1;
else return 1;
}
inline double sqr(double x){return x*x;}
inline readType read(){
readType x = 0, y = 1; char c = getchar();
while(c < '0' || c > '9'){if(c == '-') y = -1; c = getchar();}
while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return x * y;
}
struct Point{
double x,y;
Point(){}
Point(double _x,double _y){
x = _x;
y = _y;
}
void input(){scanf("%lf%lf",&x,&y);}
bool operator == (Point b)const{ return sgn(x-b.x) == 0 && sgn(y-b.y) == 0; }
bool operator < (Point b)const{ return sgn(x-b.x)== 0?sgn(y-b.y)<0:x<b.x; }
Point operator - (const Point &b)const{return Point(x-b.x,y-b.y);}
double operator ^ (const Point &b)const{return x*b.y - y*b.x;}
double distance(Point b){return sqrt((x-b.x)*(x-b.x)+(y-b.y)*(y-b.y));}
};
struct polygon{
int n;
Point p[maxp];
void input(int _n){
n = _n;
for(int i = 0;i < n;i++) p[i].input();
}
void add(Point q){p[n++] = q;}
struct cmp{
Point p;
cmp(const Point &p0){p = p0;}
bool operator()(const Point &aa,const Point &bb){
Point a = aa, b = bb;
int d = sgn((a-p)^(b-p));
if(d == 0){
return sgn(a.distance(p)-b.distance(p)) < 0;
}
return d > 0;
}
};
void norm(){
Point mi = p[0];
for(int i = 1;i < n;i++)mi = min(mi,p[i]);
sort(p,p+n,cmp(mi));
}
void Graham(polygon &convex){
norm();
int &top = convex.n;
top = 0;
if(n == 1){
top = 1;
convex.p[0] = p[0];
return;
}
if(n == 2){
top = 2;
convex.p[0] = p[0];
convex.p[1] = p[1];
if(convex.p[0] == convex.p[1])top--;
return;
}
convex.p[0] = p[0];
convex.p[1] = p[1];
top = 2;
for(int i = 2;i < n;i++){
while( top > 1 && sgn((convex.p[top-1]-convex.p[top-2])^(p[i]-convex.p[top-2])) <= 0 )
top--;
convex.p[top++] = p[i];
}
if(convex.n == 2 && (convex.p[0] == convex.p[1]))convex.n--;//特判
}
double RotateCalipers(bool flag){
double ans= flag ? 100000000 : -100000000;
int q = 1;
p[n] = p[0];
for(int i = 0; i < n; ++i){
while( (( p[q] - p[i+1] ) ^ ( p[i] - p[i+1] )) < (( p[q+1] - p[i+1] ) ^ (p[i] - p[i+1] )) ) q = (q + 1) % n;
if(!flag) ans = max(ans, max(p[q].distance(p[i]), p[q+1].distance(p[i+1]) ));
else ans = min(ans, min(p[q].distance(p[i]), p[q+1].distance(p[i+1]) ));
}
return ans * ans;
}
};
int main(){
int n;
scanf("%d", &n);
polygon pol; pol.input(n);
polygon newpol; pol.Graham(newpol);
double ans = newpol.RotateCalipers(0);
printf("%.0f\n", ans);
return 0;
}
Gym101635K
/*---------------------------------
*File name: A.cpp
*Creation date: 2020-12-10 16:57
*Link:
*-------------------------------*/
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<map>
#include<set>
#include<deque>
#include<queue>
#include<stack>
#include<bitset>
#include<string>
#include<fstream>
#include<iostream>
#include<algorithm>
#define fi first
#define se second
#define pb push_back
#define LL long long
#define PII pair<int, int>
using namespace std;
typedef int readType;
const double eps = 1e-8;
const double inf = 1e20;
const double pi = acos(-1.0);
const int maxp = 2e5;
int sgn(double x){
if(fabs(x) < eps)return 0;
if(x < 0)return -1;
else return 1;
}
inline double sqr(double x){return x*x;}
inline readType read(){
readType x = 0, y = 1; char c = getchar();
while(c < '0' || c > '9'){if(c == '-') y = -1; c = getchar();}
while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return x * y;
}
struct Point{
double x,y;
Point(){}
Point(double _x,double _y){
x = _x;
y = _y;
}
void input(){scanf("%lf%lf",&x,&y);}
void output(){printf("%lf %lf\n", x, y);}
bool operator == (Point b)const{ return sgn(x-b.x) == 0 && sgn(y-b.y) == 0; }
bool operator < (Point b)const{ return sgn(x-b.x)== 0?sgn(y-b.y)<0:x<b.x; }
Point operator - (const Point &b)const{return Point(x-b.x,y-b.y);}
double operator ^ (const Point &b)const{return x*b.y - y*b.x;}
double operator * (const Point &b)const{return x*b.x + y*b.y;}
double distance(Point b){return sqrt((x-b.x)*(x-b.x)+(y-b.y)*(y-b.y));}
};
struct Line{
Point s,e;
Line(){}
Line(Point _s,Point _e){s = _s, e = _e;}
//点到线段的距离
double length(){return s.distance(e);}
double dispointtoline(Point p){return fabs((p-s)^(e-s))/length();}
double dispointtoseg(Point p){
if(sgn((p-s)*(e-s))<0 || sgn((p-e)*(s-e))<0) return min(p.distance(s),p.distance(e));
return dispointtoline(p);
}
};
struct polygon{
int n;
Point p[maxp];
void input(int _n){
n = _n;
for(int i = 0;i < n;i++) p[i].input();
}
void add(Point q){p[n++] = q;}
struct cmp{
Point p;
cmp(const Point &p0){p = p0;}
bool operator()(const Point &aa,const Point &bb){
Point a = aa, b = bb;
int d = sgn((a-p)^(b-p));
if(d == 0){
return sgn(a.distance(p)-b.distance(p)) < 0;
}
return d > 0;
}
};
void norm(){
Point mi = p[0];
for(int i = 1;i < n;i++)mi = min(mi,p[i]);
sort(p,p+n,cmp(mi));
}
void Graham(polygon &convex){
norm();
int &top = convex.n;
top = 0;
if(n == 1){
top = 1;
convex.p[0] = p[0];
return;
}
if(n == 2){
top = 2;
convex.p[0] = p[0];
convex.p[1] = p[1];
if(convex.p[0] == convex.p[1])top--;
return;
}
convex.p[0] = p[0];
convex.p[1] = p[1];
top = 2;
for(int i = 2;i < n;i++){
while( top > 1 && sgn((convex.p[top-1]-convex.p[top-2])^(p[i]-convex.p[top-2])) <= 0 )
top--;
convex.p[top++] = p[i];
}
if(convex.n == 2 && (convex.p[0] == convex.p[1]))convex.n--;//特判
}
double RotateCalipers(){
double ans = 1e18;
int q = 1;
p[n] = p[0];
for(int i = 0; i < n; ++i){
while( (( p[q] - p[i+1] ) ^ ( p[i] - p[i+1] )) < (( p[q+1] - p[i+1] ) ^ (p[i] - p[i+1] )) ) q = (q + 1) % n;
Line l = Line(p[i], p[i + 1]);
ans = min(ans, l.dispointtoseg(p[q]));
}
return ans;
}
};
int main(){
int n, r;
scanf("%d %d", &n, &r);
polygon pol; pol.input(n);
polygon newpol; pol.Graham(newpol);
double ans = newpol.RotateCalipers();
printf("%f\n", ans);
return 0;
}