uvalive 4992 Jungle Outpost
题意:一个凸边型,目标在凸边型内且最优。问最多删除几个点使目标暴露在新凸边型外面。
思路:二分+半平面相交。
#include<cstdio>
#include<cmath>
#include<vector>
#include<algorithm>
using namespace std; struct Point
{
double x, y;
Point(double x=, double y=):x(x),y(y) { }
}; typedef Point Vector; Vector operator + (const Vector& A, const Vector& B)
{
return Vector(A.x+B.x, A.y+B.y);
}
Vector operator - (const Point& A, const Point& B)
{
return Vector(A.x-B.x, A.y-B.y);
}
Vector operator * (const Vector& A, double p)
{
return Vector(A.x*p, A.y*p);
}
double Dot(const Vector& A, const Vector& B)
{
return A.x*B.x + A.y*B.y;
}
double Cross(const Vector& A, const Vector& B)
{
return A.x*B.y - A.y*B.x;
}
double Length(const Vector& A)
{
return sqrt(Dot(A, A));
}
Vector Normal(const Vector& A)
{
double L = Length(A);
return Vector(-A.y/L, A.x/L);
} double PolygonArea(vector<Point> p)
{
int n = p.size();
double area = ;
for(int i = ; i < n-; i++)
area += Cross(p[i]-p[], p[i+]-p[]);
return area/;
} // 有向直线。它的左边就是对应的半平面
struct Line
{
Point P; // 直线上任意一点
Vector v; // 方向向量
double ang; // 极角,即从x正半轴旋转到向量v所需要的角(弧度)
Line() {}
Line(Point P, Vector v):P(P),v(v)
{
ang = atan2(v.y, v.x);
}
bool operator < (const Line& L) const
{
return ang < L.ang;
}
}; // 点p在有向直线L的左边(线上不算)
bool OnLeft(const Line& L, const Point& p)
{
return Cross(L.v, p-L.P) > ;
} // 二直线交点,假定交点惟一存在
Point GetLineIntersection(const Line& a, const Line& b)
{
Vector u = a.P-b.P;
double t = Cross(b.v, u) / Cross(a.v, b.v);
return a.P+a.v*t;
} const double eps = 1e-; // 半平面交主过程
vector<Point> HalfplaneIntersection(vector<Line>& L)
{
int n = L.size();
sort(L.begin(), L.end()); // 按极角排序 int first, last; // 双端队列的第一个元素和最后一个元素的下标
vector<Point> p(n); // p[i]为q[i]和q[i+1]的交点
vector<Line> q(n); // 双端队列
vector<Point> ans; // 结果 q[first=last=] = L[]; // 双端队列初始化为只有一个半平面L[0]
for(int i = ; i < n; i++)
{
while(first < last && !OnLeft(L[i], p[last-])) last--;
while(first < last && !OnLeft(L[i], p[first])) first++;
q[++last] = L[i];
if(fabs(Cross(q[last].v, q[last-].v)) < eps) // 两向量平行且同向,取内侧的一个
{
last--;
if(OnLeft(q[last], L[i].P)) q[last] = L[i];
}
if(first < last) p[last-] = GetLineIntersection(q[last-], q[last]);
}
while(first < last && !OnLeft(q[first], p[last-])) last--; // 删除无用平面
if(last - first <= ) return ans; // 空集
p[last] = GetLineIntersection(q[last], q[first]); // 计算首尾两个半平面的交点 // 从deque复制到输出中
for(int i = first; i <= last; i++) ans.push_back(p[i]);
return ans;
} const int maxn = + ;
int n;
Point P[maxn]; // 连续m个点是否可以保证炸到总部
bool check(int m)
{
vector<Line> lines;
for(int i = ; i < n; i++)
lines.push_back(Line(P[(i+m+)%n], P[i]-P[(i+m+)%n]));//相当于从起点0到((i+m+1)%n)这两点延顺时针方向之间的点被删除。应为向量指向起点0,只有向量左边的点符合要求,右边自然被排除。
return HalfplaneIntersection(lines).empty();
} int solve()
{
if(n == ) return ;
int L = , R = n-, M; // 炸n-3个点一定可以摧毁
while(L < R)
{
M = L + (R-L)/;
if(check(M)) R = M;
else L = M+;
}
return L;
} int main()
{
while(scanf("%d", &n) == && n)
{
for(int i = ; i < n; i++)
{
int x, y;
scanf("%d%d", &x, &y);
P[i] = Point(x, y);
}
printf("%d\n", solve());
}
return ;
}
uvalive 4992 Jungle Outpost的更多相关文章
- UVALive 4992 Jungle Outpost(半平面交判存)
Jungle Outpost Time limit: 15.000 seconds Description There is a military base lost deep in the jung ...
- UVALive 4992 Jungle Outpost(半平面交)
题意:给你n个塔(点)形成一个顺时针的凸包,敌人可以摧毁任何塔,摧毁后剩下的塔再组成凸包 在开始的凸包内选一点为主塔,保证敌人摧毁尽量多塔时主塔都还在现在的凸包内,求出最多摧毁的塔 题解:这题关键就是 ...
- LA 4992 Jungle Outpost(半平面交)
Jungle Outpost [题目链接]Jungle Outpost [题目类型]半平面交 &题解: 蓝书282 我自己写的代码居然AC了!!! 刘汝佳的说要right要-3什么的,还要特判 ...
- bzoj千题计划210:bzoj2642 | Poj3968 | UVALive 4992| hdu 3761 Jungle Outpost
http://www.lydsy.com/JudgeOnline/problem.php?id=2642 题意: n个瞭望台,形成一个凸n边形.这些瞭望台的保护范围是这个凸包内的任意点. 敌人进攻时, ...
- 【二分】【半平面交】Gym - 101309J - Jungle Outpost
发现炸毁的瞭望塔必然是连续的,其余下的部分是一个半平面. 二分答案,枚举所有可能的炸毁情况,做个半平面交,如果交出来面积是0,就可以保证不存在安全区域. #include<cstdio> ...
- UVa 1475 (二分+半平面交) Jungle Outpost
题意: 有n个瞭望塔构成一个凸n边形,敌人会炸毁一些瞭望台,剩下的瞭望台构成新的凸包.在凸多边形内部选择一个点作为总部,使得敌人需要炸毁的瞭望塔最多才能使总部暴露出来.输出敌人需要炸毁的数目. 分析: ...
- uva 1475 - Jungle Outpost
半平面交,二分: 注意,题目的点是顺时针给出的: #include<cstdio> #include<algorithm> #include<cmath> #def ...
- UVaLive4992:Jungle Outpost
传送门 半平面交. 首先,由显然成立法可以证明炸连续的几个总比分散火力效果更佳. 所以二分答案,转化为判定问题,即间隔$ans$个点的连线的半平面交是否为空. 半平面交判定即可. 时间复杂度:$O(N ...
- [GodLove]Wine93 Tarining Round #10
比赛链接: http://www.bnuoj.com/v3/contest_show.php?cid=4159 题目来源: lrj训练指南---几何算法 Flag ID Title A Board ...
随机推荐
- 站长、运维必备| 网站可用性监控产品 OneAPM Cloud Test 上线
白天太忙,到了晚上才发现网站一天都没有访问量? 直到有用户投诉才发现网站完全无法访问? 还要每月付费才能及时了解网站可用情况? 监控频率太低,不能及时发现网站不可用? 第三方服务宕机,导致您的网站不可 ...
- mysql deadlock处理
1.SHOW ENGINE INNODB STATUS 得到最后一次死锁发生的状况 =====================================140110 11:43:07 INNOD ...
- linux后台执行命令&
当在前台运行某个作业时,终端被该作业占据:而在后台运行作业时,它不会占据终端.可以使用&命令把作业放到后台执行. 如:30 2 * * * /data/app/scripts/hotbacku ...
- __init和__exit宏的作用
原文地址:http://blog.csdn.net/zhenwenxian/article/details/8564574 内核的部分函数带有__init和__exit宏,负责“初始化”和“清理收尾” ...
- Hibernate逍遥游记-第2章-使用hibernate.properties
1. package mypack; import org.hibernate.*; import org.hibernate.cfg.Configuration; import java.util. ...
- HeadFirst设计模式之组合模式
一. 1.The Composite Pattern allows us to build structures of objects in the form of trees that contai ...
- 各种不会。。。。编译和安装hadoop过程中好多命令和工具不会
http://blog.csdn.net/bamuta/article/details/13506893 64位下解决方法(重新编译~~) http://zhidao.baidu.com/link? ...
- Vim的撤销与重做
命令模式下 u:撤销 Ctrl+r:重做(撤销撤销)
- Descending Order
Descending Order Description: Your task is to make a function that can take any non-negative integer ...
- Eclipse插件安装
在线安装(一定要保证网络畅通) 更新插件: Eclipse中,Help->Install New Software...从Work with下拉列表框中选择,通过该列表框可以选择Eclipse已 ...