D. Fight with Monsters

本文探讨了一种游戏策略,玩家需与对手合作对抗一系列怪物,目标是在限制条件下使用特殊技巧来最大化个人得分。通过算法优化,确定了如何在攻击怪物时最佳利用特殊技巧。

链接:https://codeforces.com/contest/1296/problem/D

There are nn monsters standing in a row numbered from 11 to nn. The ii-th monster has hihi health points (hp). You have your attack power equal to aa hp and your opponent has his attack power equal to bb 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 00.

The fight with a monster happens in turns.

  1. You hit the monster by aa hp. If it is dead after your hit, you gain one point and you both proceed to the next monster.
  2. Your opponent hits the monster by bb 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 kk times in total (for example, if there are two monsters and k=4k=4, then you can use the technique 22 times on the first monster and 11 time on the second monster, but not 22 times on the first monster and 33 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,bn,a,b and kk (1≤n≤2⋅105,1≤a,b,k≤1091≤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 nn integers h1,h2,…,hnh1,h2,…,hn (1≤hi≤1091≤hi≤109), where hihi is the health points of the ii-th monster.

Output

Print one integer — the maximum number of points you can gain if you use the secret technique optimally.

Examples

input

Copy

6 2 3 3
7 10 50 12 1 8

output

Copy

5

input

Copy

1 1 100 99
100

output

Copy

1

input

Copy

7 4 2 1
1 3 5 4 2 7 6

output

Copy

6

代码:

#include<bits/stdc++.h>
using namespace std;
long long n,a,b,k,t,g,ans,s,max1=0; 
long long x[1000001],c[1000001];
int main()
{
    cin>>n>>a>>b>>k;
   	t=1; 
   	s=0;
   	long long j=0;
    for(int i=1;i<=n;i++)
    {
    	cin>>x[i];
    	x[i]=((x[i]-1)%(a+b))/a;
    }
    sort(x+1,x+1+n);
    for(int i=1;i<=n;i++)
    {
    	k-=x[i];
    	if(k>=0)
    	{
    		s++;
    	}
    	else
    	break;
    }
    cout<<s<<endl;
}

 

c++:Problem Statement Takahashi is about to play a game where he fights N monsters in order. Initially, Takahashi's health is H and his magic power is M. For the i-th monster he fights, he can choose one of the following actions: Fight without using magic. This can only be chosen when his health is at least A i ​ , and his health decreases by A i ​ and the monster is defeated. Fight using magic. This can only be chosen when his magic power is at least B i ​ , and his magic power decreases by B i ​ and the monster is defeated. The game ends when all N monsters are defeated or when he cannot take any action. What is the maximum number of monsters he can defeat before the game ends? Constraints 1≤N≤3000 1≤H,M≤3000 1≤A i ​ ,B i ​ ≤3000 All input values are integers. Input The input is given from Standard Input in the following format: N H M A 1 ​ B 1 ​ A 2 ​ B 2 ​ ⋮ A N ​ B N ​ Output Output the answer. Sample Input 1 Copy 4 10 14 5 8 5 6 7 9 99 99 Sample Output 1 Copy 3 By taking the following actions, Takahashi can defeat 3 monsters before the game ends. Initially, his health is 10 and his magic power is 14. Fight the 1st monster without using magic. His health decreases by 5, becoming health 5 and magic power 14. Fight the 2nd monster without using magic. His health decreases by 5, becoming health 0 and magic power 14. Fight the 3rd monster using magic. His magic power decreases by 9, becoming health 0 and magic power 5. For the 4th monster, he cannot choose either action, so the game ends. Sample Input 2 Copy 3 3000 3000 3 3 3 3 3 3 Sample Output 2 Copy 3 He may be able to defeat all monsters. Sample Input 3 Copy 10 8 8 2 2 2 3 2 2 1 2 2 3 1 2 3 3 3 2 3 1 3 2 Sample Output 3 Copy 9
最新发布
06-15
### 动态规划与战斗模拟:最大怪物数量计算 #### 问题描述 给定Takahashi的初始攻击力`H`,以及`N`个怪物,每个怪物具有攻击值`Ai`和生命值`Bi`。Takahashi可以按任意顺序选择怪物进行战斗。每次战斗,Takahashi的攻击力会减少怪物的攻击值`Ai`,如果Takahashi的攻击力大于等于怪物的生命值`Bi`,则怪物被击败。目标是计算Takahashi可以击败的最大怪物数量。 --- #### 解题思路 此问题可以通过动态规划解决。定义状态`dp[i][j]`表示在前`i`个怪物中,使用攻击力`j`时可以击败的最大怪物数量。转移方程如下: - 如果当前怪物`i`不被击败,则`dp[i][j] = dp[i-1][j]`。 - 如果当前怪物`i`被击败,则需要满足`j >= Bi`,并且`dp[i][j] = max(dp[i][j], dp[i-1][j-Bi] + 1)`。 最终答案为`max(dp[N][j])`,其中`j`的范围从0到`H`。 --- #### 算法实现 以下是C++代码实现: ```cpp #include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); int N, H; cin >> N >> H; vector<pair<int, int>> monsters(N); // 存储怪物的(Ai, Bi) for (int i = 0; i < N; ++i) { cin >> monsters[i].first >> monsters[i].second; } // 按照Ai从小到大排序,方便优化 sort(monsters.begin(), monsters.end()); // 动态规划数组 vector<int> dp(H + 1, 0); for (int i = 0; i < N; ++i) { int Ai = monsters[i].first; int Bi = monsters[i].second; // 逆序更新,避免重复使用当前怪物 for (int j = H; j >= Bi; --j) { if (j >= Bi) { dp[j] = max(dp[j], dp[j - Bi] + 1); } } } // 输出结果 cout << *max_element(dp.begin(), dp.end()) << "\n"; return 0; } ``` --- #### 代码解析 1. **输入处理**: - 首先读取Takahashi的初始攻击力`H`和怪物的数量`N`。 - 接着读取每个怪物的攻击值`Ai`和生命值`Bi`,并存储在`monsters`数组中[^4]。 2. **排序优化**: - 将怪物按照攻击值`Ai`从小到大排序,这样可以优先击败攻击值较低的怪物,从而保留更多攻击力用于后续战斗。 3. **动态规划核心逻辑**: - 使用一维数组`dp`来存储当前状态下可击败的最大怪物数量。 - 外层循环遍历每个怪物,内层循环逆序更新`dp`数组,确保每个怪物只被考虑一次。 4. **输出结果**: - 最终答案为`dp`数组中的最大值,即Takahashi可以击败的最大怪物数量。 --- #### 时间复杂度分析 - **排序**:`O(N log N)`,用于对怪物进行排序。 - **动态规划**:`O(N * H)`,其中`N`为怪物数量,`H`为Takahashi的初始攻击力。 - 总体时间复杂度为`O(N log N + N * H)`。 --- #### 注意事项 - 如果`H`非常大(例如超过`1e5`),可能需要进一步优化算法以降低时间复杂度。 - 在实际比赛中,可以尝试贪心策略作为优化手段,但需验证其正确性。 --- ###
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值