• BZOJ 1132 [POI2008]Tro(极角排序)


    【题目链接】 http://www.lydsy.com/JudgeOnline/problem.php?id=1132

    【题目大意】

      平面上有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
    ",ans>>1);
             else printf("%lld.0
    ",ans>>1);
        }return 0;
    }
  • 相关阅读:
    ZOJ 2604 Little Brackets DP
    js实现回放拖拽轨迹-------Day48
    Android蓝牙开发
    linux中的两个很重要的信号:SIGALRM信号和SIGCHID信号
    MySQL mysqldump数据导出详解
    JFinal redis cluster集群插件
    nginx平滑升级
    温故而知新-String类
    Linux环境变量具体解释
    android消息机制
  • 原文地址:https://www.cnblogs.com/forever97/p/bzoj1132.html
Copyright © 2020-2023  润新知