Shooting Game FZU - 2144 区间覆盖贪心

Shooting Game

FZU - 2144

Fat brother and Maze are playing a kind of special (hentai) game in the playground. (Maybe it’s the OOXX game which decrypted in the last problem, who knows.) But as they don’t like using repellent while playing this kind of special (hentai) game, they really suffer a lot from the mosquito. So they decide to use antiaircraft gun to shoot the mosquito. You can assume that the playground is a kind of three-dimensional space and there are N mosquitoes in the playground. Each of them is a kind of point in the space which is doing the uniform linear motion. (匀速直线运动) Fat brother is standing at (0, 0, 0) and once he shoot, the mosquito who’s distance from Fat brother is no large than R will be shot down. You can assume that the area which Fat brother shoot is a kind of a sphere with radio R and the mosquito inside this sphere will be shot down. As Fat brother hate these mosquito very much, he wants to shoot as much mosquito as he can. But as we all know, it’s tired for a man to shoot even if he is really enjoying this. So in addition to that, Fat brother wants to shoot as less time as he can.

You can (have to) assume that Fat brother is strong enough and he don’t need to rest after shooting which means that can shoot at ANY TIME.


Input

The first line of the date is an integer T, which is the number of the text cases.

Then T cases follow, each case starts with two integers N and R which describe above.

Then N lines follow, the ith line contains six integers ax, ay, az, dx, dy, dz. It means that at time 0, the ith mosquito is at (ax, ay, az) and it’s moving direction is (dx, dy, dz) which means that after time t this mosquito will be at (ax+dx*t, ay+dy*t, ax+dz*t). You can assume that dx*dx + dy*dy+ dz*dz > 0.

1 <= T <= 50, 1 <= N <= 100000, 1 <= R <= 1000000

-1000000 <= ax, ay, az <= 1000000

-100 <= dx, dy, dz <= 100

The range of each coordinate is [-10086, 10086]

Output

For each case, output the case number first, then output two numbers A and B.

A is the number of mosquito Fat brother can shoot down.

B is the number of times Fat brother need to shoot.

Sample Input
6
2 1
2 0 0 -1 0 0
-2 0 0 1 0 0
2 1
4 0 0 -1 0 0
-2 0 0 1 0 0
2 1
4 0 0 -1 0 0
1 0 0 1 0 0
2 1
1 1 1 1 1 1
-1 -1 -1 -1 -1 -1
1 1
0 0 0 1 0 0
3 1
-1 0 0 1 0 0
-2 0 0 1 0 0
4 0 0 -1 0 0
Sample Output
Case 1: 2 1
Case 2: 2 1
Case 3: 2 2
Case 4: 0 0
Case 5: 1 1
Case 6: 3 2

题意:一个人在空间直角坐标系的原点,即是(0,0,0), 他的射程范围是以R为半径的球体,有n只蚊子,每只蚊子都会有原始坐标和三个坐标方向的速度,人要尽量把多的蚊子射下来,并且开枪次数最少。


思路:假设一只蚊子能进入这个球体,必然有一个进入时间和离开时间,我们设为t,蚊子当前坐标为x = ax + dx * t, y = ay + dy * t, z = az + dz * t, 通过两点距离公式可知

(x - 0)^2 + (y - 0)^2 + (z - 0)^2  <= r,把r移到不等式左边,xyz代入进行化简,就是一条一元二次不等式

(dx^2 + dy^2 + dz^2) * t^2  +  2 * (ax * dx+ay * dy+az * dz) * t + ax^2 + ay^2 + az^2 - r^2 <= 0

接下来就是简单的高中求范围问题了通过判别式b^2 - 4*a *c

同时注意b^2 - 4*a *c>0的时候我们要求的时间t一定是大于0的,如果较大解都小于0了那么这只蚊子就是不符合的,如果仅仅是较小解小于零,让它等于0即可

求出能进入这个球体的每个蚊子的l,r区间后,那么就变成经典的贪心问题,区间选点问题,把每个区间的右端点进行从小到大排序,选定一开始的右端点不断往后判断之后的区间左端点是否小于确定下的右端点,如果小于说明1枪就可以都打死,如果发现左端点大于了选定右端点,就需要再打一枪了,此时更新选定的右端点,打枪次数加一

最坑的就是输入用%lf超时,用%I64d才过mmp

code:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
typedef long long ll;
const int maxn = 100005;
const double eps = 1e-8;
struct node{
    double x,y,z;
    double dx,dy,dz;
}mosq[maxn];//蚊子的初始坐标和移动方位坐标
struct Time{
    double l,r;
}times[maxn];//蚊子在射击范围内的时间段
int n,cnt;
ll r;//射击半径
bool cmp(const Time &a,const Time &b){
    if(fabs(a.r - b.r) > eps)
        return a.r < b.r;
    return a.l > b.l;

}//先按照右边界从小到大排序,如果右边界近似相等按左边界从大到小排序
void cal(node mos,double &a,double &b,double &c){
    a = mos.dx * mos.dx + mos.dy * mos.dy + mos.dz * mos.dz;
    b = 2 * (mos.x * mos.dx + mos.y * mos.dy + mos.z * mos.dz);
    c = mos.x * mos.x + mos.y * mos.y + mos.z * mos.z - r * r;
}
double diat(double a,double b,double c){//判别式
    return b * b - 4 * a * c;
}
void solve(node mos){//solve函数是求解方程的解即蚊子在射程内的时间边界
    double a,b,c;
    cal(mos,a,b,c);//求一元二次方程系数abc
    double del = diat(a,b,c);
    if(del < 0) return; //无解,说明蚊子不会经过射程区域
    else if(del > 0){//判别式大于零两个解
        double x1 = (-1.0 * b - sqrt(del * 1.0)) / (2.0 * a);
        double x2 = (-1.0 * b + sqrt(del * 1.0)) / (2.0 * a);
        if(x2 < 0) return;//时间必须大于0;
        if(x1 < 0)
            times[cnt].l = 0.0;
        else
            times[cnt].l = x1;
        times[cnt++].r = x2;
    }
    else{//一个解
        double x = -1.0 * b / (2.0 * a);
        if(x < 0) return;
        times[cnt].l = x;
        times[cnt++].r = x;
    }
}
int main(){
    int t,cas = 0;
    scanf("%d",&t);
    while(t--){
        scanf("%d%I64d",&n,&r);
        ll x,y,z,dx,dy,dz;
        for(int i = 0; i < n; i++){
            scanf("%I64d%I64d%I64d%I64d%I64d%I64d",&x,&y,&z,&dx,&dy,&dz);
            mosq[i].x = x * 1.0;
            mosq[i].y = y * 1.0;
            mosq[i].z = z * 1.0;
            mosq[i].dx = dx * 1.0;
            mosq[i].dy = dy * 1.0;
            mosq[i].dz = dz * 1.0;
        }
        cnt = 0;//能进入区域的蚊子数
        for(int i = 0; i < n; i++){
            solve(mosq[i]);
        }
        sort(times,times+cnt,cmp);
        int ans = 1;
        double bound = times[0].r;
        for(int i = 1; i < cnt; i++){
            if(times[i].l > bound){
                ans++;
                bound = times[i].r;
            }
        }
        if(cnt == 0)
            printf("Case %d: 0 0\n",++cas);
        else
            printf("Case %d: %d %d\n",++cas,cnt,ans);
    }
    return 0;
}

阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页