zoukankan      html  css  js  c++  java
  • UVAlive 2519 Radar Installation (区间选点问题)

    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 locating in the sea side. And any radar installation, locating on the coasting, can only cover d distance, so an island in the sea can be covered by a radius installation, if the distance between them is at most d .

    We use Cartesian coordinate system, defining the coasting is the x -axis. The sea side is above x -axis, and the land side below. Given the position of each island in the sea, and given the distance of the coverage of the radar installation, your task is to write a program to find the minimal number of radar installations to cover all the islands. Note that the position of an island is represented by its x -y coordinates.

     

    epsfbox{p2519.eps}

     

    Input 

    The input consists of several test cases. The first line of each case contains two integers n (1$ le$n$ le$1000)and d , where n is the number of islands in the sea and d is the distance of coverage of the radar installation. This is followed by n lines each containing two integers representing the coordinate of the position of each island. Then a blank line follows to separate the cases.

    The input is terminated by a line containing pair of zeros.

     

    Output 

    For each test case output one line consisting of the test case number followed by the minimal number of radar installations needed. `-1' installation means no solution for that case.

     

    Sample Input 

    3 2
    1 2
    -3 1
    2 1
    
    1 2
    0 2
    
    0 0
    

     

    Sample Output 

     

    Case 1: 2
    Case 2: 1

    题意:给定n个岛屿坐标,和雷达半径,雷达只能放在x轴上,求出最少放几个雷达。

    思路:贪心。每个岛屿都有最左和最右最远放雷达能覆盖到的点,我们把这作为左右区间。只要在区间中选中一个位置放雷达。就可以满足该岛屿被覆盖,转换为区间选点问题。

    代码:

    #include <stdio.h>
    #include <string.h>
    #include <math.h>
    #include <algorithm>
    using namespace std;
    
    double dd;
    int n, i, judge, num, ans, j;
    struct D {
    	double x;
    	double y;
    	double l;
    	double r;
    	int v;
    } d[1005];
    
    int cmp(D a, D b) {
    	if (a.r != b.r)
    		return a.r < b.r;
    	return a.l > b.l;
    }
    int main() {
    	int t = 1;
    	while (~scanf("%d%lf", &n, &dd) && n || dd) {
    		judge = 1; num = 0; ans = 0;
    		memset(d, 0, sizeof(d));
    		for (i = 0; i < n; i ++) {
    			scanf("%lf%lf", &d[i].x, &d[i].y);
    			if (d[i].y > dd)
    				judge = 0;
    			d[i].r = sqrt(dd * dd - d[i].y * d[i].y) + d[i].x;
    			d[i].l = d[i].x - sqrt(dd * dd - d[i].y * d[i].y);
    		
    		}
    		sort(d, d + n, cmp);
    		printf("Case %d: ", t ++);
    		if (judge) {
    			while (num < n) {
    				for (i = 0; i < n; i ++) {
    					if (!d[i].v) {
    						double x = d[i].r;						
    						for (j = i; j < n; j ++) {
    							if (d[j].l <= x && !d[j].v) {
    								d[j].v = 1;
    								num ++;
    							}
    						}
    						ans ++;
    						break;
    					}
    				}
    			}
    			printf("%d
    ", ans);
    		}
    		else printf("-1
    ");
    	}
    	return 0;
    }


  • 相关阅读:
    0099 数据类型转换 之 转为布尔:Boolean()
    0098 数据类型转换 之 转为数字: parseInt 、 parseFloat 、Number()、隐式转换
    0097 数据类型转换 之 转为字符串:toString()、String() 、加号拼接、隐式转换
    0096 获取变量数据类型typeof、字面量
    0095 布尔型Boolean,Undefined和 Null
    0094 字符串型 String
    0093 数字型 Number:整数、小数 、数字型进制、数字型范围、数字型三个特殊值、isNaN
    0092 数据类型、简单数据类型概述
    0091 交换两个变量的值( 实现思路:使用一个 临时变量 用来做中间存储 )
    SCSS 常用属性合集
  • 原文地址:https://www.cnblogs.com/riskyer/p/3279924.html
Copyright © 2011-2022 走看看