BZOJ 1670: [Usaco2006 Oct]Building the Moat护城河的挖掘
Description
求凸包周长.
Sol
凸包+计算几何.
这好像叫什么 Graham Scan 算法...
这个可以求凸包的周长,直径,面积.
选择一个基点,然后按极角排序,最后用一个栈一直维护方向单调.
极角排序就是先按与基点的向量和 \(x\) 轴的夹角排序,就是点积变一变.
维护方向的时候就是用叉积判断顺逆关系...
Code
/**************************************************************
Problem: 1670
User: BeiYu
Language: C++
Result: Accepted
Time:36 ms
Memory:1352 kb
****************************************************************/ #include<cstdio>
#include<cmath>
#include<utility>
#include<algorithm>
#include<iostream>
using namespace std; #define mpr make_pair
#define sqr(x) ((x)*(x))
#define x first
#define y second
typedef pair< int,int > pr;
const int N = 5005;
int n,b;
pr g[N];
int stk[N],top; inline int in(int x=0,char ch=getchar()){ while(ch>'9' || ch<'0') ch=getchar();
while(ch>='0' && ch<='9') x=(x<<3)+(x<<1)+ch-'0',ch=getchar();return x; }
pr operator - (const pr &a,const pr &b){ return mpr(a.x-b.x,a.y-b.y); }
int operator * (const pr &a,const pr &b){ return a.x*b.y-b.x*a.y; }
double Length(const pr &a){ return sqrt(sqr(1.0*a.x)+sqr(1.0*a.y)); }
int cmp(const pr &a,const pr &b){
pr v1=a-g[1],v2=b-g[1];double l1=Length(v1),l2=Length(v2);
if(v1.x*l2<v2.x*l1 || (v1.x*l2==v2.x*l1 && l1<l2)) return 1;return 0;
}
int main(){
// freopen("in.in","r",stdin);
n=in(),b=1;
for(int i=1,u,v;i<=n;i++){
u=in(),v=in(),g[i]=mpr(u,v);
if(v<g[b].y || (v==g[b].y && u<g[b].x)) b=i;
}swap(g[1],g[b]);
sort(g+2,g+n+1,cmp);
// for(int i=1;i<=n;i++) printf("%d:(%d,%d)\n",i,g[i].x,g[i].y);
// n=unique(g+2,g+n+1)-(g+2);
stk[++top]=1,stk[++top]=2;
for(int i=3;i<=n;i++){
while(top>2 && (g[i]-g[stk[top]])*(g[stk[top]]-g[stk[top-1]])<0) --top;
stk[++top]=i;
}
// cout<<"-----------"<<endl;
// for(int i=1;i<=top;i++) printf("%d:(%d,%d)\n",i,g[stk[i]].x,g[stk[i]].y);
double ans=Length(g[stk[top]]-g[1]);
for(int i=2;i<=top;i++) ans+=Length(g[stk[i]]-g[stk[i-1]]);
printf("%.2lf\n",ans);
return 0;
}
BZOJ 1670: [Usaco2006 Oct]Building the Moat护城河的挖掘的更多相关文章
- bzoj 1670: [Usaco2006 Oct]Building the Moat护城河的挖掘 -- 凸包
1670: [Usaco2006 Oct]Building the Moat护城河的挖掘 Time Limit: 3 Sec Memory Limit: 64 MB Description 为了防止 ...
- bzoj 1670 [Usaco2006 Oct]Building the Moat护城河的挖掘——凸包
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1670 用叉积判断.注意两端的平行于 y 轴的. #include<cstdio> ...
- 牛客假日团队赛5J 护城河 bzoj 1670: [Usaco2006 Oct]Building the Moat护城河的挖掘 (凸包的周长)
链接:https://ac.nowcoder.com/acm/contest/984/J 来源:牛客网 护城河 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言6 ...
- bzoj 1670: [Usaco2006 Oct]Building the Moat护城河的挖掘【凸包】
凸包模板 #include<iostream> #include<cstdio> #include<algorithm> #include<cmath> ...
- 【BZOJ】1670: [Usaco2006 Oct]Building the Moat护城河的挖掘(凸包)
http://www.lydsy.com/JudgeOnline/problem.php?id=1670 裸打了凸包.. #include <cstdio> #include <cs ...
- BZOJ_1670_[Usaco2006 Oct]Building the Moat护城河的挖掘_求凸包
BZOJ_1670_[Usaco2006 Oct]Building the Moat护城河的挖掘_求凸包 Description 为了防止口渴的食蚁兽进入他的农场,Farmer John决定在他的农场 ...
- BZOJ1670 [Usaco2006 Oct]Building the Moat护城河的挖掘
裸的凸包...(和旋转卡壳有什么关系吗...蒟蒻求教T T) 话说忘了怎么写了...(我以前都是先做上凸壳再做下凸壳的说) 于是看了下hzwer的写法,用了向量的点积,方便多了,于是果断学习(Orz) ...
- 【计算几何】【凸包】bzoj1670 [Usaco2006 Oct]Building the Moat护城河的挖掘
#include<cstdio> #include<cmath> #include<algorithm> using namespace std; #define ...
- bzoj1670【Usaco2006 Oct】Building the Moat 护城河的挖掘
1670: [Usaco2006 Oct]Building the Moat护城河的挖掘 Time Limit: 3 Sec Memory Limit: 64 MB Submit: 387 Sol ...
随机推荐
- C#创建用户控件 - IPv4地址输入框
根据网上的改写:http://blog.csdn.net/jhqin/article/details/5823363 控件属性: Text:获取或设置string类型的IP地址 Value:获取或设置 ...
- NumberFormat类
NumberFormat表示数字的格式化类,即可以按照本地的风格习惯进行数字的显示. NumberFormat是一个抽象类,和MessageFormat类一样,都是Format类的子类,本类在使用时可 ...
- openstack虚拟机启动过程
核心项目3个 1.控制台 服务名:Dashboard 项目名:Horizon 功能:web方式管理云平台,建云主机,分配网络,配安全组,加云盘 2.计算 服务名:计算 项目名:Nova 功能:负责响应 ...
- 跟我一起玩转Sencha Touch 移动 WebApp 开发(一)
1.目录 移动框架简介,为什么选择Sencha Touch? 环境搭建 创建项目框架,框架文件简介 创建简单Tabpanel案例 自定义图标的方式 WebApp产品测试和发布 HTML5离线缓存 发布 ...
- VirtualBox5中安装的CentOS6.7安装增强工具
1.安装编译内核的相关组件 yum install kernel-devel gcc 2.安装VirtualBox增强功能 sh ./VBoxLinuxAdditions.run 3.重启系统 reb ...
- AspnetIdentitySample
https://github.com/rustd/AspnetIdentitySample http://www.asp.net/web-forms/overview/getting-started/ ...
- js中自定义事件,使用了jQuery
$(function(){ $('#btn').bind("myClick", function(){ //自定义myClick事件 $('#test').append(" ...
- Python开发【第十八篇】:MySQL(二)
视图 视图是一个虚拟表(非真实存在),其本质是[根据SQL语句获取动态的数据集,并为其命名],用户使用时只需使用[名称]即可获取结果集,并可以将其当作表来使用. SELECT * FROM ( SEL ...
- ubuntu系统下更新jdk版本
1. 添加软件源 sudo add-apt-repository ppa:webupd8team/java 2. 更新软件源 sudo apt-get update 3. 安装 jdk1.8 sudo ...
- CF449B Jzzhu and Cities (最短路)
CF449B CF450D http://codeforces.com/contest/450/problem/D http://codeforces.com/contest/449/problem/ ...