天天看點

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

8 1 10 2 6 5

Yes

9 1 10 9 2 9

20 15 25 3 5 7

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();
	}
}