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

本文深入探讨了游戏开发领域的关键技术,包括游戏引擎、编程语言、硬件优化等,并重点阐述了AI音视频处理的应用场景和实现方法,如语义识别、语音识别、AR增强现实等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Number Game

Time Limit: 2 Seconds      Memory Limit: 65536 KB

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 nm(0≤ nm ≤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);
            }
            else
            break;
        }
        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;
}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值