您的位置:首页 > 编程语言 > PHP开发

UVA 1475 - Jungle Outpost(二分 + 半平面交)

2016-05-21 22:19 656 查看
题目链接:点击打开链接

思路:首先,我们要知道一个贪心结论:敌人如果有k个炸弹, 那么他一定是炸连续的k个点, 这样会使得炸的面积最大。 那么我们只要二分炸弹数mid,每隔mid个点重新建立一个平面, 仍然是n个平面, 代表n种可能情况, 那么如果他们的交存在, 那么司令部只要放在这个平面交的面积内就行了。 所以问题迎刃而解, 二分答案, 用半平面交判断答案是否可行。

细节参见代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#include <stack>
#include <bitset>
#include <cstdlib>
#include <cmath>
#include <set>
#include <list>
#include <deque>
#include <map>
#include <queue>
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
using namespace std;
typedef long long ll;
typedef long double ld;
const double eps = 1e-6;
const double PI = acos(-1);
const int mod = 1000000000 + 7;
const int INF = 0x3f3f3f3f;
const int seed = 131;
const ll INF64 = ll(1e18);
const int maxn = 50000 + 10;
int T,n,m;
struct point {
double x, y;
point(double x=0, double y=0):x(x), y(y) {}
};
typedef point Vector;
Vector operator + (Vector A, Vector B) { return Vector(A.x+B.x, A.y+B.y); }
Vector operator - (Vector A, Vector B) { return Vector(A.x-B.x, A.y-B.y); }
Vector operator * (Vector A, double p) { return Vector(A.x*p, A.y*p); }
Vector operator / (Vector A, double p) { return Vector(A.x/p, A.y/p); }
bool operator < (const point& a, const point& b) {
return a.x < b.x || (a.x == b.x && a.y < b.y);
}
int dcmp(double x) {
if(fabs(x) < eps) return 0;
else return x < 0 ? -1 : 1;
}
bool operator == (const point& a, const point& b) {
return dcmp(a.x-b.x) == 0 && dcmp(a.y-b.y) == 0;
}
double cross(Vector A, Vector B) { return A.x*B.y - A.y*B.x; }
double Dot(Vector A, Vector B) { return A.x * B.x + A.y * B.y; }
double Length(Vector A) { return sqrt(Dot(A, A)); }
Vector Normal(Vector A) {
double L = Length(A);
return Vector(-A.y/L, A.x/L);
}
struct Line {
point p;
Vector v;
double ang;
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;
}
};
bool onLeft(Line L, point p) {
return cross(L.v, p-L.p) > 0;
}
point getintersection(Line a, 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;
}
int halfplane(Line* L, int n, point* poly) {
sort(L, L+n);
int first, last;
point *p = new point
;
Line *q = new Line
;
q[first=last=0] = L[0];
for(int i = 1; i < n; i++) {
while(first < last && !onLeft(L[i], p[last-1])) last--;
while(first < last && !onLeft(L[i], p[first])) first++;
q[++last] = L[i];
if(fabs(cross(q[last].v, q[last-1].v)) < eps) {
last--;
if(onLeft(q[last], L[i].p)) q[last] = L[i];
}
if(first < last) p[last-1] = getintersection(q[last-1], q[last]);
}
while(first < last && !onLeft(q[first], p[last-1])) last--;
if(last - first <= 1) return 0;
p[last] = getintersection(q[last], q[first]);
int m = 0;
for(int i = first; i <= last; i++) poly[m++] = p[i];
return m;
}
point p[maxn], poly[maxn];
Line L[maxn];
double x, y;
int main() {
while(~scanf("%d",&n) && n) {
for(int i = 0; i < n; i++) {
scanf("%lf%lf",&x,&y);
p[i] = point(x, y);
}
int l = 1, r = n - 1;
while(r > l) {
int mid = (r + l) / 2;
for(int i = 0; i < n; i++) L[i] = Line(p[i], p[i] - p[(i+mid+1)%n]);
int m = halfplane(L, n, poly);
if(!m) r = mid;
else l = mid+1;
}
printf("%d\n", l);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: