fzu 2144 Shooting Game(贪心区间覆盖)

编程入门 行业动态 更新时间:2024-10-28 07:34:02

fzu 2144 Shooting Game(贪心<a href=https://www.elefans.com/category/jswz/34/1766384.html style=区间覆盖)"/>

fzu 2144 Shooting Game(贪心区间覆盖)

题目链接:fzu 2144 Shooting Game


题目大意:在立体的空间上,有n只蚊子,给出蚊子的坐标,以及蚊子的移动方向(向量的方式给出), 然后人的攻击范围为半径为r的球体。问说人最多可以打几只蚊子,以及需要花多少时间。


解题思路:对于每只蚊子计算进入攻击范围的时间和离开攻击范围的时间,注意有可能不进入攻击空间,计算区间可以设一个k然后解方程求出区间,然后对求出的区间做区间覆盖问题。


#include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>using namespace std;const int N = 100005;
const double eps = 0;inline bool scan_d(double &ret) {  char c; int sgn;  if(c = getchar(),c == EOF) return 0; //EOF  while(c != '-' && (c < '0' || c > '9')) c = getchar();  sgn = (c == '-') ? -1 : 1;  ret = (c == '-') ? 0 : (c - '0');  while(c=getchar(),c>='0'&&c<='9') ret=ret*10+(c-'0');  ret *= sgn;  return 1;  
} struct point {double x, y, z;void get() { scan_d(x); scan_d(y); scan_d(z); }
};struct state {double x, y;
}s[N];
int n, tmp;
double r;void add(point p, point q) {double a = q.x * q.x + q.y * q.y + q.z * q.z;double b = 2 * (p.x * q.x + p.y * q.y + p.z * q.z);double c = p.x * p.x + p.y * p.y + p.z * p.z - r * r;double t = b * b - 4 * a * c;if (t < 0) return;else if (t == 0) {double i = -b / (2.0 * a);if (i < 0) return;s[tmp].x = s[tmp].y = i;}else {double i = (-b + sqrt(t) ) / (2 * a);double j = (-b - sqrt(t) ) / (2 * a);if (i < 0 && j < 0) return;else if (i < 0) i = 0;else if (j < 0) j = 0;s[tmp].x = min(i, j);s[tmp].y = max(i, j);}tmp++;
}void init() {tmp = 0;point p, q;scanf("%d%lf", &n, &r);for (int i = 0; i < n; i++) {p.get(); q.get();add(p, q);}
}bool cmp(const state& a, const state& b) {if (fabs(a.y - b.y) > eps) return a.y - b.y < eps;return a.x - b.x < eps;
}int solve() {int ans = 0;double c = -1;for (int i = 0; i < tmp; i++) {if (s[i].x - c > eps) {c = s[i].y;ans++;}}return ans;
}int main () {int cas;scanf("%d", &cas);for (int i = 1; i <= cas; i++) {init();sort(s, s + tmp, cmp);printf("Case %d: %d %d\n", i, tmp, solve());}return 0;
}


更多推荐

fzu 2144 Shooting Game(贪心区间覆盖)

本文发布于:2024-02-11 03:25:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1678879.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:区间   贪心   fzu   Game   Shooting

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!