主要是存个代码,还有我踩的坑。。
cin和cout真的很慢,很慢,非常慢..
还有就是先把凸包求出来了,然后才能考虑凸包面积啥的
刚开始思路错了,直接上多边形面积
明明输出和标程都一样了,在hdu还是wa
欸..就当学了个凸包板子,学会了Graham。。
#include<bits/stdc++.h> #define PI 3.14159265358979323846 using namespace std; template<typename T> inline T read(T&x){ x=0;int f=0;char ch=getchar(); while (ch<'0' || ch>'9') f|=(ch=='-'),ch=getchar(); while (ch>='0' && ch<='9') x=x*10+ch-'0',ch=getchar(); return x=f?-x:x; } const int N=1e6; struct Point { double x,y; double operator ^(const Point &b)const { return x*b.y - y*b.x; } }; Point stackk[N]; //凸包中所有点,下标从0....top开始 Point p[N]; //存入的所有点 Point MinA; int top; double dist(Point A,Point B) { return sqrt((A.x-B.x)*(A.x-B.x)+(A.y-B.y)*(A.y-B.y)); } //计算叉积 判断bc向量到ac向量 是否通过左转得到 >0左转 <0右转 =0共线 double cross(Point A,Point B,Point C) { return (B.x-A.x)*(C.y-A.y)-(B.y-A.y)*(C.x-A.x); } bool cmp(Point a,Point b) //极角排序 { double k=cross(MinA,a,b); if(k>0) return 1; if(k<0) return 0; return dist(MinA,a)<dist(MinA,b); } void Graham(int n) { int i; for(i=1; i<n; i++) if(p[i].y<p[0].y||(p[i].y==p[0].y&&p[i].x<p[0].x)) swap(p[i],p[0]); MinA=p[0]; sort(p+1,p+n,cmp); stackk[0]=p[0]; stackk[1]=p[1]; top=1; for(i=2; i<n; i++) { while(cross(stackk[top-1],stackk[top],p[i])<=0&&top>=1) --top; stackk[++top]=p[i]; } top++; } int n,q; long long CalcArea(Point p[],int n) { long long res = 0; for(int i = 0; i < n; i++) res += (p[i]^p[(i+1)%n]); return abs(res); } void solve(){ read(n);read(q); for(int i = 0; i < n; i++){ long long x,y;read(x);read(y); p[i].x=double(x);p[i].y=double(y); } Graham(n); long long ans = CalcArea(stackk,top); long long tot=0.0; for(int i=0;i<top;i++) tot+=max(fabs(stackk[i].x-stackk[(i+1)%top].x),fabs(stackk[i].y-stackk[(i+1)%top].y)); // printf("%.1lf\n",ans); for(int i=0;i<q;i++){ long long t; read(t); long long pri=ans/2+tot*t+2*t*t; printf("%lld",pri); if(ans%2==1) printf(".5\n"); else printf(".0\n"); } } int main(){ // freopen("lys.in","r",stdin); // freopen("lys.out","w",stdout); int t; read(t); while(t--){ solve(); } return 0; }
标签:杭电多校,ch,int,Eternal,++,long,凸包,read,Vale From: https://www.cnblogs.com/liyishui2003/p/16583751.html