BZOJ 1132 [POI2008]Tro(极角排序)
2017-04-16 21:57
344 查看
【题目链接】 http://www.lydsy.com/JudgeOnline/problem.php?id=1132
【题目大意】
平面上有N个点. 求出所有以这N个点为顶点的三角形的面积和(N<=3000)
【题解】
我们发现直接枚举三个点计算会造成很大部分的叉积重复被计算,
因此我们枚举i,计算pj和pi点差的后缀和,我们发现对于固定边ij,
其与后面的枚举量相关贡献就为pj-pi和点差后缀和的叉积。
因此我们针对每个i进行后面数据的极角排序,O(n)计算与i相关的所有答案贡献。
【代码】
【题目大意】
平面上有N个点. 求出所有以这N个点为顶点的三角形的面积和(N<=3000)
【题解】
我们发现直接枚举三个点计算会造成很大部分的叉积重复被计算,
因此我们枚举i,计算pj和pi点差的后缀和,我们发现对于固定边ij,
其与后面的枚举量相关贡献就为pj-pi和点差后缀和的叉积。
因此我们针对每个i进行后面数据的极角排序,O(n)计算与i相关的所有答案贡献。
【代码】
#include <algorithm> #include <cstdio> #include <cmath> using namespace std; typedef long long LL; struct Point{ int x,y; int index; Point(){} Point(int x1,int y1){x=x1;y=y1;} Point operator +(const Point &b)const{return Point(x+b.x,y+b.y);} Point operator -(const Point &b)const{return Point(x-b.x,y-b.y);} int operator *(const Point &b)const{return x*b.x+y*b.y;} //点积 LL operator ^(const Point &b)const{return (LL)x*b.y-(LL)y*b.x;} //叉积 }; double dist(Point a,Point b){return sqrt((a-b)*(a-b));} int pos; Point p[3010]; bool cmp(Point a,Point b){ LL tmp=(a-p[pos])^(b-p[pos]); if(tmp==0)return dist(p[pos],a)<dist(p[pos],b); else if(tmp<0)return false; else return true; } int n; int main(){ while(~scanf("%d",&n)){ for(int i=1;i<=n;i++){ scanf("%d%d",&p[i].x,&p[i].y); }LL ans=pos=0; sort(p+1,p+n+1,cmp); for(int i=1;i<=n-2;i++){ p[0].x=p[0].y=0;pos++; sort(p+i+1,p+n+1,cmp); for(int j=i+1;j<=n;j++)p[0]=p[0]+(p[j]-p[i]); for(int j=i+1;j<=n;j++){ p[0]=p[0]-(p[j]-p[i]); ans+=(p[j]-p[i])^p[0]; } }if(ans&1)printf("%lld.5\n",ans>>1); else printf("%lld.0\n",ans>>1); }return 0; }
相关文章推荐
- bzoj1132: [POI2008]Tro (第二次做)
- bzoj1132: [POI2008]Tro
- 【bzoj1132】[POI2008] Tro
- BZOJ 1132 [POI2008]Tro 计算几何
- [BZOJ1132][POI2008]Tro(计算几何)
- BZOJ 1132: [POI2008]Tro 计算几何,统计对答案贡献
- 【BZOJ1132】[POI2008]Tro 几何
- BZOJ1132: [POI2008]Tro
- bzoj 1132: [POI2008]Tro 计算几何
- bzoj 1132: [POI2008]Tro (计算几何)
- BZOJ 1132 POI2008 Tro 计算几何
- bzoj1132 [POI2008]Tro
- 【bzoj1132】 [POI2008]Tro
- bzoj1132 [POI2008]Tro(叉积)
- 【POI2008】【BZOJ1132】Tro
- 【BZOJ】1132: [POI2008]Tro
- [几何] BZOJ 1132 [POI2008]Tro
- bzoj1132[POI2008]Tro 计算几何
- bzoj1132 [POI2008]Tro 计算几何
- BZOJ 1132: [POI2008]Tro