您的位置:首页 > 其它

zoj 3908 Number Game(zoj 2015年10月月赛F题)

2015-10-11 17:41 323 查看
Number GameTime Limit: 2 Seconds Memory Limit: 65536 KBThe 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 productof 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
题目大意:
从一个数组里面,每次取2个的和不大于k的值乘在一起,数组内1个数只能选一次,这样最多做m次,求乘积的和值的最大值。
解题思路:
首先要排个序,肯定要从当前里的最大值来考虑,让最大值取满足条件里面的最大的,于是用到了优先队列,将满足条件的放到队列里,
对于每一个数,从队列里取出满足条件的最大值,做乘积,但是这样取出来的数不能直接放到答案里,因为有可能中间的数更大,例如:
k=2000时,数组为1 1000 1000 1999,m=1时,取1000*1000比1*1999更优。于是还要排序,取较大的m个。
代码:
#include <iostream>#include <cstdio>#include <queue>#include <algorithm>using namespace std;const int maxn=200000+1000;int a[maxn];priority_queue<int> qn;priority_queue<int> qn2;int main(){int t;scanf("%d",&t);while(t--){int n,k,m;scanf("%d%d%d",&n,&m,&k);for(int i=0;i<n;i++){scanf("%d",&a[i]);}sort(a,a+n);int be=0;while(!qn.empty())qn.pop();while(!qn2.empty())qn2.pop();for(int i=n-1;i>=0;i--){if(a[i]>k)continue;if(i<be)break;while(a[be]+a[i]<=k&&be<i){qn.push(a[be]);be++;}if(!qn.empty()){int cur=qn.top();qn.pop();cur=cur*a[i];qn2.push(cur);}if(be>=i)break;}int x,y;while(!qn.empty()){x=qn.top();qn.pop();if(!qn.empty()){y=qn.top();qn.pop();x=x*y;qn2.push(x);}elsebreak;}long long ans=0;for(int i=0;i<m;i++){if(qn2.empty())break;int cur=qn2.top();ans=ans+cur;qn2.pop();}printf("%lld\n",ans);}return 0;}
[/code]

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: