题目地址: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;
}