[Swust OJ 249]--凸包面积
现在,给出了这些墨点的坐标,请帮助麦兜计算出覆盖这些墨点的最小凸多边形的面积。
每组测试数据的第一行是一个正整数N(0< N < = 105),表明了墨点的数量。接下来的N行每行包含了两个整数Xi和Yi(0<=Xi,Yi<=2000),表示每个墨点的坐标。每行的坐标间可能包含多个空格。
|
1
2
3
4
5
6
7
8
9
|
2
4
0 0
1 0
0 1
1 1
2
0 0
0 1
|
|
1
2
|
1.0
0.0
|
area += (pos.x*tmp.y - pos.y*tmp.x) / 2.0;(pos当前点,tmp下一个点,最后一个点和第一个点在来一次)#include<iostream>
#include<cstdio>
#include<algorithm>
#include<stack>
#include<cmath>
using namespace std;
struct node{
double x, y;
}point[], pos, tmp;
double dis(node a, node b){
return pow((a.x - b.y), ) + pow((a.y - b.y), );
}
//按点集分布排序,利用向量平行关系判断(极角排序),角度相同则距离小的在前面
bool cmp(node a, node b){
double povit = (a.x - pos.x)*(b.y - pos.y) - (b.x - pos.x)*(a.y - pos.y);
if (povit > || !povit && (dis(a, pos) < dis(b, pos)))
return true;
return false;
}
//当前点是否在点集左侧,利用叉乘比较3个点两条线的斜率关系
bool turn_left(node p1, node p2, node p3){
return (p2.x*p1.y + p3.x*p2.y + p1.x*p3.y - p3.x*p1.y - p1.x*p2.y - p2.x*p3.y) > ? true : false;
}
int main(){
int i, sign, n, t;
double area;
cin >> t;
while (t--){
cin >> n;
for (i = ; i < n; i++)
cin >> point[i].x >> point[i].y;
if (n <= ){
cout << "0.0\n";
continue;
}
stack<node> Q;
sign = ;
pos = point[];
for (i = ; i < n; i++){
if (pos.y == point[i].y&&pos.x>point[i].x || point[i].y < pos.y){
pos = point[i];
sign = i;
}
}
swap(point[], point[sign]);
sort(point + , point + n, cmp);
Q.push(point[]), Q.push(point[]), Q.push(point[]);
for (i = ; i < n; i++){
while (!Q.empty()){
tmp = Q.top();
Q.pop();
if (turn_left(tmp, Q.top(), point[i])){
Q.push(tmp);
break;
}
}
Q.push(point[i]);
}
area = ;
tmp = Q.top(), Q.pop();
area += (pos.x*tmp.y - pos.y*tmp.x) / 2.0;
while (!Q.empty()){
area += (tmp.x*Q.top().y - tmp.y*Q.top().x) / 2.0;
tmp = Q.top();
Q.pop();
}
printf("%.1lf\n", fabs(area));
}
return ;
}
主要的地方就是利用向量平行关系,按点集的离散化排序
(没说清楚~~~看凸包的简单概念吧http://www.cnblogs.com/zyxStar/p/4540984.html),
判断下一个的点能否覆盖已选取的点~~
[Swust OJ 249]--凸包面积的更多相关文章
- [Swust OJ 567]--老虎在不在笼子里(凸包问题)
题目链接:http://acm.swust.edu.cn/problem/567/ Time limit(ms): 1000 Memory limit(kb): 65535 一只老虎自从看了< ...
- POJ 3348 - Cows 凸包面积
求凸包面积.求结果后不用加绝对值,这是BBS()排序决定的. //Ps 熟练了template <class T>之后用起来真心方便= = //POJ 3348 //凸包面积 //1A 2 ...
- [Swust OJ 404]--最小代价树(动态规划)
题目链接:http://acm.swust.edu.cn/problem/code/745255/ Time limit(ms): 1000 Memory limit(kb): 65535 Des ...
- [Swust OJ 649]--NBA Finals(dp,后台略(hen)坑)
题目链接:http://acm.swust.edu.cn/problem/649/ Time limit(ms): 1000 Memory limit(kb): 65535 Consider two ...
- poj 3348 Cow 凸包面积
Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 8122 Accepted: 3674 Description ...
- poj 3348:Cows(计算几何,求凸包面积)
Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 6199 Accepted: 2822 Description ...
- uva109求凸包面积,判断点是不是在凸包内
自己想了一个方法判断点是不是在凸包内,先求出凸包面积,在求由点与凸包上每两个点之间的面积(点已经排好序了),如果两者相等,则点在凸包内,否则不在(时间复杂度可能有点高)但是这题能过 #include& ...
- poj3348凸包面积
用叉积求凸包面积 如图所示,每次找p[0]来计算,(叉积是以两个向量构成的平行四边形的面积,所以要/2) #include<map> #include<set> #includ ...
- poj 3348(凸包面积)
Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 8063 Accepted: 3651 Description ...
随机推荐
- .NET中的IO操作基础介绍
关于IO简介 .NET中的IO操作,经常需要调用一下几个类. clipboard[] .FileStream类 文件流类,负责大文件的拷贝,读写. .Path类 Path类中方法,基本都是对字符串(文 ...
- python10min系列之多线程下载器
今天群里看到有人问关于python多线程写文件的问题,联想到这是reboot的架构师班的入学题,我想了一下,感觉坑和考察的点还挺多,可以当成一个面试题来问,简单说一下我的想法和思路吧,涉及的代码和注释 ...
- 静态方法中为什么不能使用this
- Spring 面试复习
1 singleton 和 prototype singleton作用域:当把一个Bean定义设置为singleton作用域是,Spring IoC容器中只会存在一个共享的Bean实例,并且所有对 ...
- C#--遍历目录实例
鉴于前面几篇博客都说了,这边就啥都不说了.直接就開始贴代码了. 1.控件解释: FolderBrowserDialog控件一个----用来显示"浏览目录"对话框 TextBox控件 ...
- [cocos2d-x]用CCSpriteBatchNode进行文理贴图的优化
引言: 我们在进行手机游戏开发的过程中,由于手机的内存资源是有限的,那么对纹理贴图的优化是非常有必要的,有可能相同的功能,优化的好与不好对内存资源的消耗是非常明显的,下面我就用一个例子来说明一下. 说 ...
- 淘特房产CMS系统 7.5
资源描写叙述: 淘特房产CMS系统採用淘特AspCms开发,全部前台信息生成静态HTM,提供了楼盘.二手房.房产中介.房产经济人.业主社区等管理模块,集成了淘特CMS与动网论坛,Discuz,Oblo ...
- eclipse主题插件
打开eclipse ,选择 Help 选择Install New Software 点击 Add 输入http://eclipse-color-theme.github.com/update,选中Ec ...
- objective-C学习笔记(二)类 class 和 结构 struct
Objective-C的类型 引用类型 类 class 指针 pointer 块 block 值类型 基础数值类型 结构 struct 枚举 enum 类型装饰 协议 protocol 类别 cate ...
- UILabel显示html文本
NSString * htmlString = @"<html><body> Some html string \n <font size=\"13\ ...