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】瞭望塔的更多相关文章

  1. 【BZOJ 1038】[ZJOI2008]瞭望塔

    [题目链接]:http://www.lydsy.com/JudgeOnline/problem.php?id=1038 [题意] [题解] 可以看到所有村子的瞭望塔所在的位置只会是在相邻两个村子所代表 ...

  2. BZOJ 1040 ZJOI 2008 骑士 基环树林+树形DP

    题目大意:有一些骑士.他们每个人都有一个权值.可是因为一些问题,每个骑士都特别讨厌还有一个骑士.所以不能把他们安排在一起.求这些骑士所组成的编队的最大权值和是多少. 思路:首先貌似是有向图的样子,可是 ...

  3. BZOJ 1040 ZJOI 2008 骑士 树形DP

    题意: 有一些战士,他们有战斗力和讨厌的人,选择一些战士,使他们互不讨厌,且战斗力最大,范围1e6 分析: 把战士看作点,讨厌的关系看作一条边,连出来的是一个基环树森林. 对于一棵基环树,我们找出环, ...

  4. bzoj 1034 [ ZJOI 2008 ] 泡泡堂BNB —— 贪心

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1034 一开始想了个很麻烦的贪心做法,对于每个 a[i],找第一个大于它的 b 匹配…… 然后 ...

  5. 【BZOJ】【1038】【ZJOI2008】瞭望塔

    计算几何/半平面交 说是半平面交,实际上只是维护了个下凸壳而已……同1007水平可见直线 对于每条线段,能看到这条线段的点都在这条线段的“上方”,那么对所有n-1条线段求一个可视区域的交,就是求一个半 ...

  6. 【BZOJ 1038】 1038: [ZJOI2008]瞭望塔

    1038: [ZJOI2008]瞭望塔 Description 致力于建设全国示范和谐小村庄的H村村长dadzhi,决定在村中建立一个瞭望塔,以此加强村中的治安.我们将H村抽象为一维的轮廓.如下图所示 ...

  7. BZOJ 1038 瞭望塔

    Description 致力于建设全国示范和谐小村庄的H村村长dadzhi,决定在村中建立一个瞭望塔,以此加强村中的治安.我们将H村抽象为一维的轮廓.如下图所示 我们可以用一条山的上方轮廓折线(x1, ...

  8. 1038: [ZJOI2008]瞭望塔 - BZOJ

    Description 致力于建设全国示范和谐小村庄的H村村长dadzhi,决定在村中建立一个瞭望塔,以此加强村中的治安.我们将H村抽象为一维的轮廓.如下图所示 我们可以用一条山的上方轮廓折线(x1, ...

  9. bzoj 1038 瞭望塔 半平面交+分段函数

    题目大意 给你一座山,山的形状在二维平面上为折线 给出\((x_1,y_1),(x_2,y_2)...(x_n,y_n)\)表示山的边界点或转折点 现在要在\([x_1,x_n]\)(闭区间)中选择一 ...

  10. bzoj千题计划126:bzoj1038: [ZJOI2008]瞭望塔

    http://www.lydsy.com/JudgeOnline/problem.php?id=1038 本题可以使用三分法 将点按横坐标排好序后 对于任意相邻两个点连成的线段,瞭望塔的高度 是单峰函 ...

随机推荐

  1. HDU4003Find Metal Mineral[树形DP 分组背包]

    Find Metal Mineral Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Other ...

  2. Prefab Assist插件

    资料 Prefab文档: http://game.ceeger.com/Manual/Prefabs.html 基础:基于NGUI制作组件的Prefab 前言 在一个游戏的UI中,有一些通用的组件,比 ...

  3. Maven与Ant使用reportNG代替testng

    大家在使用TestNG时一定会发现其本身的报告生成不但简陋而且相当的不美观,而ReportNG正是一款为TestNG量身定做的报告生成插件,其报告美观.简约.清晰.漂亮的特点让很多TestNG开始慢慢 ...

  4. python-day02数据类型-字符串和列表的操作

    while循环: while True: 条件语句....... 关于break和continue,break:跳出当前的循环                               contnu ...

  5. maya 专家模式

    maya中按ctrl + 空格 可以在普通模式与专家模式之间切换,如图 普通模式 按ctrl+空格后进入专家模式 来自为知笔记(Wiz)

  6. sg函数与博弈论2

    参考链接: http://blog.sina.com.cn/s/blog_51cea4040100h3l9.html 这篇主要就是讲anti-sg.multi-sg和every-sg的. 例1 poj ...

  7. 在ESXi 5.x之间冷迁移虚机

    试过几种不同的方法都不成功, 直接用VMware vCenter Converter Standalone Client迁移, 会出现task中的source与job中的source不一致的情况, 使 ...

  8. (原创)mybatis学习二,spring和mybatis的融合

    mybatis学习一夯实基础 上文介绍了mybatis的相关知识,这一节主要来介绍mybaits和spring的融合 一,环境搭建 1,jar包下载,下载路径为jar包 2,将包导入到java工程中 ...

  9. 正在编译转换: 未能找到元数据文件 EntityFramework.dll

    错误 1 正在编译转换: 未能找到元数据文件“C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\Tools\..\IDE\Enti ...

  10. 《程序设计教学法--以Java程序设计为例》

    <程序设计教学法--以Java程序设计为例> 当老师上的第一门课就是<Java程序设计>,工作以来,断断续续上了近十次课了吧.十几年来,教材.课程内容.教学方法.教学手段不断改 ...