码迷,mamicode.com
首页 > 其他好文 > 详细

poj1328 贪心

时间:2015-10-14 21:30:50      阅读:169      评论:0      收藏:0      [点我收藏+]

标签:

  1 #include<iostream>
  2 #include<string.h>
  3 #include<algorithm>
  4 #include <math.h>
  5 const int maxn=1001;
  6 using namespace std; 
  7 int n;
  8 int d;
  9 double max_x,min_x;
 10 struct Islands
 11 {
 12     double x;
 13     double y;
 14 }point[maxn];
 15 struct Center//记录每个岛屿允许的雷达的横坐标范围
 16 {
 17     double min_x;
 18     double max_x;
 19 }center[maxn];
 20 bool together(double* max_x ,double* min_x,double other_min_x,double other_max_x)//查看两个岛屿与否能使用同一个雷达
 21 {
 22     if (*max_x>other_max_x)
 23     {
 24         *max_x=other_max_x;
 25     }
 26     if (*min_x<other_min_x)
 27     {
 28         *min_x=other_min_x;
 29     }
 30     if (*max_x<*min_x)    
 31         return false;
 32     else
 33         return true;
 34 
 35 }
 36 int solve()
 37 {
 38     int ans=0;
 39     int i=0;
 40     int j;
 41     for (int i=0;i<n;i++)
 42     {
 43         if (point[i].y>d)
 44         {
 45             return -1;
 46         }
 47     }
 48     while(i<n)
 49     {
 50         j=i+1;
 51         max_x=center[i].max_x;
 52         min_x=center[i].min_x;
 53         while(j<n)
 54         {//这个雷达可以服务的岛屿
 55             if(together(&max_x,&min_x,center[j].min_x,center[j].max_x))
 56                 j++;
 57             else
 58             {
 59                 break;
 60             }
 61         }
 62         i=j;
 63         ans++;
 64         if (i==n)
 65         {
 66             break;//循环出口
 67         }
 68     }
 69     return ans;
 70 }
 71 void getcenter(double x,double y,int i)
 72 {
 73     double r;
 74     r=sqrt(d*d-y*y);
 75     center[i].min_x=x-r;
 76     center[i].max_x=x+r;
 77 }
 78 
 79 bool cmp(const Islands &a,const Islands &b)
 80 {
 81     return a.x<b.x||(a.x==b.x&&a.y<b.y);
 82 }
 83 int main()
 84 {
 85     int time=1;
 86     
 87     while(cin>>n>>d&&(n||d))
 88     {
 89         for (int i=0;i<n;i++)
 90         {
 91             cin>>point[i].x>>point[i].y;
 92         }
 93 
 94         sort(point,point+n,cmp);
 95 
 96         for(int i=0;i<n;i++)
 97         {
 98             getcenter(point[i].x,point[i].y,i);
 99         }
100         cout<<"Case "<<time++<<": "<<solve()<<endl;
101     }
102     return 0;
103 }

 

poj1328 贪心

标签:

原文地址:http://www.cnblogs.com/acmray/p/4878448.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!