Fork me on GitHub

hdu 4717 The Moving Point 三分水题

题意:给出n个点的初始坐标以及每秒移动方向,让你计算哪个时刻距离最远的点的距离最小

点的距离是单峰函数,那么n(n-1)个单峰函数的max也是个单峰函数,直接套三分

这道题wa了一发,原因是我求距离的时候没有求平方根!!!!啊啊啊

感觉自己写题目都是思路没问题实现一堆问题啊阿西吧呜呜呜 (ノ*-_-*)ノ

The Moving Points

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2353    Accepted Submission(s): 997


Problem Description
There are N points in total. Every point moves in certain direction and certain speed. We want to know at what time that the largest distance between any two points would be minimum. And also, we require you to calculate that minimum distance. We guarantee that no two points will move in exactly same speed and direction.
 

 

Input
The rst line has a number T (T <= 10) , indicating the number of test cases.
For each test case, first line has a single number N (N <= 300), which is the number of points.
For next N lines, each come with four integers Xi, Yi, VXi and VYi (-106 <= Xi, Yi <= 106, -102 <= VXi , VYi <= 102), (Xi, Yi) is the position of the ith point, and (VXi , VYi) is its speed with direction. That is to say, after 1 second, this point will move to (Xi + VXi , Yi + VYi).
 

 

Output
For test case X, output "Case #X: " first, then output two numbers, rounded to 0.01, as the answer of time and distance.
 

上代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<stack>
#include<queue>
#include<vector>
#include<cmath>
#include<algorithm>
#define ll long long
#define eps 1e-6
using namespace std;
struct point{
    int x,y,dx,dy;
    point(){}
    point(int xx,int yy,int ddx,int ddy){x = xx;y = yy;dx = ddx;dy = ddy;}
}poi[310];
int n; 
double dist(int i,int j,double t){
    double cx1 = poi[i].x + poi[i].dx*t;
    double cy1 = poi[i].y + poi[i].dy*t;
    double cx2 = poi[j].x + poi[j].dx*t;
    double cy2 = poi[j].y + poi[j].dy*t;
    return (cx2 - cx1)*(cx2 - cx1) + (cy2 - cy1)*(cy2 - cy1);
}
double cal(double t){
    double max_dis = 0;;
    for(int i = 0;i < n;i++){
        for(int j = i + 1;j < n;j++){
            if(dist(i,j,t) > max_dis)
                max_dis = dist(i,j,t);
        }
    }
    return max_dis;
}
double san_fen(double l0,double r0){
    double l = l0,r = r0,lmid,rmid,v1,v2;
    while(r - l > eps){//浮点数的比较,eps为误差 
        lmid = l + (r-l)/3;
        rmid = r - (r-l)/3;
        //或者  lmid = (2*l + r)/3,rmid = (l + r*2 + 2)/3;
        v1 = cal(lmid);
        v2 = cal(rmid);
        if(v1 > v2) l = lmid;//寻找的是最小值 
        else r = rmid;
    }
    return r;
}

int main(){
    int t0;
    scanf("%d",&t0);
    double t;
    int a,b,c,d;
    for(int tt = 1;tt <= t0;tt++){
        scanf("%d",&n);
        for(int i = 0;i < n;i++){
            scanf("%d%d%d%d",&a,&b,&c,&d);
            poi[i] = point(a,b,c,d);
        }
        t = san_fen(0,1e5);
        printf("Case #%d: %.2f %.2f\n",tt,t,sqrt(cal(t)));
    }
    return 0;
}

 

Sample Input
2 2 0 0 1 0 2 0 -1 0 2 0 0 1 0 2 1 -1 0
 

 

Sample Output
Case #1: 1.00 0.00 Case #2: 1.00 1.00
posted @ 2018-04-12 12:37  梦想飞的菜鸟  阅读(219)  评论(0)    收藏  举报