24h購物| | PChome| 登入
2012-08-25 17:04:03| 人氣2,460| 回應0 | 上一篇 | 下一篇

[UVA][Greedy] 10382 - Watering Grass

推薦 0 收藏 0 轉貼0 訂閱站台

Problem E
Watering Grass

Input:
standard input
Output:
standard output
Time Limit:
3 seconds

n sprinklers are installed in a horizontal strip of grass l meters long and w meters wide. Each sprinkler is installed at the horizontal center line of the strip. For each sprinkler we are given its position as the distance from the left end of the center line and its radius of operation.

What is the minimum number of sprinklers to turn on in order to water the entire strip of grass?

Input

Input consists of a number of cases. The first line for each case contains integer numbers n, l and w with n <= 10000. The next n lines contain two integers giving the position of a sprinkler and its radius of operation. (The picture above illustrates the first case from the sample input.)

 

Output

For each test case output the minimum number of sprinklers needed to water the entire strip of grass. If it is impossible to water the entire strip output -1.

Sample input

8 20 2
5 3
4 1
1 2
7 2
10 2
13 3
16 2
19 4

3 10 1
3 5
9 3
6 1

3 10 1
5 3
1 1
9 1

 

Sample Output
6

2

-1


找出每個圓交矩形的左右端點, 之後採用 Greedy 挑圓

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
typedef struct {
double l, r;
} DD;
int cmp(const void *i, const void *j) {
DD *a, *b;
a = (DD *)i, b = (DD *)j;
return a->l < b->l ? -1 : 1;
}
int main() {
int n, l, w;
double x, r;
int i, j;
while(scanf("%d %d %d", &n, &l, &w) == 3) {
int m = 0;
DD D[10000];
for(i = 0; i < n; i++) {
scanf("%lf %lf", &x, &r);
if(r > w/2.0) {
double ll = x - sqrt(r*r - w*w/4.0);
double rr = x + sqrt(r*r - w*w/4.0);
D[m].l = ll, D[m].r = rr;
m++;
}
}
qsort(D, m, sizeof(DD), cmp);
int ch, flag = 0;
double right = 0;
for(i = 0, j = 0; i < m; i++) {
double tmp = right;
ch = -1;
while(j < m && D[j].l <= right) {
if(D[j].r > tmp) {
tmp = D[j].r;
ch = j;
}
j++;
}
if(tmp >= l) {
printf("%d\n", i+1);
flag = 1;
break;
}
if(ch == -1)
break;
right = tmp;
}
if(!flag) puts("-1");
}
return 0;
}

台長: Morris
人氣(2,460) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA][SCC、Tarjan算法] 11770 - Lighting Away
此分類上一篇:[UVA][Greedy] 10249 - The Grand Dinner

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文