麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

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

poj 1328 區間,貪心

2019-11-14 09:17:44
字體:
來源:轉載
供稿:網友

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


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 性视频久久 | 91成人免费网站 | 万圣街在线观看免费完整版 | 92看片淫黄大片欧美看国产片 | 久久影院午夜 | 日本中文字幕久久 | 欧美精品一区二区久久 | 国产日韩中文字幕 | 日本一区二区视频在线观看 | 国产免费一区二区三区视频 | 中文字幕极速在线观看 | 毛片大全在线观看 | 欧美三日本三级少妇三级99观看视频 | 久久丝袜脚交足黄网站免费 | 亚洲午夜天堂吃瓜在线 | 久久中文一区 | 717影院理论午夜伦八戒秦先生 | 国产精品99一区二区 | 欧美五月婷婷 | 久久免费视频一区二区三区 | 国产精品7区| 久久免费精品视频 | 精品国产乱码久久久久久丨区2区 | 毛片大全免费看 | 午夜视频你懂的 | 一级做受毛片免费大片 | 久久吊 | 4p嗯啊巨肉寝室调教男男视频 | 中文字幕亚洲欧美 | 55夜色66夜色国产精品视频 | 羞羞网站在线看 | 国产精品啪 | 蜜桃视频在线播放 | 久久精品国产一区二区电影 | 精品国产精品久久 | 久久欧美亚洲另类专区91大神 | 国产羞羞视频 | 史上最强炼体老祖动漫在线观看 | 久久亚洲精品久久国产一区二区 | 狠狠干天天操 | 青青草成人免费视频在线 |