题面
Mark loves money very much, and he keeps most of it in the bank. He stores his money in different banks. In the -th bank, he has rubles.
One day, Mark decided to gather all his money in one bank, so he will be transferring money from one bank account to another. All interbank transfers are arranged the same way: he can transfer only rubles in one transfer, and considering all fees, rubles will be credited to the other account (since banks want to make a profit, it holds that ).
It is possible that Mark will not be able to transfer all his money to one bank, but he wants to find the maximum number of rubles that can end up in any bank.
Input
Each test contains multiple test cases. The first line contains the number of test cases (). The description of the test cases follows.
The first line of each test case contains three integers , , and (, ) — the number of banks, the transfer amount, and the credited amount.
The second line of each test case contains integers () — the initial amount of rubles in each bank.
It is guaranteed that the sum of the values of across all test cases does not exceed .
Output
For each test case, output a single number — the maximum number of rubles that can be obtained in any bank.
思路
首先理解题意:进行若干次转账操作,使得出现一家银行金额最大
WARNING易错:最大银行的选择
我在初次看到这道题的时候误以为:“这个银行必定是未存钱前的金额最大的银行”
但事实不是这样,当有: 时,选择第二个银行(99)反而是最优解
解释:当 时,第一个银行转账十次,而第二个银行只用转账九次,多余资金达到了
理清上面的内容,我们就要着手解决问题
我们可以假设第 个银行是我们要求的银行,对于任意一个银行,我们知道,最多转出次数为:
如果要保证这个银行存有的资金是最多的,那么需要满足下面的条件:
- 其它银行将全部的能转移的次数全部给了自己
- 自己不向外转钱
那么我们可以得到第 个银行的最大金额为:
因此,我们只需要对所有银行求出其最大金额 ,并从中找出最大的即可
代码
#include <bits/stdc++.h>
#define int long long
using namespace std;
signed main() { signed t; cin >> t; while (t --) { int n, x, y; cin >> n >> x >> y; vector<int> a(n + 1); for (int i = 1; i <= n; i ++) cin >> a[i];
int b = 0; for (int i = 1; i <= n; i ++) b += a[i] / x;
vector<int> r; for (int i = 1; i <= n; i ++) { int R = 0; R = a[i] + (b - a[i] / x) * y; r.push_back(R); }
cout << *max_element(r.begin(), r.end()) << endl; }}