ZOJ 3908 Number Game(乱搞)

来源:互联网 发布:如何关闭阿里云 编辑:程序博客网 时间:2024/05/18 19:38

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

24 2 71 3 2 43 2 32 3 1

Sample Output

142

Author: XIAN, Weizh

要存下来sort一下选前m大的multiset乱搞

#include<cstdio>#include<cstring>#include<algorithm>#include<vector>#include<string>#include<iostream>#include<queue>#include<cmath>#include<map>#include<stack>#include<set>using namespace std;#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )#define CLEAR( a , x ) memset ( a , x , sizeof a )const int INF=0x3f3f3f3f;typedef long long LL;const int maxn=(1e5+100)*2;int ans[maxn];multiset<int>S;multiset<int>::iterator it;int t,n,m,k;int main(){    int x;    scanf("%d",&t);    while(t--)    {        S.clear();        scanf("%d%d%d",&n,&m,&k);        for(int i=1;i<=n;i++)        {            scanf("%d",&x);            if(x>0&&x<k)                S.insert(x);        }        int tot=0;        while(1)        {            if(S.size()<2)                break;            it=S.end();it--;            S.erase(it);            int x=*it;            it=S.upper_bound(k-x);            if(it==S.begin()) continue;            it--;            ans[tot++]=1LL*(*it)*x;            S.erase(it);        }        sort(ans,ans+tot);        int pos=tot-1;        LL sum=0;        while(m>0&&pos>=0)        {            m--;            sum+=ans[pos--];        }        printf("%lld\n",sum);    }    return 0;}


0 0
原创粉丝点击