UPC 组队训练 Smooth Sequences(逻辑归类)

问题 A: Smooth Sequences
时间限制: 1 Sec 内存限制: 128 MB
提交: 236 解决: 75
[提交] [状态] [讨论版] [命题人:admin]
题目描述

A sequence of n integers is called smooth, if the absolute values of the differences between successive numbers are at most d. We call a non-smooth sequence semi-smooth, if we can change exactly one number in the sequence to make it smooth. For example, let n = 4 and d = 5. The sequence 21, 20, 18, 20 is smooth, since each pair of adjacent numbers has a gap smaller than 5. The sequence 21, -8, 20, 20 is semi-smooth, since we can replace -8 with 20 to make it smooth. Write a program to test if a sequence of integers is smooth or semi-smooth.

输入

An instance of the problem consists of the size n of the sequence, and the gap d in a line.
They are positive integers in the range [1, 1000]. Then n integers, separated with space(s),follow in the next line.
Note that the test data file may contain more than one instance. The last instance is followed by a line containing a single 0.

输出

For each instance, output “Y”, if the sequence is smooth or semi-smooth in a separate line.Otherwise, output “N”.

样例输入

3 2
2 4 6
3 1
3 5 7
0

样例输出

Y
N

提示

1.All integers in the sequence have absolute values bounded by 220.
2.1≤n,d≤1000.
题意:如果连续数之间的差的绝对值在D以上,序列称为光滑的,如果我们可以在序列中精确地改变一个数以使其平滑,我们称为非光滑序列半光滑。例如,n=4,d=5。序列21, 20, 18、20是平滑的,因为每对相邻的数字具有小于5的间隙。序列21,-8, 20, 20是半光滑的,因为我们可以用20代替8,使其平滑。编写一个程序来测试一个整数序列是否平滑或半光滑。
对于每个实例,如果序列是平滑的或半光滑的,输出“Y”,否则,输出“N”。
思路:一个n个整数的序列,求出其两两相邻的数的差值,
(1)如果差值中大于d的有两个或两个以上,则序列一定不是光滑或半光滑序列,输出“N”。
(2)当差值中大于d的有1个,则该差值与其相邻的差值的平均值小于等于d(因为三个数,两个差值,一定是将中间的数字该小)则输出Y,否则输出N。
(3)当没有差值大于d时直接输出Y。

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,d;
    while(scanf("%d",&n)!=EOF)
    {
        if(n==0) break;
        scanf("%d",&d);
        double num[n],dis[n];int cnt=0;
        for(int i=0;i<n;i++)
        {
            scanf("%lf",&num[i]);
            if(i>0)
            {
                dis[i-1]=num[i]-num[i-1];
                if(dis[i-1]>d)
                    cnt++;
            }
        }
        if(cnt>1) printf("N\n");
        else
        {
            if(cnt==0) printf("Y\n");
            else
            {
                for(int i=0;i<n-1;i++)
                {
                    if(dis[i]>d&&(dis[i]+dis[i+1])/2<=d)
                    {
                        printf("Y\n");
                        break;
                    }
                    else if(dis[i]>d&&(dis[i]+dis[i+1])/2>d)
                    {
                        printf("N\n");
                        break;
                    }
                }
            }
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值