bzoj1038题解
【题意分析】
求一个下凸壳与一段折线的距离。
【解题思路】
先把直线按斜率排序,求出下凸壳,然后枚举所有的顶点的x坐标求最短y坐标差,复杂度O(nlog2n)。
【参考代码】
#include <algorithm>
#include <cstdio>
#define REP(i,low,high) for(register int i=(low);i<=(high);++i)
#define __function__(type) /*__attribute__((optimize("-O2"))) inline */type
#define __procedure__ /*__attribute__((optimize("-O2"))) inline */void
using namespace std; //defs {
#include <cmath>
template<typename real>
inline __function__(bool) fequals(
const real&one,const real&another,const real&eps=1e-
) {return fabs(one-another)<eps;}
template<typename real>
inline __function__(bool) funequals(
const real&one,const real&another,const real&eps=1e-
) {return fabs(one-another)>=eps;}
//} defs //geometry {
template<typename T=double> struct Point
{
T x,y; Point(const T&_x=,const T&_y=):x(_x),y(_y) {}
__function__(bool) operator==(const Point<T>&thr)const
{
return fequals(x,thr.x)&&fequals(y,thr.y);
}
__function__(bool) operator!=(const Point<T>&thr)const
{
return funequals(x,thr.x)||funequals(y,thr.y);
}
__function__(Point<T>) operator+(const Point<T>&thr)const
{
return Point<T>(x+thr.x,y+thr.y);
}
__function__(Point<T>) operator-(const Point<T>&thr)const
{
return Point<T>(x-thr.x,y-thr.y);
}
__function__(Point<T>) operator*(const T&lambda)const
{
return Point<T>(x*lambda,y*lambda);
}
__function__(Point<double>) operator/(const T&lambda)const
{
return Point<double>(double(x)/lambda,double(y)/lambda);
}
__function__(double) theta()const
{
return x>?(y<)**M_PI+atan(y/x):M_PI+atan(y/x);
}
__function__(double) theta_x()const{return x?atan(y/x):M_PI/;}
__function__(double) theta_y()const{return y?atan(x/y):M_PI/;}
};
template<typename T>
inline __function__(T) dot_product(const Point<T>&A,const Point<T>&B)
{
return A.x*B.x+A.y*B.y;
}
template<typename T>
inline __function__(T) cross_product(const Point<T>&A,const Point<T>&B)
{
return A.x*B.y-A.y*B.x;
}
template<typename T>
inline __function__(double) Euclid_distance(const Point<T>&A,const Point<T>&B)
{
return sqrt(pow(A.x-B.x,),pow(A.y-B.y,));
}
template<typename T>
inline __function__(T) Manhattan_distance(const Point<T>&A,const Point<T>&B)
{
return fabs(A.x-B.x)+fabs(A.y-B.y);
}
struct kbLine
{
//line:y=kx+b
double k,b; kbLine(const double&_k=,const double&_b=):k(_k),b(_b) {}
__function__(bool) operator==(const kbLine&thr)const
{
return fequals(k,thr.k)&&fequals(b,thr.b);
}
__function__(bool) operator!=(const kbLine&thr)const
{
return funequals(k,thr.k)||funequals(b,thr.b);
}
__function__(bool) operator<(const kbLine&thr)const{return k<thr.k;}
__function__(bool) operator>(const kbLine&thr)const{return k>thr.k;}
template<typename T>
__function__(bool) build_line(const Point<T>&A,const Point<T>&B)
{
return fequals(A.x,B.x)?:(k=double(A.y-B.y)/(A.x-B.x),b=A.y-k*A.x,);
}
__function__(double) theta_x()const{return atan(k);}
__function__(double) theta_y()const{return theta_x()-M_PI/;}
__function__(double) get(const double&x)const{return k*x+b;}
};
__function__(bool) parallel(const kbLine&A,const kbLine&B)
{
return A!=B&&(fequals(A.k,B.k)||A.k!=A.k&&B.k!=B.k);
}
__function__(Point<double>*) cross(const kbLine&A,const kbLine&B)
{
if(A==B||parallel(A,B)) return NULL; double _x=double(B.b-A.b)/(A.k-B.k);
Point<double>*ret=new Point<double>(_x,A.k*_x+A.b); return ret;
}
//} geometry static int n; double x[],y[]; int stack[]; kbLine L[]; int main()
{
scanf("%d",&n); REP(i,,n) scanf("%lf",x+i); REP(i,,n) scanf("%lf",y+i);
REP(i,,n-) L[i].build_line(Point<>(x[i],y[i]),Point<>(x[i+],y[i+]));
sort(L+,L+n); int top=stack[]=; REP(i,,n-)
{
for(;i<=n&&(L[i]==L[stack[top]]||parallel(L[i],L[stack[top]]));++i);
for(;i<=n&&top>;--top)
{
Point<>*last=cross(L[stack[top-]],L[stack[top]]),
* now=cross(L[ i ],L[stack[top]]);
if(last->x<now->x) break; delete last; delete now;
}
stack[++top]=i;
}
double ans=1e10; int j=;
REP(i,,n)
{
for(;L[stack[j]].get(x[i])>L[stack[j-]].get(x[i]);++j);
ans=min(ans,L[stack[--j]].get(x[i])-y[i]);
}
REP(i,,top)
{
Point<>*now=cross(L[stack[i-]],L[stack[i]]);
j=upper_bound(x+,x+n+,now->x)-x; kbLine tmp;
tmp.build_line(Point<>(x[j-],y[j-]),Point<>(x[j],y[j]));
ans=min(ans,now->y-tmp.get(now->x)); delete now;
}
return printf("%.3lf\n",ans),;
}
bzoj1038题解的更多相关文章
- 【bzoj1038】瞭望塔
[bzoj1038]瞭望塔 题意 致力于建设全国示范和谐小村庄的H村村长dadzhi,决定在村中建立一个瞭望塔,以此加强村中的治安.我们将H村抽象为一维的轮廓.如下图所示 我们可以用一条山的上方轮廓折 ...
- 【BZOJ1038】[ZJOI2008]瞭望塔 半平面交
[BZOJ1038][ZJOI2008]瞭望塔 Description 致力于建设全国示范和谐小村庄的H村村长dadzhi,决定在村中建立一个瞭望塔,以此加强村中的治安.我们将H村抽象为一维的轮廓.如 ...
- 2016 华南师大ACM校赛 SCNUCPC 非官方题解
我要举报本次校赛出题人的消极出题!!! 官方题解请戳:http://3.scnuacm2015.sinaapp.com/?p=89(其实就是一堆代码没有题解) A. 树链剖分数据结构板题 题目大意:我 ...
- noip2016十连测题解
以下代码为了阅读方便,省去以下头文件: #include <iostream> #include <stdio.h> #include <math.h> #incl ...
- BZOJ-2561-最小生成树 题解(最小割)
2561: 最小生成树(题解) Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1628 Solved: 786 传送门:http://www.lyd ...
- Codeforces Round #353 (Div. 2) ABCDE 题解 python
Problems # Name A Infinite Sequence standard input/output 1 s, 256 MB x3509 B Restoring P ...
- 哈尔滨理工大学ACM全国邀请赛(网络同步赛)题解
题目链接 提交连接:http://acm-software.hrbust.edu.cn/problemset.php?page=5 1470-1482 只做出来四道比较水的题目,还需要加强中等题的训练 ...
- 2016ACM青岛区域赛题解
A.Relic Discovery_hdu5982 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Jav ...
- poj1399 hoj1037 Direct Visibility 题解 (宽搜)
http://poj.org/problem?id=1399 http://acm.hit.edu.cn/hoj/problem/view?id=1037 题意: 在一个最多200*200的minec ...
随机推荐
- vue撸的demo
之前做过一个layui+flask的测试平台,后来听说vue挺火,学了下,做个记录 一.想法 主要想开发几大功能,方便以后复用 1.工作中常常需要记录知识,所以我决定会做一个类似markdown笔记的 ...
- 再也不用字符串拼接dom元素了
<script type="text/html" id="tmp"> <div class="cla"> <u ...
- new和delete,p150
创建对象,不带()的称为不提供显式初始化,()为空的称为值初始化. 内置类型和没定义默认ctor的类型,不带()未初始化,带()初始化为0.但有默认构造函数的,带不带()都是利用默认构造函数初始化. ...
- docker部署coredns
示例: [root@corends coredns]# cd /home/coredns/ [root@corends coredns]# ls -al total drwxr-xr-x root r ...
- NX二次开发-Block UI C++界面Specify Point(指定点)控件的获取(持续补充)
Specify Point(指定点)控件的获取 NX9+VS2012 #include <uf.h> #include <uf_ui.h> UF_initialize(); / ...
- “今日头条杯”首届湖北省大学程序设计竞赛--F. Flower Road
题目链接:点这 github链接:(包含数据和代码,题解):点这 链接:https://www.nowcoder.com/acm/contest/104/E来源:牛客网 题目描述 (受限于评测机,此题 ...
- elasticsearch5.5.2安装
elasticsearch5.x安装中一些问题的解决办法 最近在学习elk,由于编译安装使用5.2.1版本的elasticsearch,所以遇到了很多问题,下面是一些问题及解决办法. 1.修改访问e ...
- (7)centos7 用户管理
1.创建用户 useradd meng 如果创建用户时没有指定属于哪个组,则默认会创建一个名字与用户相同的组并归属于此组 对应的home目录下回创建一个meng的文件夹 2.设置密码 passwd m ...
- Gerrit Handbook for Commercial Project
前注:常见‘坑’ 不要使用 2.14 版本 gerrit.conf 中 [sendemail] 部分必须有 from = ... 字段,否则无法添加邮箱 gerrit 以用户组为单位划分权限,先将用户 ...
- 一些识别CMS的经验方法总结
今天学到了一些识别CMS的快速方法,也算是一种信息收集经验的积累,在这里要感谢一下我的同事“gakki的童养夫”对我的大力支持. 如何判断网站的CMS? robots.txt文件 robots.txt ...