Number Game Time limit: 2 Seconds Memory Limit: 65536 KB
The bored Bob is playing a number game. In the beginning, there is n numbers. For each turn, Bob would take out both numbers from the remaining numbers, and get the product of them. There is a condition, the sum of the numbers must be not larger than k .
Now, Bob was 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 is n numbers ai (0≤ ai ≤ 10000, 1≤ i ≤ n ).
Output
For each test case, the output of the maximum sum of a products Bob can get.
Sample Input
24 2 71 3 2 43 2 32 3 1
Sample Output
142
Test instructions: Is said to give the number of N, give M,k, ask the most select M times, each selected two number, a, make a+b<=k, remember the integral a*b, ask all a*b and the maximum of how much
Analysis: is an obvious greed
Obviously for each number, B, if there is an A that makes a+b<=k, and this A is the largest, then the two numbers must either be selected, or none-"that is, for each number, it matches the number is certain
Find all A, B, then greedy
1#include <cstdio>2#include <cstring>3#include <cstdlib>4#include <cmath>5#include <ctime>6#include <iostream>7#include <algorithm>8#include <map>9#include <Set>Ten#include <vector> One#include <deque> A#include <queue> - using namespacestd; -typedefLong LongLL; thetypedefDoubleDB; - #defineRep (i, n) for (int i = (0); I < (n); i++) - #defineREPN (i, n) for (int i = (n)-1; I >= 0; i--) - #defineFor (I, S, T) for (int i = (s); I <= (t); i++) + #defineFord (I, T, s) for (int i = (t); I >= (s); i--) - #defineRep (I, S, T) for (int i = (s); I < (t); i++) + #defineREPN (I, S, T) for (int i = (s)-1; I >= (t); i--) A #defineMIT (2147483647) at #defineMLL (1000000000000000000LL) - #defineINF (1000000001) - #defineMk Make_pair - #defineFT first - #defineSD Second - #defineCLR (x, y) (memset (x, y, sizeof (x))) in #defineSQR (x) ((x) * (x)) - #defineSZ (x) ((int) (x). Size ()) to #definePUF Push_front + #definePub push_back - #definePOF Pop_front the #definePOB pop_back *InlinevoidSetio (stringName) { $ stringInput = name+". in", Output = name+". out";Panax NotoginsengFreopen (Input.c_str (),"R", stdin); -Freopen (Output.c_str (),"W", stdout); the } + A Const intN =100010; the intTestnumber; + intN, M, K; - intArr[n]; $ intAnswer[n], Len; $ LL Ans; - BOOLVisit[n]; -multiset<int>splay; the -InlineintGetint () {Wuyi intRet =0; the CharCh =' '; - while(! (Ch >='0'&& Ch <='9')) Ch =GetChar (); Wu while(Ch >='0'&& Ch <='9') { -Ret = ret*Ten+ch-'0'; AboutCh =GetChar (); $ } - returnRet; - } - AInlinevoidSolve (); + theInlinevoidInput () { - intTestnumber; $Testnumber =Getint (); the while(testnumber--) { then =Getint (); them =Getint (); theK =Getint (); -for (I,1, n) arr[i] =Getint (); in Solve (); the } the } About theInlineBOOLInSet (intx) { the Set<int>:: iterator It; theIt =splay.lower_bound (x); + if(It = = Splay.end ())return 0; - if((*it)! = x)return 0; the return 1;Bayi } the theInlineintFind (intLimit) { - Set<int>:: iterator It; -It =Splay.upper_bound (Limit); the if(It = = Splay.begin ())return-1; theit--; the intRet = *It; the splay.erase (It); - returnRet; the } the theInlinevoidSolve () {94Sort (arr+1, arr+1+n); the if(arr[1] >=k) { theprintf"0\n"); the return;98 } About - while(N >1&& arr[n]+arr[1] > K) n--;101 102Len =0;103 splay.clear ();104for (I,1, N) Splay.insert (Arr[i]); the 106Ford (i, N,1) {107 if(! InSet (Arr[i]))Continue;108 intLimit = kArr[i];109 Find (Arr[i]); the intValue =Find (Limit);111 if(Value <0)Continue; theAnswer[++len] = arr[i]*Value;113 } the theSort (answer+1, answer+1+Len); them =min (M, Len);117 118Ans =0;119Ford (i, Len, len-m+1) Ans + =Answer[i]; -cout<<ans<<Endl;121 }122 123 intMain () {124 Input (); the //Solve ();126 return 0;127}
View Code
ZOJ 3908 number Game ZOJ Monthly, October 2015-f