ZOJ 3908 Number Game

本文介绍了一个数字游戏,玩家需要从一组数字中选择两个数相乘,且这两个数的和不超过限制值k,目标是在限定回合内获得最大乘积之和。文章提供了一种策略:始终选择当前条件下最大的两个数进行配对,并给出了实现这一策略的C++代码。

摘要生成于 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


/*
思路:对于一个数,每次肯定取满足条件的最大的数和他乘,然后取前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;
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值