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\n",ans>>1);
         else printf("%lld.0\n",ans>>1);
    }return 0;
}
时间: 2024-12-31 07:06:06

BZOJ 1132 [POI2008]Tro(极角排序)的相关文章

bzoj 1132 POI2008 Tro

大水题=_=,可我想复杂了…… 很裸的暴力,就是加了个小优化…… 叉积求面积 :abs(xi*yj - yi*xj) 所以去掉绝对值,把 xi 和 xj 提出来就可以求和了 去绝对值加个极角排序,每次把最左边的点当成原点,然后剩下的排序,接着枚举第二个点,求叉积之和…… 坐标都是整数,用long long,最后再除2 上代码: #include <cstdio> #include <cstring> #include <cstdlib> #include <ios

BZOJ 1132 POI2008 Tro 计算几何

题目大意:给定平面上的一些点,求这些点能组成的所有三角形的面积之和 首先我们枚举每一个点 以这个点为原点建立平面直角坐标系 然后将第一.四象限和x.y轴正半轴上的点按照斜率排序 枚举第二个和第三个点 这样做是O(n^3)的 肯定超时 但是我们发现了什么? 对于每个点k 它对答案的贡献为: (x1*yk-y1*xk)+(x2*yk-y2*xk)+...+(x_(k-1)*yk-y_(k-1)*xk) =(x1+x2+...+x_(k-1))*yk-(y1+y2+...+y_(k-1))*xk 于是

BZOJ1132: [POI2008]Tro

1132: [POI2008]Tro Time Limit: 20 Sec  Memory Limit: 162 MBSubmit: 815  Solved: 211[Submit][Status] Description 平面上有N个点. 求出所有以这N个点为顶点的三角形的面积和 N<=3000 Input 第一行给出数字N,N在[3,3000] 下面N行给出N个点的坐标,其值在[0,10000] Output 保留一位小数,误差不超过0.1 Sample Input 5 0 0 1 2 0

[POI 2008][BZOJ 1132]Tro

这题我真是无能为力了 这题的做法还是挺简单的 枚举左下角的点做为原点,把其余点按极角排序    PS.是作为原点,如枚举到 k 时,对于所有 p[i] (包括p[k]) p[i]-=p[k] (此处为向量减法) 排序后满足 i<j 的两个向量 p[i] 和 p[j] 的叉积都是正数了 ΣΣp[i]×p[j] = ΣΣ(p[i].x*p[j].y-p[i].y*p[j].x) = Σ(p[i].x*Σp[j].y)-Σ(p[i].y*Σp[j].x) 计算叉积和的复杂度就从 O(n2) 降为了 O

bzoj 1914: [Usaco2010 OPen]Triangle Counting 数三角形——极角排序

Description 在一只大灰狼偷偷潜入Farmer Don的牛群被群牛发现后,贝西现在不得不履行着她站岗的职责.从她的守卫塔向下瞭望简直就是一件烦透了的事情.她决定做一些开发智力的小练习,防止她睡着了.想象牧场是一个X,Y平面的网格.她将N只奶牛标记为1-N (1 <= N <= 100,000),每只奶牛的坐标为X_i,Y_i (-100,000 <= X_i <= 100,000;-100,000 <= Y_i <= 100,000; 1 <= i &l

【BZOJ1132】[POI2008]Tro 几何

[BZOJ1132][POI2008]Tro Description 平面上有N个点. 求出所有以这N个点为顶点的三角形的面积和 N<=3000 Input 第一行给出数字N,N在[3,3000] 下面N行给出N个点的坐标,其值在[0,10000] Output 保留一位小数,误差不超过0.1 Sample Input 5 0 0 1 2 0 2 1 0 1 1 Sample Output 7.0 题解:我们将所有点按x排序,枚举最左面的点.设当前点为i,我们想计算右面所有点对与其形成三角形的面

Hdu5032 极角排序+树状数组

题目链接 思路:参考了题解.对询问进行极角排序,然后用树状数组维护一下前缀和即可. /* ID: onlyazh1 LANG: C++ TASK: test */ #include<bits/stdc++.h> using namespace std; #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 typedef long long ll; const int maxn=1010; const int maxm=10

Codeforces Round #124 (Div. 1) C. Paint Tree(极角排序)

C. Paint Tree time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output You are given a tree with n vertexes and n points on a plane, no three points lie on one straight line. Your task is to paint

HDU Always Cook Mushroom (极角排序+树状数组)

Problem Description Matt has a company, Always Cook Mushroom (ACM), which produces high-quality mushrooms. ACM has a large field to grow their mushrooms. The field can be considered as a 1000 * 1000 grid where mushrooms are grown in grid points numbe