UVA 1421 UVAlive 4253 - Archery(二分)

Korea's reputation in archery is well known because the Korean archery teams have been sweeping almost all gold, silver, and bronze medals in the Olympic Games.

An archery game ICPC supported by NEXON (one of Korea's leading publishers of online contents) will be held in Korea. As a ceremonial event of the game, a famous master of archery will shoot an arrow to hit through all target boards made of paper. Because an arrow flies along a straight line, it depends on his position of the archer line whether or not he hits all targets.

The figure below shows an example of the complete view of a game field from the sky. Every target is represented by a line segment parallel to the archer line. Imagine the coordinate system of which the origin is the leftmost point of the archer line and the archer line is located on the positive x -axis.

In the above figure, the master can hit all targets in position B. However, he never hits all targets in position A because any ray from A intersects at most 3 targets.

Given the width of the archer line and the target locations, write a program for determining if there exists a position at which the master can hit all targets. You may assume that the y -coordinates of all targets are different. Note that if an arrow passes through an end point of a target, it is considered to hit that target.

Input 

Your program is to read from standard input. The input consists of T test cases. The number of test cases T(1T30) is given in the first line of the input. Each test case starts with a line containing an integerW (2W10, 000, 000) , the width of an archer line. The next line contains an integer N (2N5, 000) , the number of target boards. The i -th line of the following N lines contains three integers Di , Li , Ri (1DiW, 0Li < RiW) , where 1iN , Di represents the y -coordinate of the i -th target, and Li andRi represent the x -coordinates of the leftmost point and the rightmost point of the target, respectively. Note that Di  Dj if i  j .

Output 

Your program is to write to standard output. Print exactly one line for each test case. Print ``YES" if there exists a position on the archer line at which a master of archery can hit all targets, otherwise, ``NO".

The following shows sample input and output for three test cases.

Sample Input 

3 
15 
4 
10 2 7 
7 5 12 
2 7 12 
4 9 13 
6 
3 
2 1 3 
4 0 2 
5 4 6 
10 
4 
8 2 5 
4 2 5 
6 5 8 
2 5 8

Sample Output 

YES 
NO 
YES

这题在UVA上没人过,我也过不了,在UVAlive上就能过。不知道是不是UVA的问题

题意:一些靶子。人可以在0-w的点射箭,问能否一箭射穿所有靶子。

思路:二分人站的位置,然后去判断,判断过程中计算每个靶子需要的角度[L,R],维护L,R区间,先进行按y排序,这样一来,如果一个靶子在LR左边,那么人肯定向左移动,反之人要向右移动。如果找到一个位置合适就return true。

代码:

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>
using namespace std;
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
const int N = 5005;
const double esp = 1e-6;
int t, n;
double w;
struct Seg {
	double d, l, r;
} s[N];

bool cmp(Seg a, Seg b) {
	return a.d < b.d;
}

int judge(double x) {
	double L = atan2(s[0].d, s[0].r - x);
	double R = atan2(s[0].d, s[0].l - x);
	for (int i = 1; i < n; i++) {
		double l = atan2(s[i].d, s[i].r - x);
		double r = atan2(s[i].d, s[i].l - x);
		if (r - L < -esp)
			return -1;
		if (l - R > esp)
			return 1;
		L = max(L, l);
		R = min(R, r);
	}
	return 0;
}

bool solve() {
	sort(s, s + n, cmp);
	double l = 0, r = w;
	while (r - l > esp) {
		double mid = (l + r) / 2;
		int flag = judge(mid);
		if (flag == 0) return true;
		else if (flag == -1) r = mid;
		else l = mid;
	}
	return false;
}

int main() {
	scanf("%d", &t);
	while (t--) {
		scanf("%lf%d", &w, &n);
		for (int i = 0; i < n; i++)
			scanf("%lf%lf%lf", &s[i].d, &s[i].l, &s[i].r);
		printf("%s\n", solve()?"YES":"NO");
	}
	return 0;
}


你可能感兴趣的:(UVA 1421 UVAlive 4253 - Archery(二分))