D. Fight with Monsters
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
There are n monsters standing in a row numbered from 1 to n. The i-th monster has hi health points (hp). You have your attack power equal to a hp and your opponent has his attack power equal to b hp.
You and your opponent are fighting these monsters. Firstly, you and your opponent go to the first monster and fight it till his death, then you and your opponent go the second monster and fight it till his death, and so on. A monster is considered dead if its hp is less than or equal to 0.
The fight with a monster happens in turns.
You hit the monster by a hp. If it is dead after your hit, you gain one point and you both proceed to the next monster.
Your opponent hits the monster by b hp. If it is dead after his hit, nobody gains a point and you both proceed to the next monster.
You have some secret technique to force your opponent to skip his turn. You can use this technique at most k times in total (for example, if there are two monsters and k=4, then you can use the technique 2 times on the first monster and 1 time on the second monster, but not 2 times on the first monster and 3 times on the second monster).
Your task is to determine the maximum number of points you can gain if you use the secret technique optimally.
Input
The first line of the input contains four integers n,a,b and k (1≤n≤2⋅105,1≤a,b,k≤109) — the number of monsters, your attack power, the opponent’s attack power and the number of times you can use the secret technique.
The second line of the input contains n integers h1,h2,…,hn (1≤hi≤109), where hi is the health points of the i-th monster.
Output
Print one integer — the maximum number of points you can gain if you use the secret technique optimally.
Examples
inputCopy
6 2 3 3
7 10 50 12 1 8
outputCopy
5
inputCopy
1 1 100 99
100
outputCopy
1
inputCopy
7 4 2 1
1 3 5 4 2 7 6
outputCopy
6
1.规则是对于每个怪物,都是自己先进行攻击。(清楚这点很重要)
2.对于每个怪物的hp,我们都进行对(a+b)取余。取余的结果必<=(a+b)。此时轮到我们进行攻击。若此时hp已经为0(表示上一轮对手将其打死),如果我们要对手跳过的话此时怪物hp应为b,所以设其值为b
3.若此时hp>0。我们将其值减a. 表示攻击后怪物的hp。1.若此时hp<=0,表示怪物被我们打死。2.若hp>0,则表示怪物没死,但此时轮到对手了,如果要跳过的话,此时设其值取余后在减a
4.对hp值进行排序,注意。排序后去选择的策略,与排序前去选择的策略是一样的。
#include<iostream>
#include<algorithm>
using namespace std;
int main() {
int n, a, b, k;
int h[200005];
cin >> n >> a >> b >> k;
for (int i = 0; i < n; i++) {
cin >> h[i];
h[i] = h[i] % (a + b);
if (h[i]) h[i] -= a;
else h[i] = b;
}
sort(h, h + n);
int ans = 0;
for (int i = 0; i < n; i++) {
if (h[i] <= 0) ans++;
else {
while (h[i] > 0&&k) {
h[i] -= a;
k--;
}
if (h[i] <= 0) ans++;
}
}
cout << ans << endl;
return 0;
}