Neiborhood Search(2021_0919_ICPC网络赛第一场I题)

We have a set S of one-dimensional points.

Given a target point A, we would like to find the neighboring points of A in S. We consider two points are neighbors, if and only if they are within a distance of r.

Input

The first line lists all the coordinates of the points in S. Each coordinate (including the last one) is followed by a single space.

The second line is the coordinate of point A.

And the third line is the value of distance r.

You can assume that all the points in S have different coordinates, ∣S∣≤100,000, and all the values above are 16-bit integers.

Output

A single line prints the coordinates of A’s neighbors in S. The coordinates should be sorted in descending order, and each coordinate (including the last one) should be followed by a single space.

If the neighbourhood is empty, print an empty line.

Sample Input
4 2 6 8
5
1

Sample Output
6 4

题意

输入一行数,以换行结尾,每个数后面都跟一个空格(最后一个数也有)
输入a,b,判断距离a小于等于b有哪些点,从大到小输出这些邻居点即可

思路

主要注意输入就可以,题目很简单,因为我没读出来输入的最后一个也有空格,所以一直WA

代码

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
long long b[500010],s[500010];
bool cmp(long long x,long long y)
{
    return x>y;
}
int main()
{
    int j=0;
    char a;
    int f1=0;
    long long ans=0;
    while(~scanf("%c",&a))
    {
        if(a=='\n')
        {
           /* if(f1)
                b[++j]=-ans;
            else
                b[++j]=ans;*/
            break;
        }
        if(a!=' ')
        {
            if(a=='-')
                f1=1;
            else
                ans=ans*10+(a-'0');
        }
        else if(a==' ')
        {
            if(f1==0)
                b[++j]=ans;
            else
                b[++j]=-ans;
            ans=0;
            f1=0;
        }

    }
    long long c,d;
    scanf("%lld",&c);
    scanf("%lld",&d);
    long long e,f;
    e=c-d;
    f=c+d;
    int k=0;
    for(int i=1; i<=j; i++)
    {
        if(b[i]>=e&&b[i]<=f)
            s[++k]=b[i];
    }
    if(k==0)
        printf("\n");
    else
    {
        sort(s+1,s+1+k,cmp);
        for(int i=1; i<=k; i++)
            printf("%lld ",s[i]);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值