poj 1328 Radar Installation(贪心算法)

15 篇文章 0 订阅
这一题我提交了不下10遍,我的ac率!
错误最后发现在case 后面的情况数下!没有仔细比较结果啊!


/*贪心算法*/
#include <iostream>
//#include <fstream>
#include <algorithm>
#include <math.h>
#define MAX 1010
using namespace std;

typedef struct _point
{
    int x;
    int y;
}point;

typedef struct _bound
{
    double left;
    double right;
}bound;

void swap(bound &a,bound &b)
{
    bound temp=a;
    a=b;
    b=temp;    
}


//fstream fin;
int main()
{
    //fin.open("1328.txt",ios::in);
    int n,d;
    int count=1; //case
    while(cin>>n>>d)
    {
        //cout<<n<<endl;
        if(n==0&&d==0) 
             break;
        point *p=new point[n];
        bound *b=new bound[n];
        
        for(int i=0;i<n;i++)
           cin>>p[i].x>>p[i].y;
        
        bool flag=true;
        for(int i=0;i<n;i++)
          if(d<abs(p[i].y))
          {
              cout<<"Case "<<count++<<": -1"<<endl;
              flag=false;
              break;
          }
        //计算一个顶点的左右界
       if(flag){
         for(int i=0;i<n;i++)
         {
           b[i].left=p[i].x*1.000-sqrt(d*d*1.00-p[i].y*p[i].y*1.0000);
           b[i].right=p[i].x*1.000+sqrt(d*d*1.00-p[i].y*p[i].y*1.0000);
         }
         
        for(int i=0;i<n;i++)
         {
             int j=i;
             while(b[j].left<b[j-1].left&&j>0)
             {
                 swap(b[j],b[j-1]);
                 j--;
             }
         } 
 
         int num=1; //记录雷达的个数
         double temp=b[0].right;
         for(int i=0;i<n-1;i++)
         {
            if(b[i+1].left>temp) 
            {
                temp=b[i+1].right;
                num++;
            }
            else if(b[i+1].right<temp)
              temp=b[i+1].right; 
         } 
         cout<<"Case "<<count++<<": "<<num<<endl;
      } 
      
      delete []p;
      delete []b;   
    } 
    system("pause");
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值