POJ 3348 Cows | 凸包模板题
题目:
给几个点,用绳子圈出最大的面积养牛,输出最大面积/50
题解:
Graham凸包算法的模板题
下面给出做法
1.选出x坐标最小(相同情况y最小)的点作为极点(显然他一定在凸包上)
2.其他点进行极角排序<极角指从坐标轴的某一方向逆时针旋转到向量的角度>,
极角一样按距离从近到远(可以用叉积实现)
3.用栈维护凸包上的点,将极点和极角序最小的点依次入栈
4.按顺序扫描,检查栈顶的前两个元素与这个点构成的线段是否拐向右(顺时针侧,叉积小于0)
如果满足就弹出栈顶元素,直到不满足或者栈里不足两个元素
反之入栈
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<stack>
#define N 10005
using namespace std;
int n,m;
struct point
{
int x,y;
point (){};
point (int _x,int _y)
{
x=_x,y=_y;
}
point operator - (const point &a)const
{
return point (x-a.x,y-a.y);
}
int operator * (const point &a) const
{
return x*a.y-y*a.x;
}
int norm()const
{
return x*x+y*y;
}
}p[N],q[N];
bool cmp(int u,int v)
{
int det=(p[u]-p[1])*(p[v]-p[1]);
if (det!=0) return det>0;
return (p[u]-p[1]).norm() < (p[v]-p[1]).norm();
}
void Graham()
{
int id=1;
for (int i=2;i<=n;i++)
if (p[i].x<p[id].x || (p[i].x==p[id].x && p[i].y<p[id].y))
id=i;
if (id!=1) swap(p[1],p[id]);
int per[N];
for (int i=1;i<=n;i++)
per[i]=i;
sort(per+2,per+1+n,cmp);
q[++m]=p[1];
for (int i=2;i<=n;i++)
{
int j=per[i];
while (m>=2 && (p[j]-q[m-1])*(q[m]-q[m-1])>=0) m--;
q[++m]=p[j];
}
}
int Area()
{
int res=0;
q[m+1]=q[1];
for (int i=1;i<=m;i++)
res+=q[i]*q[i+1];
return res/2;
}
int main()
{
scanf("%d",&n);
for (int i=1;i<=n;i++)
scanf("%d%d",&p[i].x,&p[i].y);
Graham();
int ans=Area()/50;
printf("%d\n",ans);
return 0;
}
POJ 3348 Cows | 凸包模板题的更多相关文章
- POJ 3348 Cows (凸包模板+凸包面积)
Description Your friend to the south is interested in building fences and turning plowshares into sw ...
- POJ 3348 - Cows 凸包面积
求凸包面积.求结果后不用加绝对值,这是BBS()排序决定的. //Ps 熟练了template <class T>之后用起来真心方便= = //POJ 3348 //凸包面积 //1A 2 ...
- POJ 3348 Cows [凸包 面积]
Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 9022 Accepted: 3992 Description ...
- POJ 3348 Cows 凸包 求面积
LINK 题意:给出点集,求凸包的面积 思路:主要是求面积的考察,固定一个点顺序枚举两个点叉积求三角形面积和除2即可 /** @Date : 2017-07-19 16:07:11 * @FileNa ...
- poj 3348 Cows 凸包 求多边形面积 计算几何 难度:0 Source:CCC207
Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7038 Accepted: 3242 Description ...
- POJ 3348 Cows | 凸包——童年的回忆(误)
想当年--还是邱神给我讲的凸包来着-- #include <cstdio> #include <cstring> #include <cmath> #include ...
- poj 3348 Cow 凸包面积
Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 8122 Accepted: 3674 Description ...
- Sliding Window POJ - 2823 单调队列模板题
Sliding Window POJ - 2823 单调队列模板题 题意 给出一个数列 并且给出一个数m 问每个连续的m中的最小\最大值是多少,并输出 思路 使用单调队列来写,拿最小值来举例 要求区间 ...
- POJ 2186:Popular Cows Tarjan模板题
Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 25945 Accepted: 10612 De ...
随机推荐
- hibernate系列之四
数据库中表之间的关系: 一对一.一对多.多对多 一对多的建表原则:在多的一方创建外键指向一的一方的主键: 多对多的建表原则:创建一个中间表,中间表中至少有两个字段作为外键分别指向多对多双方的主键: 一 ...
- Flask初学者:视图函数/方法返回值(HTML模板/Response对象)
返回HTML模板:使用“from flask import render_template”,在函数中传入相对于文件夹“templates”HTML模板路径名称字符串即可(默认模板路径),flask会 ...
- maston总结
Substitution Tags(替换标签) % ; # this is embedded Perl You have <% $cd_count %> CDs. Escaping sub ...
- vue webpack build 打包过滤console.log()日志
vue cli创建项目在 webpack.prod.conf.js文件 //打包时清除页面中所有打印及debugger断点 new webpack.optimize.UglifyJsPlugin({ ...
- mysql学习第三天练习(流程控制函数)
-- 流程控制函数 -- 1.查询员工部门号,并赋予部门名 select empno,ename,deptno,case deptno then '10部门' then '20部门' else '30 ...
- [学习笔记]CSS选择器
CSS语法结构 selector { property1 : value; property2 : value; } 如果包含多个属性,那么属性每个属性的结尾需要有一个分号.如果属性的 ...
- git 设置别名 git alias
git config --global alias.st status git config --global alias.ck checkout git config --global alias. ...
- svn git 导入本地文件到远程服务器 import
以前,想要把本地的一个文件上传到svn 或者git 服务器的时候,都要先把服务器上的文件夹down 下来,然后把要添加的文件添加进去,然后提交. 想想都麻烦. 现在我们用import 命令就可以做到, ...
- 5.bootstrap栅格 清除浮动
只要用到栅格,就注意要清除浮动,清除方法就是在父元素的class上加一个clearfix 1.情景: . <div class="col-sm-7"> <div ...
- Field 'flag' doesn't have a default value错误
错误代码: java.sql.SQLException: Field 'flag' doesn't have a default value at com.mysql.jdbc.SQLError.cr ...