河南第六届ACM省赛(外星人的供给站)

题目地址:http://acm.nyist.net/JudgeOnline/problem.php?pid=710

思路:POJ1328原题,没想到把变量定义成int类型,按double类型使用会导致re,头一次遇到这种情况

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <cstring>
#include <climits>
#include <cmath>
#include <cctype>

const int inf = 0x7f7f7f7f;//2139062143
typedef long long ll;
using namespace std;

struct point
{
    double left;
    double right;
};

bool cmp(point a,point b)
{
    return a.left < b.left;
}

int main()
{
    int t;
    int n;
    double r;
    point a[110];
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%lf",&n,&r);
        double x,y;//刚开始把x,y定义成int类型的,结果老是re
        for(int i=0; i<n; i++)
        {
            scanf("%lf%lf",&x,&y);
            a[i].left = x - sqrt(r*r-y*y);
            a[i].right = x + sqrt(r*r-y*y);
        }
        sort(a,a+n,cmp);
        double k = a[0].right;
        int sum = 1;
        for(int i=1; i<n; i++)
        {
            if(a[i].left > k)
            {
                sum++;
                k = a[i].right;
            }
            else if(a[i].right < k)//左右都在上一个点的边界之内,所以要更新右边界
            {
                k = a[i].right;
            }
        }
        printf("%d\n",sum);
    }
    return 0;
}

我这个代码的错误是通过原点来判断,这样其实有很多错误,假如前一个点高一点,后面一个点低一点,这个圆的圆心应该要更新,我的却没有更新

错误代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <cstring>
#include <climits>
#include <cmath>
#include <cctype>

const int inf = 0x7f7f7f7f;//2139062143
typedef long long ll;
using namespace std;

struct point
{
    double x;
    double y;
};

bool cmp(point a,point b)
{
    return a.x < b.x;
}

int main()
{
    int t;
    int n;
    double r;
    scanf("%d",&t);
    point a[110];
    while(t--)
    {
        scanf("%d%lf",&n,&r);
        for(int i=0; i<n; i++)
        {
            scanf("%lf%lf",&a[i].x,&a[i].y);
        }
        sort(a,a+n,cmp);
        double temp = sqrt(r*r-a[0].y*a[0].y);
        temp += a[0].x;//求出第一个点的圆心
        //printf("%lf\n",temp);
        int sum = 1;
        for(int i=1; i<n; i++)
        {
            double temp1 = sqrt(r*r-a[i].y*a[i].y);
            temp1 = a[i].x - temp1;
            //printf("%lf\n",temp1);
            if(temp1 <= temp)
            {
                continue;
            }
            temp = temp1;
            sum++;
        }
        printf("%d\n",sum);
    }
    return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值