国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

poj 1328 區間,貪心

2019-11-11 07:51:58
字體:
來源:轉載
供稿:網友

        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.
Figure A Sample Input of Radar Installations
InputThe input consists of several test cases. The first line of each case contains two integers n (1<=n<=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 OutputFor 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 21 2-3 12 11 20 20 0Sample Output
Case 1: 2Case 2: 1

給定海島個數、雷達半徑以及各海島坐標,求能覆蓋所有海島的最小雷達數。

貪心策略依然是從左往右,盡量讓每顆雷達覆蓋最大島嶼數。

對于每個點先求出以該點為圓心,d為半徑的圓在x軸上的交點,左右交點就是覆蓋此海島的雷達所在的區間。

對于每個區間,按照區間的右端點排序。

從最左邊開始貪心,對于第一個右端點x0,所有左端點<x0的海島都會與x0共用一個雷達

#include<iostream>#include<cmath>#include<cstring>#include<cstdio>#include<algorithm>#define inf 0x3f3f3f3f#define ll long longusing namespace std;struct node{    int x,y;}q[1010];struct xnode{    double l,r;}xn[1010];bool cmp(xnode a,xnode b){    if(a.r==b.r)        return a.l<b.l;    return a.r<b.r;}bool visited[1010];int main(){    int n,d;    int kcase=1;    while(cin>>n>>d)    {        if(n==0)            return 0;        bool flag=0;        for(int i=1;i<=n;i++)        {            cin>>q[i].x>>q[i].y;            if(q[i].y>d)                flag=1;        }        if(flag==1||d==0)        {            cout<<"Case "<<kcase++<<": "<<-1<<endl;            continue;        }        for(int i=1;i<=n;i++)        {            double len=sqrt(1.0*d*d-q[i].y*q[i].y);            xn[i].l=q[i].x-len;            xn[i].r=q[i].x+len;        }        sort(xn+1,xn+n+1,cmp);        int ans=0;        memset(visited,0,sizeof(visited));        for(int i=1;i<=n;i++)            if(!visited[i])        {            visited[i]=1;            ans++;            for(int j=i+1;j<=n;j++)                if(!visited[j]&&xn[j].l<=xn[i].r)                    visited[j]=1;        }         cout<<"Case "<<kcase++<<": "<<ans<<endl;    }    return 0;}


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 怀远县| 响水县| 北辰区| 泽普县| 韶关市| 祁门县| 铁岭县| 客服| 洛隆县| 兴业县| 锡林郭勒盟| 新乡市| 庆阳市| 鸡东县| 永吉县| 泗阳县| 石柱| 友谊县| 右玉县| 岑巩县| 琼海市| 湖州市| 安康市| 永清县| 临澧县| 集贤县| 建昌县| 镇赉县| 文水县| 盐城市| 达孜县| 黔南| 咸阳市| 孙吴县| 福海县| 泰安市| 讷河市| 彭山县| 昭苏县| 武威市| 马公市|