哭哭哭,vector没有清空.....
调了好久,不过也学到了类似遍历的写法,有收获!个人感觉我这份代码的可读性是最好的(捂脸)
-
输入
- 第一行输入一个正整数N表示共有n次测试数据。
每一组测试数据的第一行有三个整数n,w,h,n表示共有n个喷水装置,w表示草坪的横向长度,h表示草坪的纵向长度。
随后的n行,都有两个整数xi和ri,xi表示第i个喷水装置的的横坐标(最左边为0),ri表示该喷水装置能覆盖的圆的半径。
输出 - 每组测试数据输出一个正整数,表示共需要多少个喷水装置,每个输出单独占一行。
如果不存在一种能够把整个草坪湿润的方案,请输出0。
样例输入 -
2 2 8 6 1 1 4 5 2 10 6 4 5 6 5
样例输出 -
1 2
//我的AC代码
#include <cstdio>
#include <cmath>
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
const int maxn=10001;
struct Edge
{
double left,right;
Edge(double x,double y):left(x),right(y) {}
bool operator <(const Edge &t) const
{
return left<t.left;
}
};
vector<Edge> vec;
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int n;
double w,h;
scanf("%d%lf%lf",&n,&w,&h);
double x,y;
for(int i=1;i<=n;i++)
{
scanf("%lf%lf",&x,&y);
if(y<=h/2.0) continue;
double temp= sqrt( y*y-h*h/4.0 );
vec.push_back(Edge(x-temp,x+temp));
}
sort(vec.begin(),vec.end());
int cnt=0;
double preright=0;
double maxright=0;
int Len=vec.size();
for(int i=0;i<Len;i++)
{
int j=i;
for(; j<Len && vec[j].left<=preright ; j++)
{
if(vec[j].right>=maxright)
{
maxright=vec[j].right;
}
}
if(maxright>preright)
{
cnt++;
preright=maxright;
if(preright>=w) break;
}
else
{
break;
}
i=j-1;
}
if(preright>=w)
printf("%d\n",cnt);
else
printf("0\n");
vec.clear();
}
return 0;
}