Gym - 100781G Goblin Garden Guards (掃描線)
阿新 • • 發佈:2018-07-28
ret += 就是 nbsp sca 殺死 return style node
題意:
n 只哥布林,每只哥布林都有一個位置坐標。
m 個炮臺,每個炮臺都有一個位置坐標和一個攻擊半徑。
如果一個哥布林在任何一個炮臺的攻擊範圍內,都會被殺死。
求最後沒有被殺死的哥布林的數量。
這題暴力加一些小小的優化可以爆過去。。。然後場上並不敢試。
標算是掃描線。炮臺攻擊範圍內的每個橫坐標都拉一個掃描線,把線的兩端的點和哥布林的點一起加進一個數組。
然後排序,就會發現能被殺死的哥布林的點在一根線的兩個端點之間。
直接掃一遍統計答案就可以了。註意存點的數組要開夠。
另外就是。。。排序的時候 y 坐標從小到大排序就 WA, 改成從大到小就A了。。我也不知道為啥。
#include <iostream> #include <cstdio> #include <cstdlib> #include <cmath> #include <algorithm> using namespace std; #define maxn 9000000 + 1000 struct Node { int x, y, type; Node(int xx, int yy, int tt) : x(xx), y(yy), type(tt) {} Node() {} }a[maxn];bool cmp(Node a, Node b) { if (a.x != b.x) return a.x < b.x; if (a.y != b.y) return a.y > b.y; return a.type > b.type; } int getdis(int x, int y) { return round(floor(sqrt(x*x - y*y))); } int tot = 0; int main() { int n; scanf("%d", &n); for(int i = 1; i <= n; i++) { tot++; scanf("%d%d", &a[tot].x, &a[tot].y); a[tot].type = 0; } int m; scanf("%d", &m); for (int i = 1; i <= m; i++) { int x, y, r; scanf("%d%d%d", &x, &y, &r); for (int j = -r; j <= r; j++) { a[++tot] = Node(x+j, y+getdis(r, j), 1); a[++tot] = Node(x+j, y-getdis(r, j), -1); } } sort(a+1, a+1+tot, cmp); int sum = 0, ans = 0; for (int i = 1; i <= tot; i++) { sum += a[i].type; if (a[i].type == 0 && sum != 0) ans++; } printf("%d\n", n - ans); }
Gym - 100781G Goblin Garden Guards (掃描線)