HDU1687 Lucky Light 【贪心】

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

3
2

莫名其妙地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 【贪心】

时间: 2024-07-28 20:58:20

HDU1687 Lucky Light 【贪心】的相关文章

【HDU 1687】Lucky Light(思维+计算几何)

传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1687 题意:有一个光源,下面有很多放置的板子,问光线被板子分割之后在地上能产生多少块亮区(看题中的图就能看懂). 分析:这个题的做法和省选第一天的C题很像,由于是求在地面上,也就是一条直线上的亮区,我们可以求出地面上被木板遮挡形成的暗区的左坐标和右坐标,然后合并区间就可以了.而求地面上的坐标,可以用相似三角形,若光源为(sx,sy),点为(x1,y1)和(x2,y2),则地面上的坐标为:sx-(sx-

Codeforces Beta Round 84 (Div. 2 Only)

layout: post title: Codeforces Beta Round 84 (Div. 2 Only) author: "luowentaoaa" catalog: true tags: mathjax: true - codeforces 传送门 不得不说这远古场太简单了 A - Nearly Lucky Number (签到) 题意 给出一个数字 求其中7和4的数目是否是7和4组成 #include<bits/stdc++.h> using namespa

CodeForces 1000B Light It Up(贪心、思维)

https://codeforces.com/problemset/problem/1000/B 题意: 一个模拟思维题.就是有一盏灯,0时刻开着.n次操作,你可以在其中加入一次操作(或者不加),操作为:a[i]时刻按一下开关,状态变为相反状态(开->关or关->开).问灯亮着的时长最长为多少? 样例一0~4开(时长为4-0),4~6关(时长为6-4),6~7开(时长为7-6),7~10关(时长为10-7),可以这这些时间点中加操作使开灯时间变长 在3处加操作,开灯时长变为0-3(开)3-4(

light oj 1219 树上贪心

1 #include <iostream> 2 #include <cstdlib> 3 #include <cstring> 4 #include <queue> 5 #include <cstdio> 6 #include <algorithm> 7 #include <map> 8 //#include <time.h> 9 //#include <ext/pb_ds/assoc_container

hdu-5676 ztr loves lucky numbers(乱搞题)

题目链接: ztr loves lucky numbers Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) 问题描述 ztr喜欢幸运数字,他对于幸运数字有两个要求 1:十进制表示法下只包含4.7 2:十进制表示法下4和7的数量相等 比如47,474477就是 而4,744,467则不是 现在ztr想知道最小的但不小于n的幸运数字是多少 输入描述 有TT(1≤T≤10?5??)组数据,

Most financial institutions 贪心 H

H - 贪心 Crawling in process... Crawling failed Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Description Most financial institutions had become insolvent during financial crisis and went bankrupt or were bought b

poj 3404&amp;&amp;poj1700(贪心)

Bridge over a rough river Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4143   Accepted: 1703 Description A group of N travelers (1 ≤ N ≤ 50) has approached an old and shabby bridge and wishes to cross the river as soon as possible. Ho

UVA1614(贪心)

Hell on the Markets Time Limit:3000MS   Memory Limit:Unknown   64bit IO Format:%lld & %llu [Submit]  [Go Back]  [Status] Description Most financial institutions had become insolvent during financial crisis and went bankrupt or were bought by larger i

B. Light It Up 思维题

Recently, you bought a brand new smart lamp with programming features. At first, you set up a schedule to the lamp. Every day it will turn power on at moment 00 and turn power off at moment MM. Moreover, the lamp allows you to set a program of switch