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 ...
随机推荐
- http核心模块 node
ttp超文本传输协议* 协议至少双方 -> http双方!! * 客户端(浏览器) -> 服务器 BS - 原生应用(QQ) -> 服务器 CS * 就是数据如何传输 * 特点: * ...
- flutter进行自动编译操作步骤
环境: mac os 操作系统 xcode最新版本 10.2.1 flutter进行编译报错解决方案: (null): warning: (armv7) /Users/tommy/Desktop/Pr ...
- css盒模型宽高混合计算calc
例如: .element{ width:calc(expression); } 兼容性:在IE9+.FF4.0+.Chrome19+.Safari6+都得到了较好支持,但是在移动端的支持不是很好. 其 ...
- Delphi 窗体函数GetWindowRect 取窗口矩形坐标
GetWindowRect,用于取窗口矩形坐标.返回值类型:布尔型(LongBool).执行成功返回真(True),否则返回假(False);参数1类型:整数型(HWND),目标窗口的窗口句柄;参数2 ...
- CacheException: java.io.OptionalDataException
CacheException: java.io.OptionalDataException iro.authc.AbstractAuthenticator] - Authentication fail ...
- php-验证码类-PDO类-缩略图类
Verify.class.php 验证码类 <?php class Verify{ const VERIFY_TYPE_NUM=1; const VERIFY_TYPE_EN=2; const ...
- Minimum Snap轨迹规划详解(2)corridor与时间分配
在上一篇文章中,我们得到的轨迹并不是很好,与路径差别有点大,我们期望规划出的轨迹跟路径大致重合,而且不希望有打结的现象,而且希望轨迹中的速度和加速度不超过最大限幅值.为了解决这些问题有两种思路: 思路 ...
- 听说这个FFT跑得巨jb快
#pragma GCC target ("avx2") #include <immintrin.h> #include<bits/stdc++.h> usi ...
- 天梯L3-003. 社交集群——并查集
在社交网络平台注册时,用户通常会输入自己的兴趣爱好,以便找到和自己兴趣相投的朋友.有部分兴趣相同的人们就形成了“社交集群”.现请你编写程序,找出所有的集群. 输入格式: 输入的第一行给出正整数N(&l ...
- 架构-Java-Netty:Netty框架
ylbtech-架构-Java-Netty:Netty框架 Netty是由JBOSS提供的一个java开源框架.Netty提供异步的.事件驱动的网络应用程序框架和工具,用以快速开发高性能.高可靠性的网 ...