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

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

poj 1328 區間,貪心

2019-11-11 06:46: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;}


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 刚察县| 康平县| 依安县| 洮南市| 壤塘县| 资阳市| 理塘县| 漳平市| 广德县| 镇原县| 赤壁市| 仙居县| 曲周县| 清水县| 钟祥市| 当涂县| 凤翔县| 大田县| 平昌县| 天祝| 肥东县| 肥城市| 平湖市| 阳西县| 长治市| 海伦市| 明光市| 广州市| 北碚区| 南丹县| 永新县| 泽州县| 新津县| 阿拉善右旗| 云浮市| 屏边| 象山县| 邹平县| 南部县| 林芝县| 望都县|