存模板:(极角排序)
#include <cmath>
#include <math.h>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define maxn 100030
using namespace std;
inline void read(int &x) {
char ch;
bool flag = false;
for (ch = getchar(); !isdigit(ch); ch = getchar())if (ch == '-') flag = true;
for (x = 0; isdigit(ch); x = x * 10 + ch - '0', ch = getchar());
x = flag ? -x : x;
}
inline void write(int x) {
static const int maxlen = 100;
static char s[maxlen];
if (x < 0) { putchar('-'); x = -x;}
if (!x) { putchar('0'); return; }
int len = 0; for (; x; x /= 10) s[len++] = x % 10 + '0';
for (int i = len - 1; i >= 0; --i) putchar(s[i]);
}
int n,m;
struct point {
int x,y;
}a[maxn],S;
point operator - (point A,point B){
point C;
C.x=A.x-B.x;
C.y=A.y-B.y;
return C;
}
point operator + (point A,point B){
point C;
C.x=A.x+B.x;
C.y=A.y+B.y;
return C;
}
long long cross(long long x1,long long y1,long long x2,long long y2){
return 1ll*x1*y2-1ll*x2*y1;
}
long long cross(point A,point B){
return 1ll*A.x*B.y-1ll*A.y*B.x;
}
long long dot(long long x1,long long y1,long long x2,long long y2){
return 1ll*x1*y2+1ll*x2*y2;
}
bool cmp(point A,point B){
long long tmp=cross( A-S , B-S );
if (tmp>0)
return 1;
if (tmp<0)
return 0;
if (tmp==0)
if (A.x!=B.x)
return A.x<B.x;
else
return A.y<B.y;
}
int stack[maxn],top;
void doit(){
for (int i=1;i<=n;i++)
{
while ( ( top>=2) && ( cross( a[i]-a[ stack[ top ] ],a[ stack[ top-1 ] ]-a[ stack[ top ] ] )<=0ll ) )
top--;
stack[++top]=i;
}
}
double dist(point A,point B){
return sqrt( (double) ( 1.0*(A.x-B.x)*(A.x-B.x)+1.0*(A.y-B.y)*(A.y-B.y) ) );
}
int main(){
read(n); read(m);
S.x=10737418ll;
for (int i=1;i<=n;i++)
{
read(a[i].x); read(a[i].y);
if ( (a[i].x<S.x)|| ( (a[i].x==S.x)&& (a[i].y<S.y) ) )
S=a[i];
}
sort(a+1,a+n+1,cmp);
doit();
double sum=0;
for(int i=1;i<top;i++)
sum=sum+dist( a[ stack[i] ],a[ stack[ i+1] ] );
double PI=acos(-1);
sum+=1.0*dist( a[stack[ top ] ],a[ stack[ 1] ] )+2*PI*m;
printf("%.0f\n",round(sum ) );
return 0;
}
凸包可以一遍求出来,也可以上下凸壳分别求出来。
二者的区别之一就是分开求上下凸壳可以完全保留再两个相邻凸包点的线段上的点,但是一遍求出来的凸包好像不能
上下凸壳分开求的代码:
#include <cmath> #include <math.h> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <algorithm> #include <vector> #define LL long long #define pi 3.1415926535898 using namespace std; int n,m; template<typename T> struct Point{ T x,y; bool bo; }; Point<int> operator - (const Point<int> &a,const Point<int> &b){ Point<int> c; c.x=a.x-b.x; c.y=a.y-b.y; return c; } //加不加const 的区别? 常量引用 Point<int> operator + (const Point<int> &a,const Point<int> &b){ Point<int> c; c.x=a.x+b.x; c.y=a.y+b.y; return c; } bool cmp(Point<int> a,Point<int> b){ if (a.x<b.x) return 1; if (a.x>b.x) return 0; return a.y<b.y; } double dist(Point<int> A,Point<int> B){ return sqrt(1.0*(A.x-B.x)*(A.x-B.x)+1.0*(A.y-B.y)*(A.y-B.y)); } int cross(Point<int> a ,Point<int> b){ return a.x*b.y-b.x*a.y; } LL sqr(int a){return 1ll*a*a;} double ans=0; Point<int> beg[1500]; int tot; typedef Point<int> kk; vector< kk > q1; vector< kk > q2; Point<int> a[1500]; int doit(Point<int> *a,Point<int> *beg){ for (int i=1;(i>=1)&&(i<=n);i++){ while( ( q1.size() >= 2 ) && ( cross( q1[q1.size()-2]-q1[q1.size()-1] , a[i]-q1[q1.size()-1] ) > 0 ) ) q1.pop_back(); while( ( q2.size() >= 2 ) && ( cross( q2[q2.size()-2]-q2[q2.size()-1] , a[i]-q2[q2.size()-1] ) < 0 ) ) q2.pop_back(); q1.push_back(a[i]); q2.push_back(a[i]); } int tot=0; for (int i=0;i<q1.size()-1;i++) beg[++tot]=q1[i]; for (int i=q2.size()-1;i>=1;i--) beg[++tot]=q2[i]; return tot; } int main(){ scanf("%d%d",&n,&m); for (int i=1;i<=n;i++) scanf("%d%d",&a[i].x,&a[i].y); sort(a+1,a+n+1,cmp); tot=doit( a , beg ); beg[0]=beg[tot]; for (int i=0;i<tot;i++) ans+=dist(beg[i],beg[i+1]); ans+=2.0*m*pi; printf("%.0f\n",round(ans)); return 0; }
ps.C++中lf似乎只用于输入,用于输出会挂!!!!!!