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 ...
随机推荐
- 自然语言18.2_NLTK命名实体识别
QQ:231469242 欢迎nltk爱好者交流 http://blog.csdn.net/u010718606/article/details/50148261 NLTK中对于很多自然语言处理应用有 ...
- JSP简单标签带属性开发
1.开发带属性的标签,标签处理器类中属性要有相应setter方法,符合javaBean规范 2.tld文件中进行相应属性标签配置 属性配置相关说明如下图 3.简单标签带属性的例子 1).通过设置标签属 ...
- js随笔,css和js禁止网页选择文本,table的class样式使得td的class样式失效,jquery获得元素坐标
css使用user-select,user-select不是W3C标准,浏览器支持不完整:user-select有两个值,none用户不可以选择文本,text用户可以选择文本 body{-moz-us ...
- Android学习笔记——Handler(二)
对比请看http://blog.sina.com.cn/s/blog_78c913e30100uqmf.html 以下代码是MainActivity.java中的代码 package com.exam ...
- tableview中在tableheaderView上放一个视图,第一次进入视图显示不正常,往下拉视图仍然不正常,往上拉视图正常
解决办法: frame来源不正常,从直接在viewDidLoad方法中设置的frame,改为 - (void)viewDidLayoutSubviews { [super viewDidLayoutS ...
- Quartz.NET总结(一)入门
前段时间,花了大量的时间,将原先的计划任务,切换到Quartz.NET来进行管理.原先的后台定时服务都是通过计划任务来实现的,但是随着业务增长,计划任务也越来越多,每个后台服务,都得创建一个计划任务. ...
- Code First Migrations: Making __MigrationHistory not a system table
https://blog.oneunicorn.com/2012/02/27/code-first-migrations-making-__migrationhistory-not-a-system- ...
- OC-copy
一,堆与栈 1,栈区,stack: 后进先出,由编译器自动分配并释放,一般存放函数的参数值.局部变量 2,堆区,heap:先进先出,由程序员分配和释放 3,全局区,静态区:程序结束后由系统释放, 4, ...
- Yii2 高级版新建一个 Api 应用
原文地址:http://www.getyii.com/topic/28 先在项目的根目录下复制一份 backend 为 api: cp backend/ api -r 拷贝 api 环境 cp -a ...
- Java并发编程核心方法与框架-Future和Callable的使用
Callable接口与Runnable接口对比的主要优点是Callable接口可以通过Future获取返回值.但是Future接口调用get()方法取得结果时是阻塞的,如果调用Future对象的get ...