【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 本题可以使用三分法 将点按横坐标排好序后 对于任意相邻两个点连成的线段,瞭望塔的高度 是单峰函 ...
随机推荐
- FFT,NTT 专题
学习傅里叶的基本性质及其代码,可以参考大神理解 还有 ACdream 的博客 贴一下NTT的模板: using namespace std; typedef long long ll; int n; ...
- Stanford coursera Andrew Ng 机器学习课程编程作业(Exercise 1)
Exercise 1:Linear Regression---实现一个线性回归 在本次练习中,需要实现一个单变量的线性回归.假设有一组历史数据<城市人口,开店利润>,现需要预测在哪个城市中 ...
- HDU 5155 Harry And Magic Box --DP
题意:nxm的棋盘,要求每行每列至少放一个棋子的方法数. 解法:首先可以明确是DP,这种行和列的DP很多时候都要一行一行的推过去,即至少枚举此行和前一行. dp[i][j]表示前 i 行有 j 列都有 ...
- AC日记——密码翻译 openjudge 1.7 09
09:密码翻译 总时间限制: 1000ms 内存限制: 65536kB 描述 在情报传递过程中,为了防止情报被截获,往往需要对情报用一定的方式加密,简单的加密算法虽然不足以完全避免情报被破译,但仍 ...
- webapp开发需要注意的浏览器内核知识
Web App:1.开发成本较低使用web开发技术就可以轻松的完成web app的开发2.升级较简单升级不需要通知用户,在服务端更新文件即可,用户完全没有感觉3.维护比较轻松和一般的web一样,维护比 ...
- uwp项目总结
- php中Jpgraph的运用
用Jpgraph,只要了解它的一些内置函数,可以轻松得画出折线图.柱形图.饼状图等图表. 首先要保证PHP打开了Gd2的扩展: 打开PHP.ini,定位到extension=php_gd2.dll,把 ...
- Meet Python: little notes 3 - function
Source: http://www.liaoxuefeng.com/ ♥ Function In python, name of a function could be assigned to a ...
- VS XML注释
1.<c> <c>text</c> 其中: text 希望将其指示为代码的文本. 备注 <c> 标记为您提供了一种将说明中的文本标记为代码的方法.使用 ...
- data:image/png;base64
大家可能注意到了,网页上有些图片的src或css背景图片的url后面跟了一大串字符,比如: data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAkAAAAJ ...