hdu4717 The Moving Points 三分求函数最小值

The Moving Points

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


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
 


将任意两点的距离公式写出来是一个开口向上的抛物线,然后所有的子函数都是一个开口向上的抛物线,他们取max肯定也是先减后增的,然后
就用三分法逐步逼近最低点就行,100次计算就完全可以满足精度要求了


#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

const int maxn = 300 + 10;

int T, N;

struct point {
	int x, y, vx, vy;
}p[maxn];

double MAX(double a, double b) {
	return a > b ? a : b;
}

double calc(double tm) {
	double ans = 0;
	for (int i = 0; i < N - 1; i++) {
		point a = p[i];
		for (int j = i + 1; j < N; j++) {
			point b = p[j];
			double X = (a.x - b.x) + tm * (a.vx - b.vx);
			double Y = (a.y - b.y) + tm * (a.vy - b.vy);
			ans = MAX(ans, X * X + Y * Y);
		}
	}
	return ans;
}

double getTime() {
	double l = 0.0, r = 1e6, mid, mmid;
	for (int i = 1; i <= 100; i++) {
		mid = (l + r) / 2;
		mmid = (mid + r) / 2;
		calc(mid) < calc(mmid) ? r = mmid : l = mid;
	}
	return mid;
}

int main()
{
	scanf("%d", &T);
	for (int t = 1; t <= T; t++) {
		scanf("%d", &N);
		for (int i = 0; i < N; i++) {
			scanf("%d%d%d%d", &p[i].x, &p[i].y, &p[i].vx, &p[i].vy);
		}
		double tm = getTime();
		printf("Case #%d: %.2f %.2f\n", t, tm, sqrt(calc(tm)));
	}
	return 0;
}




你可能感兴趣的:(计算几何,三分,hduoj)