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;
}
BZOJ 1132 [POI2008]Tro(极角排序)的更多相关文章
- BZOJ.1132.[POI2008]Tro(极角排序)
BZOJ 洛谷 考虑暴力,每次枚举三个点,答案就是\(\frac12\sum_{k<j<i}(i-k)\times(j-k)\). 注意到叉积有分配率,所以固定\(k\),枚举\(i,j\ ...
- bzoj 1132 POI2008 Tro
大水题=_=,可我想复杂了…… 很裸的暴力,就是加了个小优化…… 叉积求面积 :abs(xi*yj - yi*xj) 所以去掉绝对值,把 xi 和 xj 提出来就可以求和了 去绝对值加个极角排序,每次 ...
- bzoj 1132 [POI2008]Tro 几何
[POI2008]Tro Time Limit: 20 Sec Memory Limit: 162 MBSubmit: 1796 Solved: 604[Submit][Status][Discu ...
- bzoj 1132: [POI2008]Tro 计算几何
题目大意: 平面上有N个点. 求出所有以这N个点为顶点的三角形的面积和 N<=3000 题解 我们看到了n的范围,于是我们就知道这一定不是一个线性算法 所以我们尝试枚举三角形的一个点,那么我们现 ...
- 【刷题】BZOJ 1132 [POI2008]Tro
Description 平面上有N个点. 求出所有以这N个点为顶点的三角形的面积和 N<=3000 Input 第一行给出数字N,N在[3,3000] 下面N行给出N个点的坐标,其值在[0,10 ...
- 【BZOJ】1132: [POI2008]Tro
题意 给\(n(1 \le n \le 3000)\)个点,求所有三角形的面积和. 分析 首先枚举一个点,发现把其它点按照关于这个点的极角排序后第\(i\)个点关于前面\(1\)到\(i-1\)的点组 ...
- BZOJ1132: [POI2008]Tro(叉积 排序)
题意 世上最良心题目描述qwq 平面上有N个点. 求出所有以这N个点为顶点的三角形的面积和 N<=3000 Sol 直接模拟是$n^3$的. 考虑先枚举一个$i$,那么我们要算的就是$\sum_ ...
- BZOJ1132: [POI2008]Tro
1132: [POI2008]Tro Time Limit: 20 Sec Memory Limit: 162 MBSubmit: 815 Solved: 211[Submit][Status] ...
- bzoj1132[POI2008]Tro 计算几何
1132: [POI2008]Tro Time Limit: 20 Sec Memory Limit: 162 MBSubmit: 1722 Solved: 575[Submit][Status] ...
随机推荐
- POJ 3061 Subsequence ( 尺取法)
题目链接 Description A sequence of N positive integers (10 < N < 100 000), each of them less than ...
- 贿赂囚犯 Bribe the prisoners ( 动态规划+剪枝)
一个监狱里有P个并排着的牢房,从左往右一次编号为1,2,-,P.最初所有牢房里面都住着一个囚犯.现在要释放一些囚犯.如果释放某个牢房里的囚犯,必须要贿赂两边所有的囚犯一个金币,直到监狱的两端或者空牢房 ...
- POJ 30253 Fence Repair (二叉树+优先队列)
题目链接 Description Farmer John wants to repair a small length of the fence around the pasture. He meas ...
- bzoj 2733 平衡树启发式合并
首先对于一个连通块中,询问我们可以直接用平衡树来求出排名,那么我们可以用并查集来维护各个块中的连通情况,对于合并两个平衡树,我们可以暴力的将size小的平衡树中的所有节点删掉,然后加入大的平衡树中,因 ...
- python中的Queue模块
queue介绍 queue是python的标准库,俗称队列.可以直接import引用,在python2.x中,模块名为Queue.python3直接queue即可 在python中,多个线程之间的数据 ...
- face_recognition 人脸识别报错
[root@localhost examples]# python facerec_from_video_file.py RuntimeError: module compiled against A ...
- OpenRCT2
https://github.com/OpenRCT2/OpenRCT2 https://github.com/LRFLEW/HRM-CCPU https://github.com/LRFLEW/Op ...
- 在Mysql中插入百万级别数据的方法
首先,建立部门表和员工表: 部门表: create table dept( id int unsigned primary key auto_increment, deptno mediumint u ...
- Leetcode 之Wildcard Matching(32)
跟上题类似,主要考虑‘*’的匹配问题.如果遇到‘*’,则跳过继续匹配,如果不匹配,则s++,重新扫描. bool isMatch2(const char *s, const char *p) { if ...
- 算法入门系列1:k-means
k-means是一种无监督学习算法,用于聚类. 下图(来自http://www.cnblogs.com/jerrylead/archive/2011/04/06/2006910.html)展示了k-m ...