【BZOJ 1038】【ZJOI 2008】瞭望塔
http://www.lydsy.com/JudgeOnline/problem.php?id=1038
半平面交裸题,求完半平面后在折线段上的每个点竖直向上和半平面上的每个点竖直向下求距离,统计最小的值作为答案即可。
1A!!!斯巴达!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
#include<cmath>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 303; struct Point {
double x, y;
Point(double _x = 0, double _y = 0) : x(_x), y(_y) {}
} t[N], p[N];
struct Line {
Point p, v; double ang;
Line(Point _p = Point(0, 0), Point _v = Point(0, 0), double _ang = 0) : p(_p), v(_v), ang(_ang) {}
bool operator < (const Line &x) const {
return ang < x.ang;
}
} l[N], q[N]; Point operator + (Point a, Point b) {return Point(a.x + b.x, a.y + b.y);}
Point operator - (Point a, Point b) {return Point(a.x - b.x, a.y - b.y);}
Point operator * (Point a, double x) {return Point(a.x * x, a.y * x);}
Point operator / (Point a, double x) {return Point(a.x / x, a.y / x);} int dcmp(double x) {return fabs(x) < 1e-8 ? 0 : (x < 0 ? -1 : 1);}
double Dot(Point a, Point b) {return a.x * b.x + a.y * b.y;}
double Cross(Point a, Point b) {return a.x * b.y - a.y * b.x;}
double sqr(double x) {return x * x;}
double dis(Point a, Point b) {return sqrt(sqr(a.x - b.x) + sqr(a.y - b.y));} bool onleft(Point a, Line b) {return dcmp(Cross(a - b.p, b.v)) < 0;}
Point intersection(Line a, Line b) {
Point u; double t;
u = a.p - b.p;
t = Cross(b.v, u) / Cross(a.v, b.v);
return a.p + (a.v * t);
} int n, head = 1, tail = 2;
double ans; void mkhalf() {
q[1] = l[1]; q[2] = l[2];
p[1] = intersection(q[1], q[2]);
for(int i = 3; i < n; ++i) {
while (head < tail && !onleft(p[tail - 1], l[i])) --tail;
while (head < tail && !onleft(p[head], l[i])) ++head;
q[++tail] = l[i];
if (dcmp(Cross(q[tail].v, q[tail - 1].v) == 0)) {
--tail;
if (onleft(l[i].p, q[tail])) q[tail] = l[i];
}
if (head < tail) p[tail - 1] = intersection(q[tail - 1], q[tail]);
}
// while (head < tail + 1 && !onleft(p[tail - 1], q[head])) --tail;
} double cal(int num) {
Point j;
double x = t[num].x, y = t[num].y;
if (x <= p[head].x) {
j = intersection(q[head], Line(t[num], Point(0, 1)));
return j.y - y;
}
if (x >= p[tail - 1].x) {
j = intersection(q[tail], Line(t[num], Point(0, 1)));
return j.y - y;
}
int left = head, right = tail - 1, mid;
while (left < right) {
mid = (left + right + 1) >> 1;
if (p[mid].x > x) right = mid - 1;
else left = mid;
}
j = intersection(q[left + 1], Line(t[num], Point(0, 1)));
return j.y - y;
} double cal2(int num) {
Point j;
double x = p[num].x, y = p[num].y;
int left = 1, right = n, mid;
while (left < right) {
mid = (left + right + 1) >> 1;
if (t[mid].x > x) right = mid - 1;
else left = mid;
}
j = intersection(Line(t[left], t[left + 1] - t[left]), Line(p[num], Point(0, -1)));
return y - j.y;
} int main() {
scanf("%d", &n);
for(int i = 1; i <= n; ++i) scanf("%lf", &t[i].x);
for(int i = 1; i <= n; ++i) scanf("%lf", &t[i].y); for(int i = 1; i < n; ++i) l[i] = Line(t[i], t[i + 1] - t[i]), l[i].ang = atan2(l[i].v.y, l[i].v.x);
sort(l + 1, l + n);
mkhalf(); ans = cal(1);
for(int i = 2; i <= n; ++i)
ans = min(ans, cal(i));
for(int i = head; i < tail; ++i)
if (t[1].x <= p[i].x && p[i].x <= t[n].x)
ans = min(ans, cal2(i)); // for(int i = head; i < tail; ++i) printf("%.2lf %.2lf\n", p[i].x, p[i].y); printf("%.3lf\n", ans + 1e-8);
return 0;
}
因为半平面不会围成一个"圈",所以我把最后"去除冗余"的部分注释掉了。最后输出答案加eps是听别人说的,不加会怎么样呢,我也不知道_(:з」∠)_
【BZOJ 1038】【ZJOI 2008】瞭望塔的更多相关文章
- 【BZOJ 1038】[ZJOI2008]瞭望塔
[题目链接]:http://www.lydsy.com/JudgeOnline/problem.php?id=1038 [题意] [题解] 可以看到所有村子的瞭望塔所在的位置只会是在相邻两个村子所代表 ...
- BZOJ 1040 ZJOI 2008 骑士 基环树林+树形DP
题目大意:有一些骑士.他们每个人都有一个权值.可是因为一些问题,每个骑士都特别讨厌还有一个骑士.所以不能把他们安排在一起.求这些骑士所组成的编队的最大权值和是多少. 思路:首先貌似是有向图的样子,可是 ...
- BZOJ 1040 ZJOI 2008 骑士 树形DP
题意: 有一些战士,他们有战斗力和讨厌的人,选择一些战士,使他们互不讨厌,且战斗力最大,范围1e6 分析: 把战士看作点,讨厌的关系看作一条边,连出来的是一个基环树森林. 对于一棵基环树,我们找出环, ...
- bzoj 1034 [ ZJOI 2008 ] 泡泡堂BNB —— 贪心
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1034 一开始想了个很麻烦的贪心做法,对于每个 a[i],找第一个大于它的 b 匹配…… 然后 ...
- 【BZOJ】【1038】【ZJOI2008】瞭望塔
计算几何/半平面交 说是半平面交,实际上只是维护了个下凸壳而已……同1007水平可见直线 对于每条线段,能看到这条线段的点都在这条线段的“上方”,那么对所有n-1条线段求一个可视区域的交,就是求一个半 ...
- 【BZOJ 1038】 1038: [ZJOI2008]瞭望塔
1038: [ZJOI2008]瞭望塔 Description 致力于建设全国示范和谐小村庄的H村村长dadzhi,决定在村中建立一个瞭望塔,以此加强村中的治安.我们将H村抽象为一维的轮廓.如下图所示 ...
- BZOJ 1038 瞭望塔
Description 致力于建设全国示范和谐小村庄的H村村长dadzhi,决定在村中建立一个瞭望塔,以此加强村中的治安.我们将H村抽象为一维的轮廓.如下图所示 我们可以用一条山的上方轮廓折线(x1, ...
- 1038: [ZJOI2008]瞭望塔 - BZOJ
Description 致力于建设全国示范和谐小村庄的H村村长dadzhi,决定在村中建立一个瞭望塔,以此加强村中的治安.我们将H村抽象为一维的轮廓.如下图所示 我们可以用一条山的上方轮廓折线(x1, ...
- bzoj 1038 瞭望塔 半平面交+分段函数
题目大意 给你一座山,山的形状在二维平面上为折线 给出\((x_1,y_1),(x_2,y_2)...(x_n,y_n)\)表示山的边界点或转折点 现在要在\([x_1,x_n]\)(闭区间)中选择一 ...
- bzoj千题计划126:bzoj1038: [ZJOI2008]瞭望塔
http://www.lydsy.com/JudgeOnline/problem.php?id=1038 本题可以使用三分法 将点按横坐标排好序后 对于任意相邻两个点连成的线段,瞭望塔的高度 是单峰函 ...
随机推荐
- 【CSS】使用边框和背景
1. 应用边框样式 先从控制边框样式的属性开始.简单边框有三个关键属性:border-width.border-style 和 border-color . <!DOCTYPE html> ...
- OAuth2授权原理
最近在做第三方接入的,初步定下使用OAuth2协议,花了些时间对OAuth2的授权方式做了些了解. 我还记得一两年前,跟一位同事聊起互联网时,当时我说过一个想法: 目前不少较为稀有的资源,很多都是论坛 ...
- jmeter 函数助手里的P,property的使用
1.函数助手里的 p及property的使用 ${__P(init,2)} , ${__property(init,start,200)} 可以自行定义变量名称,及变量的默认值 P 变量名为init, ...
- 如何撰写PRD
PRD(Product-Requirement-Document,产品需求文档),这对于任何一个产品经理来说都不会陌生的一个文档,一个PRD是衡量一个产品经理整体思维的标准,一个PRD可以看出一个产品 ...
- 实现鼠标拖动canvas绘制的图片
不啰嗦上代码: <html> <head> <meta http-equiv="Content-Type" content="text/ht ...
- BZOJ 2005: [Noi2010]能量采集
2005: [Noi2010]能量采集 Time Limit: 10 Sec Memory Limit: 552 MBSubmit: 3312 Solved: 1971[Submit][Statu ...
- 用Metasploit破解Mysql用户名和密码
下面这个方式是普适的,但缺点就是必须要有自己的用户名和密码字典.其原理就是用user.txt与pass.txt的两个文本去不停交叉验证. msf auxiliary(mysql_login) > ...
- 由项目中一个hash2int函数引发的思考
hash2int /** * 计算一个字符串的md5折算成int返回 * @param type $str * @return type */ function hash2int($str) { $m ...
- Java 集合系列10之 HashMap详细介绍(源码解析)和使用示例
概要 这一章,我们对HashMap进行学习.我们先对HashMap有个整体认识,然后再学习它的源码,最后再通过实例来学会使用HashMap.内容包括:第1部分 HashMap介绍第2部分 HashMa ...
- 解析百度搜索结果链接的url,获取真正的url
通常,在百度输入关键词搜索出现的列表页,点击目标链接,然而跳转的时候却是百度地址,经过百度解析,才真的跳到目标页面. 在SEO中,经常需要看下自己的网站排名,又不想手动每天手动去点,可用以下方法去得到 ...