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]
相关文章推荐
- 快点写作业(作业提醒器)
- poj1700--贪心--Crossing River
- bash + script
- hdu 4605 Magic Ball Game(主席树学习第二弹)
- watchOS开发——配置静态通知界面和动态通知界面
- 《代码阅读方法与实践》阅读笔记2
- 简介
- vim增加emmet插件
- 封装、继承、多态的再次理解
- 组织之瓶颈
- 设置图片大小和旋转
- poj1200 Crazy Search
- eclipse自定义new菜单
- poj1028--动态规划--Ignatius and the Princess III
- Android音频系统之AudioFlinger(三)
- 移动应用开发(IOS/android等一下)在一般图像缓存方案评述(附流程图)
- poj1028--动态规划--Ignatius and the Princess III
- 黑马程序员—多线程安全问题(锁)
- Ubuntu 14.04(64位)编译android4.2.2
- 最长单词2