您的位置:首页 > Web前端

AtCoder Grand Contest 017-B - Moderate Differences

2017-07-09 23:26 513 查看

B - Moderate Differences

Time limit : 2sec / Memory limit : 256MB

Score : 400 points

Problem Statement

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

Input is given from Standard Input in the following format:

N A B C D

Output

Print YES if it is possible to fill the squares under the condition; print NO otherwise.

Sample Input 1

5 1 5 2 4

Sample Output 1

YES

For example, fill the squares with the following integers: 1, −1, 3, 7, 5, from left to right.

Sample Input 2

4 7 6 4 5

Sample Output 2

NO

Sample Input 3

48792 105960835 681218449 90629745 90632170

Sample Output 3

NO

Sample Input 4

491995 412925347 825318103 59999126 59999339

Sample Output 4

YES

题目大意:一行有n个方格,第一个中填入了一个数A,最后一个填入了一个数B,其他格子尚未填写,问是否存在一种填入方法,使得每两个格子中数字之差的绝对值在[C,D]区间中。

解题思路:详见editorial

#include<iostream>
using namespace std;
typedef long long LL;
const int MAXN=105;
LL n,a,b,c,d;

int main()
{
while(cin>>n>>a>>b>>c>>d)
{
bool flag=false;
for(int i=0;i<=n-1;i++)
{
if(c*(n-1-i)-d*i<=b-a&&(n-1-i)*d-c*i>=b-a)
{
flag=true;
break;
}
}
if(flag) cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: