Codeforces Round #689 (Div. 2, based on Zed Code Competition) E. Water Level 贪心

E. Water Level

In recent years John has very successfully settled at his new job at the office. But John doesn't like to idly sit around while his code is compiling, so he immediately found himself an interesting distraction. The point of his distraction was to maintain a water level in the water cooler used by other zebras.

Originally the cooler contained exactly 𝑘 liters of water. John decided that the amount of water must always be at least 𝑙 liters of water but no more than 𝑟 liters. John will stay at the office for exactly 𝑡 days. He knows that each day exactly 𝑥 liters of water will be used by his colleagues. At the beginning of each day he can add exactly 𝑦 liters of water to the cooler, but at any point in time the amount of water in the cooler must be in the range [𝑙,𝑟].

Now John wants to find out whether he will be able to maintain the water level at the necessary level for 𝑡 days. Help him answer this question!

Input

The first line of the input contains six integers 𝑘, 𝑙, 𝑟, 𝑡, 𝑥 and 𝑦 (1≤𝑙≤𝑘≤𝑟≤1018;1≤𝑡≤1018;1≤𝑥≤106;1≤𝑦≤1018) — initial water level, the required range, the number of days, daily water usage and the exact amount of water that can be added, respectively.

Output

Print "Yes" if John can maintain the water level for 𝑡 days and "No" otherwise.

Examples

inputCopy

8 1 10 2 6 4

outputCopy

No

inputCopy

8 1 10 2 6 5

outputCopy

Yes

inputCopy

9 1 10 9 2 9

outputCopy

No

inputCopy

20 15 25 3 5 7

outputCopy

Yes

Note

In the first example, John can't increase the amount of water at the beginning of the first day, since it would exceed the limit 𝑟. That is why after the first day the cooler will contain 2 liters of water. The next day John adds 4 liters to the cooler but loses 6 liters, leaving John with 0 liters, which is outside the range [1,10].

In the second example, after the first day John is left with 2 liters of water. At the beginning of the next day he adds 5 liters, then 6 liters get used, leaving John with 1 liter of water which is in range [1,10].

In the third example, after the first day John is left with 7 liters, after the second day — 5 liters, after the fourth — 1 liter. At the beginning of the fifth day John will add 9 liters and lose 2 liters. Meaning, after the fifth day he will have 8 liters left. Then each day the water level will decrease by 2 liters and after the eighth day John will have 2 liters and after the ninth day — 0 liters. 0 is outside range [1,10], so the answer is "No".

In the fourth example, after the first day John is left with 15 liters of water. At the beginning of the second day he adds 7 liters and loses 5, so after the second day he is left with 17 liters. At the beginning of the third day he adds 7 more liters of water and loses 5, so after the third day he is left with 19 liters. 19 is in range [15,25] so the answer is "Yes".

题意

有个饮水机,一开始有k升水。

每天早上你可以往里面加y升水,每天晚上你的同事一定会喝掉x升水

问你能否有一种方案使得t天内,水都在[l,r]这个范围。

题解

分成两种情况讨论:

第一种情况 x>=y的时候,当y>=x 即 我每天加的水小于同事喝的水的时候,我贪心使得每天降的尽量少就行。第一天如果k+y<=r,我就加水,否则不加。剩下的每一天都加水。判断一下天数是否大于等于t即可。

第二种情况y>x的时候,当x>y的情况,即每天我加的水比喝的水多。那么先让同事随便喝,直到不能喝位置,然后我再加一次水。然后再让同事喝水,我再加一次水,这样一直循环即可。如果当前水量曾经遇到过,那么就说明可以无限循环了。因为每天剩下的水量等于 k mod x,所以最多遇到x次,复杂度O(x)

代码

#include<bits/stdc++.h>
using namespace std;
long long k,l,r,t,x,y;
const int maxn = 1e6+5;
int vis[maxn];
bool check(long long now) {
	if (now > r || now < l) {
		return false;
	}
	return true;
}
void solve1() {
	if (k+y>r) {
		t--;
		k-=x;
	}
	if (!check(k)) {
		cout<<"NO"<<endl;
		return;
	}
	int gap=x-y;
	if (gap == 0) {
		cout<<"YES"<<endl;
		return;
	}
	// cout<<k<<" "<<l<<" "<<gap<<" "<<t<<endl;
	if((k-l)/gap>=t) {
		cout<<"YES"<<endl;
	} else {
		cout<<"NO"<<endl;
	}
}
void solve2() {
	// step 0 k->l
	long long num1 = (k-l)/x;
	if (num1 >= t) {
		cout<<"YES"<<endl;
		return;
	}
	k = k-num1*x;
	t-= num1;
	// step 1 无限循环的增加一次,减少无数次
	while((!vis[k])&&t>0) {
		// cout<<k<<" "<<t<<endl;
		vis[k]=1;
		k=k+y;
		if(!check(k)) {
			cout<<"NO"<<endl;
			return;
		}    
		long long num2 = (k-l)/x;
		t-=min(num2,t);
		k=k-num2*x;
		// cout<<"2:" << k<<" "<<t<<endl;
	}
	cout<<"YES"<<endl;
}
int main() {
	cin>>k>>l>>r>>t>>x>>y;
	k-=l;
	r-=l;
	l=0;
	if (x>=y) {
		solve1();
	} else {
		solve2();
	}
}
posted @ 2020-12-16 01:08  qscqesze  阅读(214)  评论(0编辑  收藏  举报