poj 1328 Radar Installation 贪心

老规矩,链接思密达http://poj.org/problem?id=1328

典型贪心

主要有三个问题

1.sort函数里加的n不能是int外的类型,完全没碰到过这么wa的
2.结构体的cmp函数,<是升序,>是降序
3.没考虑到一个特殊情况,就是下一个区间如果右点小于上一个的右点要更新公共右点,下面代码

#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;
struct qujian
{
 double left;
 double right;
}dao[1005];
bool cmp(qujian a,qujian b){
return a.left<b.left;
}//升序排列
int main()
{
    double d,flag,y,x,chang,num,comright,m;
    int b,n;

    while(cin>>n>>d&&(n!=0||d!=0))
    {
      num=1;
      m++;
      flag=1;
      if(d==0)
        flag=0;
     for(int i=0;i<n;i++)
    {
      cin>>x>>y;
      chang=sqrt(d*d-y*y);
      dao[i].left=x-chang;
      dao[i].right=x+chang;
      if(y>d)
        flag=0;
    }
    if(flag==0)
    {
      cout<<"Case "<<m<<": "<<-1<<endl;
      continue;
    }
    else
    if(n==0)
    {
    cout<<"Case "<<m<<": "<<0<<endl;
    continue;
    }
    sort(dao,dao+n,cmp);
    comright=dao[0].right;
    for(b=1;b<n;b++)
    {
     if(dao[b].left>comright)
     {
        num++;
        comright=dao[b].right;

     }
     else
     {
         if(dao[b].right<=comright)
        comright=dao[b].right;
     }
    }
    cout<<"Case "<<m<<": "<<num<<endl;
    }

    return 0;
}

c++交,g++有毒

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值