cf 题解--D. Dahlia The Champion

D. Dahlia The Champion

Lowie is creating a new champion in the critically acclaimed game LoL (Land of Literacy). Named it Dahlia, he planned that the champion will have 4 different abilities. One of them is Pull 'em Over.

Long story short: Assuming that the map is a Cartesian plane (an O−xy plane), Dahlia and the targets are small enough to be considered as points. When Pull 'em Over is activated, Dahlia throws her stretchy arms at a direction of her choice, and if there is any target in the direction with the distance not more than R from her, Dahlia pull the first target she reaches towards her immediately.

Supposed that Dahlia is standing in position (x0,y0). There are N different target in the map. Without any movements from her and all of the targets, count the number of targets she can choose to Pull 'em Over.

Input
The first line contains four integers: x0, y0, R, N. (|x0|,|y0|≤109, 1≤R≤109, 1≤N≤5∗105) - the coordinates of Dahlia, the radius she can cover, and the number of targets.

The i-th line of the next N lines contain two integers: xi and yi - coordinates of the i-th target.

It is guaranteed that there are no two targets standing on a same position on the map, and there are no targets stand on the same position as Dahlia.

Output
Output a single integer denotes your answer to the test - number of targets she can choose to Pull 'em Over.

Examples
inputCopy
0 0 10 4
1 2
4 1
-1 -2
3 -4
outputCopy
4
inputCopy
1 2 5 4
-2 -2
6 2
10 1
-2 1
outputCopy
3

题目描述:

有四处分布的多个点p。在起点(x0,y0)处,可以把任意方向的距离不超过R的点p拉到身边,问最多能拉多少个。

分析:

判断每个点p与起点的距离是否超过R,如果从起点看,在某一方向上已经把点p拉到身边,p后面的点就不能再拉过来了。

所以用一个矢量<x,y>来记录他们方向。在一个方向上只能选取满足题意的一个点。为了方便,把x同一单位化,这样只比较y就能判断2个点p1,p2是否在同一方向。

代码:

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<sstream>
#include<vector>
#include<stack>
#include<deque>
#include<cmath>
#include<map>
using namespace std;
typedef long long ll;
const int maxn=5e5+6;
const double INF=1e15;
struct odc
{
    double x,y;
    bool ok;
}a[maxn];
bool cmp(struct odc a,struct odc b)
{
    if(a.x==b.x) 
    {
        if(a.y==b.y) return a.ok>b.ok;
        else return a.y<b.y;
    }
    return a.x<b.x;
}
int main()
{
    ll x0,y0,R,n;
    scanf("%lld%lld%lld%lld",&x0,&y0,&R,&n);
    int ans=0;
    for(int i=0;i<n;i++)
    {
        ll x,y;
        scanf("%lld%lld",&x,&y);
        x=x-x0,y=y-y0;
        if(R*R>=x*x+y*y) a[i].ok=1;
        else a[i].ok=0;
        if(x==0) a[i].x=INF,a[i].y=(y>=0?INF:-INF);
        else a[i].x=x/(abs(x)),a[i].y=y*1.0/abs(x);
    }
    sort(a,a+n,cmp);
    if(a[0].ok) ans++;
    for(int i=1;i<n;i++)
    {
        if(a[i].ok&&(a[i].y!=a[i-1].y)) ans++;
    }
    cout<<ans<<endl;
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/studyshare777/p/12678697.html