AtCoder Grand Contest 017 B

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≤CD≤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

Copy
5 1 5 2 4

Sample Output 1

Copy
YES

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


Sample Input 2

Copy
4 7 6 4 5

Sample Output 2

Copy
NO

Sample Input 3

Copy
48792 105960835 681218449 90629745 90632170

Sample Output 3

Copy
NO

Sample Input 4

Copy
491995 412925347 825318103 59999126 59999339

Sample Output 4

Copy
YES

题意:告诉你n个空格,最左边和最右边的数字确定,相邻的空格数字之差(绝对值)在[c,d]范围内,问数字能不能全部添加在空格内
解法:
1 c<=xi+1-xi<=d -d<=xi+1-xi<=-c
2 ∑(xi+1-xi)=xN-xN-1+.....+x3-x2+x2-x1=xN-x1=b-a
3 如果有m个符合-d<=xi+1-xi<=-c 那么应该有n-m+1个符合c<=xi+1-xi<=d
那么 c(n-m+1)-dm<=∑(xi+1-xi)<=-cm+(n-m+1)d
---->c(n-m+1)-dm<=b-a<=-cm+(n-m+1)d
求存在m就行
 1 #include<bits/stdc++.h>
 2 #define N 10005
 3 #define LL long long
 4 #define inf 1<<29
 5 #define eps 1e-7
 6 using namespace std;
 7 long long n,a,b,c,d;
 8 
 9 int main(){
10     int flag=0;
11     cin>>n>>a>>b>>c>>d;
12     for(int i=0;i<n;i++){
13         if(c*(n-1-i)-d*i<=abs(b-a)&&-1*c*(i)+(n-1-i)*d>=abs(b-a)){
14             flag=1;
15         }
16     }
17     if(flag==1){
18         cout<<"YES"<<endl;
19     }else{
20         cout<<"NO"<<endl;
21     }
22     return 0;
23 }
原文地址:https://www.cnblogs.com/yinghualuowu/p/7143696.html