zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    html5+css3中的background: -moz-linear-gradient 用法 (转载)
    CentOS 安装Apache服务
    Linux 笔记
    CURL 笔记
    Spring Application Context文件没有提示功能解决方法
    LeetCode 389. Find the Difference
    LeetCode 104. Maximum Depth of Binary Tree
    LeetCode 520. Detect Capital
    LeetCode 448. Find All Numbers Disappeared in an Array
    LeetCode 136. Single Number
  • 原文地址:https://www.cnblogs.com/forever97/p/bzoj1132.html
Copyright © 2011-2022 走看看