首页 技术 正文
技术 2022年11月15日
0 收藏 432 点赞 3,683 浏览 2540 个字

题意:一个凸边型,目标在凸边型内且最优。问最多删除几个点使目标暴露在新凸边型外面。

思路:二分+半平面相交。

 #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 ;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,030
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,520
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,368
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,148
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,781
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,859