标签:
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?
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 are n numbers ai(0≤ ai ≤10000, 1≤ i ≤n).
For each test case, output the maximum sum of products Bob can get.
2 4 2 7 1 3 2 4 3 2 3 2 3 1
14 2
题意:就是说给出n个数,给出m,k,问最多选m次,每次选两个数a,b,使得a+b<=k,记有积分a*b,问所有a*b的和最大为多少
分析:就是一个显然的贪心
显然对于每个数,b,如果存在一个a使得a+b<=k,并且这个a最大,那么这两个数就一定要么都选,要么都不选-》也就是说,对于每个数,与它匹配的数是一定的
找出所有的a,b,然后贪心即可
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> 10 #include <vector> 11 #include <deque> 12 #include <queue> 13 using namespace std; 14 typedef long long LL; 15 typedef double DB; 16 #define Rep(i, n) for(int i = (0); i < (n); i++) 17 #define Repn(i, n) for(int i = (n)-1; i >= 0; i--) 18 #define For(i, s, t) for(int i = (s); i <= (t); i++) 19 #define Ford(i, t, s) for(int i = (t); i >= (s); i--) 20 #define rep(i, s, t) for(int i = (s); i < (t); i++) 21 #define repn(i, s, t) for(int i = (s)-1; i >= (t); i--) 22 #define MIT (2147483647) 23 #define MLL (1000000000000000000LL) 24 #define INF (1000000001) 25 #define mk make_pair 26 #define ft first 27 #define sd second 28 #define clr(x, y) (memset(x, y, sizeof(x))) 29 #define sqr(x) ((x)*(x)) 30 #define sz(x) ((int) (x).size()) 31 #define puf push_front 32 #define pub push_back 33 #define pof pop_front 34 #define pob pop_back 35 inline void SetIO(string Name) { 36 string Input = Name+".in", Output = Name+".out"; 37 freopen(Input.c_str(), "r", stdin); 38 freopen(Output.c_str(), "w", stdout); 39 } 40 41 const int N = 100010; 42 int TestNumber; 43 int n, m, k; 44 int Arr[N]; 45 int Answer[N], Len; 46 LL Ans; 47 bool Visit[N]; 48 multiset<int> Splay; 49 50 inline int Getint() { 51 int Ret = 0; 52 char Ch = ‘ ‘; 53 while(!(Ch >= ‘0‘ && Ch <= ‘9‘)) Ch = getchar(); 54 while(Ch >= ‘0‘ && Ch <= ‘9‘) { 55 Ret = Ret*10+Ch-‘0‘; 56 Ch = getchar(); 57 } 58 return Ret; 59 } 60 61 inline void Solve(); 62 63 inline void Input() { 64 int TestNumber; 65 TestNumber = Getint(); 66 while(TestNumber--) { 67 n = Getint(); 68 m = Getint(); 69 k = Getint(); 70 For(i, 1, n) Arr[i] = Getint(); 71 Solve(); 72 } 73 } 74 75 inline bool InSet(int x) { 76 set<int>::iterator It; 77 It = Splay.lower_bound(x); 78 if(It == Splay.end()) return 0; 79 if((*It) != x) return 0; 80 return 1; 81 } 82 83 inline int Find(int Limit) { 84 set<int>::iterator It; 85 It = Splay.upper_bound(Limit); 86 if(It == Splay.begin()) return -1; 87 It--; 88 int Ret = *It; 89 Splay.erase(It); 90 return Ret; 91 } 92 93 inline void Solve() { 94 sort(Arr+1, Arr+1+n); 95 if(Arr[1] >= k) { 96 printf("0\n"); 97 return; 98 } 99 100 while(n > 1 && Arr[n]+Arr[1] > k) n--; 101 102 Len = 0; 103 Splay.clear(); 104 For(i, 1, n) Splay.insert(Arr[i]); 105 106 Ford(i, n, 1) { 107 if(!InSet(Arr[i])) continue; 108 int Limit = k-Arr[i]; 109 Find(Arr[i]); 110 int Value = Find(Limit); 111 if(Value < 0) continue; 112 Answer[++Len] = Arr[i]*Value; 113 } 114 115 sort(Answer+1, Answer+1+Len); 116 m = min(m, Len); 117 118 Ans = 0; 119 Ford(i, Len, Len-m+1) Ans += Answer[i]; 120 cout<<Ans<<endl; 121 } 122 123 int main() { 124 Input(); 125 //Solve(); 126 return 0; 127 }
ZOJ 3908 Number Game ZOJ Monthly, October 2015 - F
标签:
原文地址:http://www.cnblogs.com/StupidBoy/p/4883804.html