UVa1615 Highway (贪心,区间选点)

链接:http://bak.vjudge.net/problem/UVA-1615

分析:以村庄为圆心,D为半径作圆,可选区间就是高速公路在圆内的部分(包括两个交点),按区间右端点从小到大,如若右端点相同再按左端点从大到小排好序,接下来就是很纯粹的区间选点问题。

 1 #include <cstdio>
 2 #include <algorithm>
 3 #include <cmath>
 4 using namespace std;
 5
 6 const int maxn = 1e5+5;
 7
 8 struct Interval {
 9     int l, r;
10     bool operator < (const Interval& rhs) const {
11         return r < rhs.r || (r == rhs.r && l > rhs.l);
12     }
13 } itv[maxn];
14
15 int L, D, N;
16
17 int main() {
18     while (scanf("%d%d%d", &L, &D, &N) == 3) {
19         double dis = D;
20         for (int i = 0; i < N; i++) {
21             double a, b; scanf("%lf%lf", &a, &b);
22             double d = sqrt(dis*dis - b*b);
23             itv[i].l = a - d; itv[i].r = a + d;
24         }
25         sort(itv, itv + N);
26         int ans = 0;
27         double curPos = -1e9;
28         for (int i = 0; i < N; i++)
29         if (curPos < itv[i].l) {
30             curPos = itv[i].r < L ? itv[i].r : L;
31             ans++;
32         }
33         printf("%d\n", ans);
34     }
35     return 0;
36 }
时间: 2024-08-06 07:57:37

UVa1615 Highway (贪心,区间选点)的相关文章

poj1328Radar Installation(贪心—区间选点)

题目链接: 啊哈哈,点我点我 题目: Radar Installation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 52037   Accepted: 11682 Description Assume the coasting is an infinite straight line. Land is in one side of coasting, sea in the other. Each small isl

POJ 1328 Radar Installation 【贪心 区间选点】

解题思路:给出n个岛屿,n个岛屿的坐标分别为(a1,b1),(a2,b2)-----(an,bn),雷达的覆盖半径为r 求所有的岛屿都被覆盖所需要的最少的雷达数目. 首先将岛屿坐标进行处理,因为雷达的位置在x轴上,所以我们设雷达的坐标为(x,0),对于任意一个岛屿p(a,b),因为岛屿要满足在雷达的覆盖范围内,所以 (x-a)^2+b^2=r^2,解得 xmin=a-sqrt(r*r-b*b);//即为区间的左端点 xmax=a+sqrt(r*r-b*b);//即为区间的右端点 接下来区间选点即

[ACM] POJ 3485 Highway (区间选点问题)

Highway Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 871   Accepted: 402 Description Bob is a skilled engineer. He must design a highway that crosses a region with few villages. Since this region is quite unpopulated, he wants to mini

【uva 1615】Highway(算法效率--贪心 区间选点问题)

题意:给定平面上N个点和一个值D,要求在x轴上选出尽量少的点,使得对于给定的每个店,都有一个选出的点离它的欧几里德距离不超过D. 解法:先把问题转换成模型,把对平面的点满足条件的点在x轴的直线上可得到一个个区间,这样就是选最小的点覆盖所有的区间的问题了.我之前的一篇博文有较详细的解释:关于贪心算法的经典问题(算法效率 or 动态规划).代码实现我先空着.挖坑~

UVA - 1615 Highway(高速公路)(贪心+区间选点)

题意:给定平面上n(n<=105)个点和一个值D,要求在x轴上选出尽量少的点,使得对于给定的每个点,都有一个选出的点离它的欧几里德距离不超过D. 分析: 1.根据D可以算出每个点在x轴上的可选区域,计算出区域的左右端点. 2.贪心选点,每次都选这个区域的最右端点,这样此端点可存在于尽可能多的区域. #pragma comment(linker, "/STACK:102400000, 102400000") #include<cstdio> #include<cs

poj 1328 Radar Installation【贪心区间选点】

Radar Installation Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 20000/10000K (Java/Other) Total Submission(s) : 22   Accepted Submission(s) : 9 Problem Description Assume the coasting is an infinite straight line. Land is in one side of coa

POJ1328 Radar Installation 【贪心&#183;区间选点】

Radar Installation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 54593   Accepted: 12292 Description Assume the coasting is an infinite straight line. Land is in one side of coasting, sea in the other. Each small island is a point loca

nyoj 710 外星人的供给站【贪心区间选点】

外星人的供给站 时间限制:1000 ms  |  内存限制:65535 KB 难度:3 描述 外星人指的是地球以外的智慧生命.外星人长的是不是与地球上的人一样并不重要,但起码应该符合我们目前对生命基本形式的认识.比如,我们所知的任何生命都离不开液态水,并且都是基于化学元素碳(C)的有机分子组合成的复杂有机体. 42岁的天文学家Dr. Kong已经执著地观测ZDM-777星球十多年了,这个被称为“战神”的红色星球让他如此着迷.在过去的十多年中,他经常有一些令人激动的发现.ZDM-777星球表面有着

POJ1328 Radar Installation 【贪心&amp;#183;区间选点】

Radar Installation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 54593   Accepted: 12292 Description Assume the coasting is an infinite straight line. Land is in one side of coasting, sea in the other. Each small island is a point loca