问题标签
贪心(greedy)
数学(math)
*1100
原题链接:
题目描述
Monocarp is playing a computer game. In order to level up his character, he can complete quests. There are nn quests in the game, numbered from 1 to n .
Monocarp can complete quests according to the following rules:
- the 1 -st quest is always available for completion;
- the i -th quest is available for completion if all quests j<i have been completed at least once.
Note that Monocarp can complete the same quest multiple times.
For each completion, the character gets some amount of experience points:
- for the first completion of the i -th quest, he gets aiai experience points;
- for each subsequent completion of the i -th quest, he gets bibi experience points.
Monocarp is a very busy person, so he has free time to complete no more than k quests. Your task is to calculate the maximum possible total experience Monocarp can get if he can complete no more than k quests.
输入格式
The first line contains a single integer t ( 1≤t≤104 ) — the number of test cases.
The first line of each test case contains two integers nn and kk ( 1≤n≤2⋅105 ; 1≤k≤2⋅105 ) — the number of quests and the maximum number of quests Monocarp can complete, respectively.
The second line contains nn integers a1,a2,…,an ( 1≤ai≤103 ).
The third line contains nn integers b1,b2,…,bn( 1≤bi≤103 ).
Additional constraint on the input: the sum of nn over all test cases does not exceed 2⋅105.
输出格式
For each test case, print a single integer — the maximum possible total experience Monocarp can get if he can complete no more than k quests.
题意翻译
有 n 个任务,在完成 i 任务时,i 之前的所有任务都必须完成。同时,可以多次完成某个任务。总共可以做 k 次任务,第一次完成 i 任务的贡献为 ai ,后面完成 i 任务的贡献为 bi ,求最大贡献。
输入输出样例
输入 #1复制
4 4 7 4 3 1 2 1 1 1 1 3 2 1 2 5 3 1 8 5 5 3 2 4 1 4 2 3 1 4 7 6 4 1 4 5 4 5 10 1 5 1 2 5 1
输出 #1复制
13 4 15 15
说明/提示
In the first test case, one of the possible quest completion sequences is as follows: 1,1,2,3,2,4,4 ; its total experience is equal to 4‾+1+3‾+1‾+1+2‾+1=13(the underlined numbers correspond to the instances when we complete a quest for the first time).
In the second test case, one of the possible quest completion sequences is as follows: 1,1 ; its total experience is equal to 1‾+3=4 .
In the third test case, one of the possible quest completion sequences is as follows: 1,2,2,2,3 ; its total experience is equal to 3‾+2‾+3+3+4‾=15 .
思路:
这是一道贪心的题目,开始的时候我还使用动态规划去做这道题,发现不太适合,这时我有想起来这道题前两天刚做的 http://t.csdnimg.cn/c2m8R 这道题,与这道题很类似,他都是关于是否要在某个位置进行停留,要停留多久,进行一定的轮数,然后呢,要求一定轮数下来的可以得到的最大分数呢,或则是最大经验值,反正就是求个最大值,我们就想起上篇文章的暴力枚举了,因为他一共就n个位置,如果n个位置都走完了以后,还有剩余的轮数的话,我们肯定剩余的轮数都选择b[i] 当中最大的那个值去走,即走完n个位置的得分加上剩余轮数*b数组的最大值,那么对于走了k轮,其实就是枚举一下每一个i,我们对于k轮,每次都让其往前走,然后计算一下走了这么多步,如果剩余的不再往前走的最大值是多少,这样就可以求解出最大值,我说的可能比较难懂,其实你们看代码就理解了
#include<bits/stdc++.h>
using namespace std;
void solve()
{
int n,k;
cin>>n>>k;
vector<int> a(n);
vector<int> b(n);
for(int i=0;i<n;i++)
{
cin>>a[i];
}
for(int i=0;i<n;i++)
{
cin>>b[i];
}
long long sum=0;
long long ans=0;
int curmax=0;
for(int i=0;i<min(n,k);i++)
{
sum+=a[i];
curmax=max(curmax,b[i]);
ans=max(ans,1LL*(k-i-1)*curmax+sum);
}
cout<<ans<<endl;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin>>t;
while(t--)
{
solve();
}
return 0;
}