Atcoder B - Moderate Differences
http://agc017.contest.atcoder.jp/tasks/agc017_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≤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
依题可得:c<=x(i+1)-xi<=d 或者 -d<=x(i+1)-xi<=-c;
且有∑(xi+1-xi)=xN-xN-1+.....+x3-x2+x2-x1=xN-x1=b-a
如果有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就行
#include <iostream> #include <cstdio> #include <cstring> #include <queue> #include <cmath> #include <vector> #include <algorithm> using namespace std; typedef long long ll; int main() { ll a,n,b,c,d; while(cin>>n>>a>>b>>c>>d) { ll k=0; for(ll i=0;i<n;i++) { if((c*(n-i-1)-i*d)<=(b-a)&&(b-a)<=(d*(n-i-1)-i*c)) { puts("YES"); goto eg; } } puts("NO"); eg:; } return 0; }