• 半平面求交 - 洛谷 - UVA1475 Jungle Outpost


    欢迎关注更多精彩
    关注我,学习常用算法与数据结构,一题多解,降维打击。

    往期相关背景点击前往

    题目大意

    题目链接
    https://www.luogu.com.cn/problem/UVA1475

    在丛林里有n个瞭望塔,瞭望塔的保护范围就是一个凸n多边形,敌人进攻会炸毁一些瞭望塔,使总部暴露在剩下的瞭望塔围成的凸多边形之外,请你选择一个点作为总部,使得敌人要炸毁的瞭望塔尽量多。

    解析

    此题提到最多要多少个,首先想到枚举+验证的方式。

    枚举可以利用二分优化,前提是要证明答案是具体有单调性,既如果炸x个塔不能摧毁防御,那么炸x-1个塔也不能摧毁防御。如果炸x个塔可以摧毁防御,那么炸x+1个塔也可以摧毁防御。

    可以先尝试炸1个的情形。
    在这里插入图片描述

    炸1个那么就是把剩下两个点连边。把所有点都炸1遍,可以看到最后有效防御区域是一个半平面求交凸多边形。上图中表示为黄色区域。

    接下来看炸2个的情况。

    炸两个有2种策略:

    1. 炸连续的两个塔
    2. 炸不连续的两个塔

    先看炸不连续的情形,

    在这里插入图片描述

    如果炸不连续的两个塔,则会退化成炸1个塔情形,并不是最优解。 从这里可以推断出,这个是一单调的问题。

    看炸连续的两个塔,

    在这里插入图片描述
    没有公共区域。

    综上所述,如果连续炸掉x个塔,那么答案是关于x单调的。

    可以通过二分枚举x, 再构建半平面求交验证的方法解决。

    二分枚举点击前往

    代码

    #include
    #include
    #include 
    #include 
    #include 
    #include 
    #include 
    
    
    using namespace std;
    const double EPS = 1e-7;
    
    const int N = 1e5 + 10;
    
    int cmp(double d) {
    	if (abs(d) < EPS)return 0;
    	if (d > 0)return 1;
    	return -1;
    }
    
    class Point {
    public:
    	double x, y;
    	int id;
    
    	Point() {}
    	Point(double a, double b) :x(a), y(b) {}
    	Point(const Point& p) :x(p.x), y(p.y), id(p.id) {}
    
    	void in() {
    		scanf("%lf %lf", &x, &y);
    	}
    	void out() {
    		printf("%f %f\n", x, y);
    	}
    
    	double dis() {
    		return sqrt(x * x + y * y);
    	}
    
    	double dis2() {
    		return x * x + y * y;
    	}
    
    	Point operator -() const {
    		return Point(-x, -y);
    	}
    
    	Point operator -(const Point& p) const {
    		return Point(x - p.x, y - p.y);
    	}
    
    	Point operator +(const Point& p) const {
    		return Point(x + p.x, y + p.y);
    	}
    	Point operator *(double d)const {
    		return Point(x * d, y * d);
    	}
    
    	Point operator /(double d)const {
    		return Point(x / d, y / d);
    	}
    
    
    	void operator -=(Point& p) {
    		x -= p.x;
    		y -= p.y;
    	}
    
    	void operator +=(Point& p) {
    		x += p.x;
    		y += p.y;
    	}
    	void operator *=(double d) {
    		x *= d;
    		y *= d;
    	}
    
    	void operator /=(double d) {
    		this ->operator*= (1 / d);
    	}
    
    	bool operator<(const Point& a) const {
    		return x < a.x || (abs(x - a.x) < EPS && y < a.y);
    	}
    
    	bool operator==(const Point& a) const {
    		return abs(x - a.x) < EPS && abs(y - a.y) < EPS;
    	}
    };
    
    // 向量操作
    
    double cross(const Point& a, const Point& b) {
    	return a.x * b.y - a.y * b.x;
    }
    
    double dot(const Point& a, const Point& b) {
    	return a.x * b.x + a.y * b.y;
    }
    
    
    class Line {
    public:
    	Point front, tail;
    	double ang;
    	Line() {}
    	Line(const Point& a, const Point& b) :front(a), tail(b) {
    		ang = atan2(front.y - tail.y, front.x - tail.x);
    	}
    };
    
    int cmp(const Line& a, const Line& b) {
    	//auto ta = atan2(a.front.y - a.tail.y, a.front.x - a.tail.x);
    	//auto tb = atan2(b.front.y - b.tail.y, b.front.x - b.tail.x);
    
    	return cmp(a.ang - b.ang);
    }
    
    
    // 点在直线哪一边>0 左边,<0边
    int SideJudge(const Line& a, const Point& b) {
    	return cmp(cross(a.front - a.tail, b - a.tail));
    }
    
    
    int LineSort(const Line& a, const Line& b) {
    	int c = cmp(a, b);
    	if (c)return c < 0;
    	return SideJudge(b, a.front) > 0;
    }
    
    /*
    点p 到 p+r 表示线段1
    点q 到 q+s 表示线段2
    线段1 上1点用 p' = p+t*r (0<=t<=1)
    线段2 上1点用 q' = q+u*s (0<=u<=1)
    让两式相等求交点 p+t*r = q+u*s
    两边都叉乘s
    (p+t*r)Xs = (q+u*s)Xs
    pXs + t*rXs = qXs
    t = (q-p)Xs/(rXs)
    同理,
    u = (p-q)Xr/(sXr) -> u = (q-p)Xr/(rXs)
    
    以下分4种情况:
    1. 共线,sXr==0 && (q-p)Xr==0, 计算 (q-p)在r上的投影在r长度上的占比t0,
    计算(q+s-p)在r上的投影在r长度上的占比t1,查看[t0, t1]是否与范围[0,1]有交集。
    如果t0>t1, 则比较[t1, t0]是否与范围[0,1]有交集。
    t0 = (q-p)*r/(r*r)
    t1 = (q+s-p)*r/(r*r) = t0 + s · r / (r · r)
    2. 平行sXr==0 && (q-p)Xr!=0
    3. 0<=u<=1 && 0<=t<=1 有交点
    4. 其他u, t不在0到范围内,没有交点。
    */
    pair<double, double> intersection(const Point& q, const Point& s, const Point& p, const Point& r) {
    	// 计算 (q-p)Xr
    	auto qpr = cross(q - p, r);
    	auto qps = cross(q - p, s);
    
    	auto rXs = cross(r, s);
    	if (cmp(rXs) == 0)return { -1, -1 }; // 平行或共线
    	// 求解t, u
    	// t = (q-p)Xs/(rXs)
    	auto t = qps / rXs;
    
    	// u = (q-p)Xr/(rXs)
    	auto u = qpr / rXs;
    
    	return { u, t };
    }
    
    Point LineCross(const Line& a, const Line& b) {
    	Point dira = a.front - a.tail;
    	Point dirb = b.front - b.tail;
    
    	auto p = intersection(a.tail, dira, b.tail, dirb);
    	return a.tail + dira * p.first;
    }
    
    class HalfPlane {
    public:
    	vector<Line> lines;
    	vector<int> q;
    	vector<Point> t;
    	int len;
    
    	HalfPlane() {
    		lines.resize(N);
    		q.resize(N);
    		t.resize(N);
    	}
    
    	void addLine(const Line& a) {
    		lines[len++]=a;
    	}
    
    	vector<Point> run() {
    		sort(lines.begin(), lines.begin()+len, LineSort);
    
    		int l = -1, r = 0;
    		q[0] = 0;
    		for (int i = 1; i < len; ++i) {
    			if (cmp(lines[i], lines[i - 1]) == 0)continue;
    			while (r - l > 1 && SideJudge(lines[i], t[r]) < 0)r--;
    			while (r - l > 1 && SideJudge(lines[i], t[l + 2]) < 0)l++;
    			q[++r] = i;
    			t[r] = LineCross(lines[q[r]], lines[q[r - 1]]);
    		}
    		while (r - l > 1 && SideJudge(lines[q[l + 1]], t[r]) < 0)r--;
    		t[r + 1] = LineCross(lines[q[l + 1]], lines[q[r]]);
    		r++;
    
    		// 统计交点
    		l++;
    		vector<Point> ans(r - l);
    		for (int i = 0; i < ans.size(); ++i) {
    			ans[i] = t[i + l + 1];
    		}
    
    		return ans;
    	}
    };
    
    Point oiPs[N*2];
    HalfPlane hp;
    
    bool boomJudge(int n, int k) {
    	hp.len = 0;
    	for (int i = 0; i < n; ++i) {
    		hp.addLine(Line(oiPs[i+k+1], oiPs[i]));
    	}
    
    	auto keyPoints = hp.run();
    	double ans = 0;
    	for (int i = 2; i < keyPoints.size(); ++i) {
    		ans += cross(keyPoints[i - 1] - keyPoints[0], keyPoints[i] - keyPoints[0]);
    	}
    
    	return cmp(ans/2) == 0;
    }
    
    void  solve() {
    	int n;
    	
    	while (scanf("%d", &n) ==1 &&n) {
    		int a, b;
    
    		for (int i = n - 1; i >= 0; --i) {
    			scanf("%d%d", &a, &b);
    			oiPs[i] = Point(a, b);
    			oiPs[i + n] = oiPs[i];
    		}
    
    		int l = 1, r = n-2;
    
    		while (l < r) {
    			int mid = (l + r) >> 1;
    			if (boomJudge(n, mid)) {
    				r = mid;
    			}
    			else {
    				l = mid + 1;
    			}
    		}
    
    		printf("%d\n", l);
    	}
    }
    
    
    int main() {
    	solve();
    	return 0;
    
    }
    
    /*
    3
    0 0
    50 50
    60 10
    5
    0 0
    0 10
    10 20
    20 10
    25 0
    
    */
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
    • 247
    • 248
    • 249
    • 250
    • 251
    • 252
    • 253
    • 254
    • 255
    • 256
    • 257
    • 258
    • 259
    • 260
    • 261
    • 262
    • 263
    • 264
    • 265
    • 266
    • 267
    • 268
    • 269
    • 270
    • 271
    • 272
    • 273
    • 274
    • 275
    • 276
    • 277
    • 278
    • 279
    • 280
    • 281
    • 282
    • 283
    • 284
    • 285
    • 286
    • 287
    • 288
    • 289
    • 290

    本人码农,希望通过自己的分享,让大家更容易学懂计算机知识。创作不易,帮忙点击公众号的链接。

  • 相关阅读:
    java计算机毕业设计教师招聘考试题库系统源码+mysql数据库+系统+lw文档+部署
    Kubeadm 部署 k8s 集群
    奇安信 测试|测试开发 面试真题|面经 汇总
    如何用 CMake 生成 C++ 库(支持 find_package 机制)
    通过代理模式 + 责任链模式实现对目标执行方法拦截和增强功能
    计算几何+2sat:1020T3
    Lazada、速卖通、亚马逊等跨境平台自养买家号测评的用处及解析
    E. Round Dance
    电脑系统修复,操作简单
    Bert不完全手册6. Bert在中文领域的尝试 Bert-WWM & MacBert & ChineseBert
  • 原文地址:https://blog.csdn.net/chenbb1989/article/details/134465311