【bzoj1038】瞭望塔
【bzoj1038】瞭望塔
题意
致力于建设全国示范和谐小村庄的H村村长dadzhi,决定在村中建立一个瞭望塔,以此加强村中的治安。我们将H村抽象为一维的轮廓。如下图所示 我们可以用一条山的上方轮廓折线(x1, y1), (x2, y2), …. (xn, yn)来描述H村的形状,这里x1 < x2 < …< xn。瞭望塔可以建造在[x1, xn]间的任意位置, 但必须满足从瞭望塔的顶端可以看到H村的任意位置。可见在不同的位置建造瞭望塔,所需要建造的高度是不同的。为了节省开支,dadzhi村长希望建造的塔高度尽可能小。请你写一个程序,帮助dadzhi村长计算塔的最小高度。
分析
分析1:模拟退火+二分
调不出来。
调得出来的时候估计阳寿已尽。
有生之年内应该不会调得出来吧。
就放一个半成品代码吧。
#include <cstdio>
#include <cctype>
#include <cmath>
#include <ctime>
#include <algorithm>
using namespace std;
#define rep(i,a,b) for (int i=(a);i<=(b);i++)
const int N=512;
const double MAX=1e11;
const double EPS=1e-6;
const int T=1;
const double SPEED=0.99;
const double END=0.00001;
int rd(void);
int n;
struct point {
double x,y;
point(double _x=0,double _y=0) {
x=_x,y=_y;
}
friend point operator - (point a,point b) {
return point(a.x-b.x,a.y-b.y);
}
}p[N];
double det(point a,point b) {
return a.x*b.y-a.y*b.x;
}
double now; double nd;
double ans;
int rd(void) {
int x=0,f=1; char c=getchar();
for (;!isdigit(c);c=getchar()) if (c=='-') f=-1;
for (;isdigit(c);c=getchar()) x=x*10+c-'0';
return x*f;
}
int cmp(double a) {
if (fabs(a)<EPS) return 0;
return a<0?-1:1;
}
int Check(double xn,double yn) {
rep(i,2,n) {
double t=det(p[i-1]-point(xn,yn),p[i]-point(xn,yn));
if (cmp(t)<0) return 0;
}
return 1;
}
int InBorder(double xn,double l,double r) {
return cmp(xn-l)>=0&&cmp(r-xn)>=0;
}
double Solve(double xn) {
double l=0,r=MAX;
while (cmp(r-l)>0) {
double mid=(l+r)/2;
if (Check(xn,mid))
r=mid;
else l=mid;
}
double bsH;
rep(i,1,n-1)
if (InBorder(xn,p[i].x,p[i+1].x)) {
double k=(p[i+1].y-p[i].y)/(p[i+1].x-p[i].x),b=p[i].y-k*p[i].x;
bsH=k*xn+b;
break;
}
return l-bsH;
}
double RandFloat(void) {
return rand()%1000/1000.0;
}
double SA(void) {
now=(p[1].x+p[n].x)/2.0;
nd=Solve(now);
double res=nd;
for (double tem=p[n].x-p[1].x;tem>END;tem*=SPEED) {
double nx=now+tem*(RandFloat()*2-1);
if (!InBorder(nx,p[1].x,p[n].x)) continue;
double nxd=Solve(nx); double slack=RandFloat();
if (cmp(nxd-nd)<0||cmp(tem-slack)>0) {
now=nx;
nd=nxd;
res=min(res,nxd);
}
}
return res;
}
int main(void) {
#ifndef ONLINE_JUDGE
freopen("bzoj1038.in","r",stdin);
freopen("bzoj1038.out","w",stdout);
#endif
srand(19980406);
n=rd();
rep(i,1,n) p[i].x=rd();
rep(i,1,n) p[i].y=rd();
ans=MAX;
rep(tms,1,T) {
double t=SA();
ans=min(ans,t);
}
printf("%0.3lf\n",ans);
return 0;
}
分析2:三分法
对于一条线段,上面的点的答案必然是单峰的。
证明略,自己画个图YY去吧。
有一篇题解:http://blog.csdn.net/Fuxey/article/details/50528819
分析3:半平面交
http://blog.csdn.net/regina8023/article/details/43935773
【bzoj1038】瞭望塔的更多相关文章
- BZOJ1038 瞭望塔
学习了半平交面. 我这里写的是训练指南中的双端队列,每次判断是否删去更优然后更新. 看hzwer中有一处不太明白就是为何要将两段加入队列 后来对拍出错才知道是因为精度,当两线重合时他们叉积返回值是一个 ...
- 【BZOJ1038】[ZJOI2008]瞭望塔 半平面交
[BZOJ1038][ZJOI2008]瞭望塔 Description 致力于建设全国示范和谐小村庄的H村村长dadzhi,决定在村中建立一个瞭望塔,以此加强村中的治安.我们将H村抽象为一维的轮廓.如 ...
- bzoj1038: [ZJOI2008]瞭望塔
Description 致力于建设全国示范和谐小村庄的H村村长dadzhi,决定在村中建立一个瞭望塔,以此加强村中的治安.我们将H村抽象为一维的轮廓.如下图所示 我们可以用一条山的上方轮廓折线(x1, ...
- bzoj千题计划126:bzoj1038: [ZJOI2008]瞭望塔
http://www.lydsy.com/JudgeOnline/problem.php?id=1038 本题可以使用三分法 将点按横坐标排好序后 对于任意相邻两个点连成的线段,瞭望塔的高度 是单峰函 ...
- 【bzoj1038】瞭望塔 半平面交
题目描述 致力于建设全国示范和谐小村庄的H村村长dadzhi,决定在村中建立一个瞭望塔,以此加强村中的治安.我们将H村抽象为一维的轮廓.如下图所示 我们可以用一条山的上方轮廓折线(x1, y1), ( ...
- 【BZOJ1038】【ZJOI2008】瞭望塔 [模拟退火]
瞭望塔 Time Limit: 10 Sec Memory Limit: 162 MB[Submit][Status][Discuss] Description 致力于建设全国示范和谐小村庄的H村村 ...
- 「BZOJ1038」「洛谷P2600」「ZJOI2008」瞭望塔 半平面交+贪心
题目链接 BZOJ/洛谷 题目描述 致力于建设全国示范和谐小村庄的H村村长dadzhi,决定在村中建立一个瞭望塔,以此加强村中的治安. 我们将H村抽象为一维的轮廓.如下图所示: 我们可以用一条山的上方 ...
- [BZOJ1038][ZJOI2008]瞭望塔(半平面交)
1038: [ZJOI2008]瞭望塔 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 2999 Solved: 1227[Submit][Statu ...
- 刷题总结——瞭望塔(bzoj1038)
题目: Description 致力于建设全国示范和谐小村庄的H村村长dadzhi,决定在村中建立一个瞭望塔,以此加强村中的治安.我们将H村抽象为一维的轮廓.如下图所示 我们可以用一条山的上方轮廓折线 ...
随机推荐
- JS脚本语言里的循环
js脚本语言: 循环:(循环操作某一个功能(执行某段代码)) 四要素: 循环初始值 循环条件 状态改变 循环体 for(穷举 迭代) while 举例:(穷举) 与7相关的数 <scr ...
- C#获取当前页面的url
C#获取当前页面的url string a= Request.ApplicationPath; // / string b = Request.CurrentExecutionFilePath; // ...
- tee 命令基本使用方法、输出到多个文件
功能说明:读取标准输入的数据,并将其内容输出成文件.语 法:tee [-ai][--help][--version][文件...]补充说明:tee指令会从标准输入设备读取数据,将其内容输出到标准输出 ...
- UVA 10817 十一 Headmaster's Headache
Headmaster's Headache Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Sub ...
- SetWindowHookEx 做消息响应
HHOOK g_Hook = NULL; LRESULT CALLBACK CallWndProc( _In_ int nCode, _In_ WPARAM wParam, _In_ LPARAM l ...
- 如何解决Angular 2 的templateUrl和styleUrl的路径问题?
参考地址:https://github.com/kittencup/angular2-ama-cn/issues/18 前言: templateUrl表示的是组件在浏览器中运行时依赖的模板地址,所以在 ...
- Codeforces Round #249 (Div. 2)B(贪心法)
B. Pasha Maximizes time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- FZU 2216 The Longest Straight(最长直道)
Description 题目描述 ZB is playing a card game where the goal is to make straights. Each card in the dec ...
- 获取Token不完整问题
有时会遇到获取Token只能获取一半的问题,明明有两个Cookie,但只获取到一个,这个是因为301重定向跳转设置问题,设置为True就可以获取到完整的Token了. myHttpWebRequest ...
- Codeforces Round #375 (Div. 2) C. Polycarp at the Radio 贪心
C. Polycarp at the Radio time limit per test 2 seconds memory limit per test 256 megabytes input sta ...