HDU 4717 The Moving Points (三分搜索)

The Moving Points

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


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 X i, Y i, VX i and VY i (-10 6 <= X i, Y i <= 10 6, -10 2 <= VX i , VY i <= 10 2), (X i, Y i) is the position of the i th point, and (VX i , VY i) is its speed with direction. That is to say, after 1 second, this point will move to (X i + VX i , Y i + VY i).
 

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

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
 


题意要求minimize the maximum distance between every two points.

由于关于距离的函数是一个二次函数,是一个单峰函数,n个单峰函数的max也是单峰函数

那么本题就可以使用三分搜索来求解了


AC代码如下:

//
//  Created by TaoSama on 2015-05-07
//  Copyright (c) 2015 TaoSama. All rights reserved.
//
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <set>
#include <vector>

using namespace std;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const int N = 1e5 + 10;

int n;
double x[305], y[305], vx[305], vy[305];

double getdis(int i, int j, double t) {
    return ((x[i] + t * vx[i]) - (x[j] + t * vx[j])) *
           ((x[i] + t * vx[i]) - (x[j] + t * vx[j])) +
           ((y[i] + t * vy[i]) - (y[j] + t * vy[j])) *
           ((y[i] + t * vy[i]) - (y[j] + t * vy[j]));
}

double cal(double t) {
    double Max = 0;
    for(int i = 1; i <= n; ++i) {
        for(int j = i + 1; j <= n; ++j) {
            Max = max(Max, getdis(i, j, t));
        }
    }
    return Max;
}

int main() {
#ifdef LOCAL
    freopen("in.txt", "r", stdin);
//  freopen("out.txt","w",stdout);
#endif
    ios_base::sync_with_stdio(0);

    int t; scanf("%d", &t);
    int kase = 0;
    while(t--) {
        scanf("%d", &n);
        for(int i = 1; i <= n; ++i) {
            scanf("%lf%lf%lf%lf",
                  x + i, y + i, vx + i, vy + i);
        }

        double l = 0, r = 1e10;
        for(int i = 1; i <= 100; ++i) {
            double ll = l + (r - l) / 3, rr =  r - (r - l) / 3;
            //cout << ll << ' ' << cal(ll) << endl;
            //cout << rr << ' ' << cal(rr) << endl;

            if(cal(ll) < cal(rr)) r = rr;
            else l = ll;
        }
        printf("Case #%d: %.2f %.2f\n", ++kase, l, sqrt(cal(l)));
    }
    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值