问题 B: Moderate Differences--------------------思维(数学)

53 篇文章 0 订阅
50 篇文章 1 订阅

题目描述
There are N squares in a row. The leftmost square contains the integer A, and the rightmost contains the integer B. The other squares are empty.

Aohashi would like to fill the empty squares with integers so that the following condition is satisfied:

For any two adjacent squares, the (absolute) difference of the two integers in those squares is between C and D (inclusive).
As long as the condition is satisfied, it is allowed to use arbitrarily large or small integers to fill the squares. Determine whether it is possible to fill the squares under the condition.

Constraints
3≤N≤500000
0≤A≤109
0≤B≤109
0≤C≤D≤109
All input values are integers.
输入
Input is given from Standard Input in the following format:

N A B C D
输出
Print YES if it is possible to fill the squares under the condition; print NO otherwise.
样例输入 Copy
5 1 5 2 4
样例输出 Copy
YES
提示
For example, fill the squares with the following integers: 1, −1, 3, 7, 5, from left to right.

题意:
有N个格子排成一排,最左边的里面写了数字A,最右边的写了数字B,中间的 格子都是空的。 你需要在中间的每个格子里填上一个数字,使得这个序列中,任意相邻两个数 的差的绝对值在[C,D]之间。 问是否存在这样一种可行的填数方案,输出YES或者NO。

解析:

假设现在存在K个 C≤xi+1-xi≤D 因为要满足n-1个连续,所以一定有n-1-k个 -D≤xi+1-xi≤-C
又因为 ∑(xi+1-xi)=xn-x1  即B-A 。我们把这些式子加起来求和
得到
左区间: K*C-(n-1-k)*d
右区间: K*D-(n-1-k)*C;

那么就得到了  K*C-(n-1-k)*d≤B-A≤K*D-(n-1-k)*C

枚举k就完事了

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n;
ll a,b,c,d;
int main()
{
    while(scanf("%lld",&n)==1)
    {
        scanf("%lld %lld %lld %lld",&a,&b,&c,&d);
        for(ll i=0;i<=n-1;i++)//因为有可能存在0个C≤xi+1−xi≤D的式子
        {
            if((b-a)>=(i*c-(n-i-1)*d)&&(b-a)<=(i*d-(n-i-1)*c))
            {
                printf("YES");
                return 0;
            }
        }
    }
    printf("NO");
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值