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

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

poj 1328 區(qū)間,貪心

2019-11-11 07:24:48
字體:
供稿:網(wǎng)友

        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

給定海島個數(shù)、雷達(dá)半徑以及各海島坐標(biāo),求能覆蓋所有海島的最小雷達(dá)數(shù)。

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

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

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

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

#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;}


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 新津县| 普兰县| 长武县| 巴中市| 垣曲县| 望城县| 定州市| 博客| 伊宁县| 卓尼县| 临沂市| 凤台县| 衡东县| 丹巴县| 瑞金市| 丰城市| 凤城市| 原阳县| 嘉祥县| 新闻| 夏邑县| 七台河市| 龙山县| 方山县| 瑞丽市| 开封市| 句容市| 临夏县| 巧家县| 祁连县| 寿阳县| 永嘉县| 安溪县| 东乌珠穆沁旗| 高尔夫| 隆安县| 昌图县| 资阳市| 天峨县| 澄城县| 遵义市|