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护城河的挖掘的更多相关文章

  1. bzoj 1670: [Usaco2006 Oct]Building the Moat护城河的挖掘 -- 凸包

    1670: [Usaco2006 Oct]Building the Moat护城河的挖掘 Time Limit: 3 Sec  Memory Limit: 64 MB Description 为了防止 ...

  2. bzoj 1670 [Usaco2006 Oct]Building the Moat护城河的挖掘——凸包

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1670 用叉积判断.注意两端的平行于 y 轴的. #include<cstdio> ...

  3. 牛客假日团队赛5J 护城河 bzoj 1670: [Usaco2006 Oct]Building the Moat护城河的挖掘 (凸包的周长)

    链接:https://ac.nowcoder.com/acm/contest/984/J 来源:牛客网 护城河 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言6 ...

  4. bzoj 1670: [Usaco2006 Oct]Building the Moat护城河的挖掘【凸包】

    凸包模板 #include<iostream> #include<cstdio> #include<algorithm> #include<cmath> ...

  5. 【BZOJ】1670: [Usaco2006 Oct]Building the Moat护城河的挖掘(凸包)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1670 裸打了凸包.. #include <cstdio> #include <cs ...

  6. BZOJ_1670_[Usaco2006 Oct]Building the Moat护城河的挖掘_求凸包

    BZOJ_1670_[Usaco2006 Oct]Building the Moat护城河的挖掘_求凸包 Description 为了防止口渴的食蚁兽进入他的农场,Farmer John决定在他的农场 ...

  7. BZOJ1670 [Usaco2006 Oct]Building the Moat护城河的挖掘

    裸的凸包...(和旋转卡壳有什么关系吗...蒟蒻求教T T) 话说忘了怎么写了...(我以前都是先做上凸壳再做下凸壳的说) 于是看了下hzwer的写法,用了向量的点积,方便多了,于是果断学习(Orz) ...

  8. 【计算几何】【凸包】bzoj1670 [Usaco2006 Oct]Building the Moat护城河的挖掘

    #include<cstdio> #include<cmath> #include<algorithm> using namespace std; #define ...

  9. bzoj1670【Usaco2006 Oct】Building the Moat 护城河的挖掘

    1670: [Usaco2006 Oct]Building the Moat护城河的挖掘 Time Limit: 3 Sec  Memory Limit: 64 MB Submit: 387  Sol ...

随机推荐

  1. shell操作mysql之增删改查

    假设mysql用户名root 密码123456,新建测试数据表utable 脚本如下: #!/bin/bash#mysqlop.shmysql="/app/local/mysql/bin/m ...

  2. 9月23日JavaScript作业----子菜单下拉

    例题一.子菜单下拉 <style type="text/css"> *{ margin:0px auto; padding:0px} #menu{ width:700p ...

  3. 知乎布局||offsetTop||侧边栏自动等高

    1.对a标签的详细介绍 直接在a标签使用onclick,怎么去除a的默认链接,onclick="return test()" 注意这里的return 不可舍去,test函数可以直接 ...

  4. NodeJS Debugger

    http://cnodejs.org/topic/4f16442ccae1f4aa27001105 http://blog.csdn.net/ygh_0912/article/details/9108 ...

  5. draw9的使用说明

    转载来自:http://isux.tencent.com/android-ui-9-png.html 在Android的设计过程中,为了适配不同的手机分辨率,图片大多需要拉伸或者压缩,这样就出现了可以 ...

  6. Autofac.Integration.Owin

    public static IAppBuilder UseAutofacMiddleware(this IAppBuilder app, ILifetimeScope container) { if ...

  7. JavaWeb学习总结(一)——JavaWeb开发入门

    http://www.cnblogs.com/xdp-gacl/p/3729033.html 只为成功找方法,不为失败找借口! JavaWeb学习总结(一)--JavaWeb开发入门 一.基本概念 1 ...

  8. OC-改错题

    1,类方法中不能访问成员变量 2,id后不能加*(因为id相当于NSObject *) 3,id类型的变量不能用点语法 4,类对象只能调用类方法,不能调用对象方法 .description #impo ...

  9. Yii 框架里数据库操作详解-[增加、查询、更新、删除的方法 'AR模式']

    public function getMinLimit () {        $sql = "...";        $result = yii::app()->db-& ...

  10. iframe和frameset的使用

    iframe是:inner frame的缩写, 必须指明src属性,不能直接在里面写内容, 否则不会显示. 可以载入html, *.图片文件, txt文件等等. html的属性中,表示长度高度的尺寸属 ...