ZOJ 3908 Number Game ZOJ Monthly, October 2015 - F

时间:2022-06-12 17:50:08

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 题意:就是说给出n个数,给出m,k,问最多选m次,每次选两个数a,b,使得a+b<=k,记有积分a*b,问所有a*b的和最大为多少
分析:就是一个显然的贪心
显然对于每个数,b,如果存在一个a使得a+b<=k,并且这个a最大,那么这两个数就一定要么都选,要么都不选-》也就是说,对于每个数,与它匹配的数是一定的
找出所有的a,b,然后贪心即可
 #include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <vector>
#include <deque>
#include <queue>
using namespace std;
typedef long long LL;
typedef double DB;
#define Rep(i, n) for(int i = (0); i < (n); i++)
#define Repn(i, n) for(int i = (n)-1; i >= 0; i--)
#define For(i, s, t) for(int i = (s); i <= (t); i++)
#define Ford(i, t, s) for(int i = (t); i >= (s); i--)
#define rep(i, s, t) for(int i = (s); i < (t); i++)
#define repn(i, s, t) for(int i = (s)-1; i >= (t); i--)
#define MIT (2147483647)
#define MLL (1000000000000000000LL)
#define INF (1000000001)
#define mk make_pair
#define ft first
#define sd second
#define clr(x, y) (memset(x, y, sizeof(x)))
#define sqr(x) ((x)*(x))
#define sz(x) ((int) (x).size())
#define puf push_front
#define pub push_back
#define pof pop_front
#define pob pop_back
inline void SetIO(string Name) {
string Input = Name+".in", Output = Name+".out";
freopen(Input.c_str(), "r", stdin);
freopen(Output.c_str(), "w", stdout);
} const int N = ;
int TestNumber;
int n, m, k;
int Arr[N];
int Answer[N], Len;
LL Ans;
bool Visit[N];
multiset<int> Splay; inline int Getint() {
int Ret = ;
char Ch = ' ';
while(!(Ch >= '' && Ch <= '')) Ch = getchar();
while(Ch >= '' && Ch <= '') {
Ret = Ret*+Ch-'';
Ch = getchar();
}
return Ret;
} inline void Solve(); inline void Input() {
int TestNumber;
TestNumber = Getint();
while(TestNumber--) {
n = Getint();
m = Getint();
k = Getint();
For(i, , n) Arr[i] = Getint();
Solve();
}
} inline bool InSet(int x) {
set<int>::iterator It;
It = Splay.lower_bound(x);
if(It == Splay.end()) return ;
if((*It) != x) return ;
return ;
} inline int Find(int Limit) {
set<int>::iterator It;
It = Splay.upper_bound(Limit);
if(It == Splay.begin()) return -;
It--;
int Ret = *It;
Splay.erase(It);
return Ret;
} inline void Solve() {
sort(Arr+, Arr++n);
if(Arr[] >= k) {
printf("0\n");
return;
} while(n > && Arr[n]+Arr[] > k) n--; Len = ;
Splay.clear();
For(i, , n) Splay.insert(Arr[i]); Ford(i, n, ) {
if(!InSet(Arr[i])) continue;
int Limit = k-Arr[i];
Find(Arr[i]);
int Value = Find(Limit);
if(Value < ) continue;
Answer[++Len] = Arr[i]*Value;
} sort(Answer+, Answer++Len);
m = min(m, Len); Ans = ;
Ford(i, Len, Len-m+) Ans += Answer[i];
cout<<Ans<<endl;
} int main() {
Input();
//Solve();
return ;
}