The bored Bob is playing a number game. In the beginning, there are n numbers. For each turn, Bob will take out two numbers from the remaining numbers, and get the product of them. There is a condition that the sum of two numbers must be not larger than k.
Now, Bob is curious to know what the maximum sum of products he can get, if he plays at most m turns. Can you tell him?
Input
The first line of input contains a positive integer T, the number of test cases. For each test case, the first line is three integers n, m(0≤ n, m ≤100000) and k(0≤k ≤20000). In the second line, there are n numbers ai(0≤ ai ≤10000, 1≤ i ≤n).
Output
For each test case, output the maximum sum of products Bob can get.
Sample Input
2 4 2 7 1 3 2 4 3 2 3 2 3 1
Sample Output
14 2
/*
思路:对于一个数,每次肯定取满足条件的最大的数和他乘,然后取前m 大
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<set>
#include<map>
#define L(x) (x<<1)
#define R(x) (x<<1|1)
#define MID(x,y) ((x+y)>>1)
#define bug printf("hihi\n")
#define eps 1e-8
using namespace std;
#define INF 0x3f3f3f3f
#define N 100005
multiset<int>s;
multiset<int>::iterator it;
int n,m,k;
int a[N];
int cmp(int a,int b)
{
return a>b;
}
void solve()
{
int i,j;
long long ans=0;
int cnt=0;
while(s.size()>=2)
{
it=s.end();
--it;
int x=*it;
s.erase(it);
it=s.upper_bound(k-x);
if(it==s.begin()) continue;
--it;
a[cnt++]=x*(*it);
s.erase(it);
}
sort(a,a+cnt,cmp);
for(i=0;i<m&&i<cnt;i++)
ans=ans+a[i];
printf("%lld\n",ans);
}
int main()
{
int i,j,t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d%d",&n,&m,&k);
s.clear();
int x;
while(n--)
{
scanf("%d",&x);
s.insert(x);
}
solve();
}
return 0;
}
本文介绍了一个数字游戏,玩家需要从一组数字中选择两个数相乘,且这两个数的和不超过限制值k,目标是在限定回合内获得最大乘积之和。文章提供了一种策略:始终选择当前条件下最大的两个数进行配对,并给出了实现这一策略的C++代码。
230

被折叠的 条评论
为什么被折叠?



