Lucky Light
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 272 Accepted Submission(s): 74
Problem Description
We have a (point) light source at position (xL, yL) with yL > 0, and a finite series of line segments, all of finite non-zero length, given by the coordinates of their two endpoints. These endpoints are all different. The line segments
are all situated above the x-axis (y > 0).
The segments cast their shadows onto the x-axis. We assume that the shadows of two segments either do not overlap at all, or have an overlap that has some non-zero width (they do not just touch). We also assume that for each segment its shadow is more than
just one point, i.e., there is no segment that is directed toward the light source. The height of the light source (yL) is at least 1 unit larger than the y-coordinates of the endpoints of the line segments. This guarantees that indeed each line segment has
a bounded shadow on the x-axis.
The collection of shadows divides the x-axis into dark and lighted areas (intervals). The problem is to determine the number of lighted areas — which is at least 2 (if there is at least one line segment, otherwise it is 1).
In the picture below the three line segments A, B and C cause three lighted areas, as indicated.
Input
The first line of the input file contains a single number: the number of test cases to follow. Each test case has the following format:
One line with one integer n with 0 ≤ n ≤ 100: the number of line segments.
One line with two integers xL and yL, the coordinates of the light source, separated by a single space. The coordinates satisfy -100 ≤ xL ≤ 100 and 1 ≤ yL ≤ 1,000.
n lines, each containing four integers xi, yi, ui and vi, separated by single spaces, that specify x- and y-coordinates of the two endpoints (xi, yi) and (ui, vi) of the ith line segment, where -100 ≤ xi, ui ≤ 100 and 0 < yi, vi < yL, for 1 ≤ i ≤ n.
Output
For every test case in the input file, the output should contain a single number, on a single line: the number of lighted areas.
Sample Input
2
3
50 60
55 45 30 35
64 39 92 18
20 30 40 16
2
-10 50
-10 1 10 11
-10 11 10 1
Sample Output
莫名其妙地WA,没找到原因,重敲了遍,又莫名其妙地AC,一头雾水。。磊哥教的区间选点方法真心棒。
#include <stdio.h>
#include <algorithm>
using std::sort;
using std::swap;
double X, Y;
struct Node{
double left, right;
} arr[102];
double getX(double a, double b){
return X - Y * (X - a) / (Y - b);
}
bool cmp(Node a, Node b){
return a.left < b.left;
}
int main(){
double x1, y1, x2, y2, flag;
int t, n, ans;
scanf("%d", &t);
while(t--){
scanf("%d", &n);
scanf("%lf%lf", &X, &Y);
for(int i = 0; i < n; ++i){
scanf("%lf%lf%lf%lf", &x1, &y1, &x2, &y2);
arr[i].left = getX(x1, y1);
arr[i].right = getX(x2, y2);
if(arr[i].left > arr[i].right) swap(arr[i].left, arr[i].right);
}
sort(arr, arr + n, cmp);
ans = 1;
flag = arr[0].right;
for(int i = 1; i < n; ++i){
if(flag < arr[i].left){
++ans;
flag = arr[i].right;
}else if(flag < arr[i].right) flag = arr[i].right;
}
if(!n) printf("1\n");
else printf("%d\n", ans + 1);
}
return 0;
}
HDU1687 Lucky Light 【贪心】,布布扣,bubuko.com
HDU1687 Lucky Light 【贪心】
原文:http://blog.csdn.net/chang_mu/article/details/26094047