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 ;
}
												

ZOJ 3908 Number Game ZOJ Monthly, October 2015 - F的更多相关文章

  1. ZOJ 3913 Bob wants to pour water ZOJ Monthly, October 2015 - H

    Bob wants to pour water Time Limit: 2 Seconds      Memory Limit: 65536 KB      Special Judge There i ...

  2. ZOJ 3911 Prime Query ZOJ Monthly, October 2015 - I

    Prime Query Time Limit: 1 Second      Memory Limit: 196608 KB You are given a simple task. Given a s ...

  3. ZOJ 3910 Market ZOJ Monthly, October 2015 - H

    Market Time Limit: 2 Seconds      Memory Limit: 65536 KB There's a fruit market in Byteland. The sal ...

  4. ZOJ 3905 Cake ZOJ Monthly, October 2015 - C

    Cake Time Limit: 4 Seconds      Memory Limit: 65536 KB Alice and Bob like eating cake very much. One ...

  5. ZOJ 3903 Ant ZOJ Monthly, October 2015 - A

    Ant Time Limit: 1 Second      Memory Limit: 32768 KB There is an ant named Alice. Alice likes going ...

  6. 143 - ZOJ Monthly, October 2015 I Prime Query 线段树

    Prime Query Time Limit: 1 Second      Memory Limit: 196608 KB You are given a simple task. Given a s ...

  7. 思维+multiset ZOJ Monthly, July 2015 - H Twelves Monkeys

    题目传送门 /* 题意:n个时刻点,m次时光穿梭,告诉的起点和终点,q次询问,每次询问t时刻t之前有多少时刻点是可以通过两种不同的路径到达 思维:对于当前p时间,从现在到未来穿越到过去的是有效的值,排 ...

  8. ZOJ 2971 Give Me the Number;ZOJ 2311 Inglish-Number Translator (字符处理,防空行,strstr)

    ZOJ 2971 Give Me the Number 题目 ZOJ 2311 Inglish-Number Translator 题目 //两者题目差不多,细节有点点不一样,因为不是一起做的,所以处 ...

  9. ZOJ Monthly, October 2010 ABEFI

    ZOJ 3406 Another Very Easy Task #include <cstdio> #include <cstring> const int N = 10000 ...

随机推荐

  1. java笔记--关于int和byte[]的转换

    关于int和byte[]数组的转换 --如果朋友您想转载本文章请注明转载地址"http://www.cnblogs.com/XHJT/p/3891747.html "谢谢-- 众所 ...

  2. Activity切换后,如i何保存上一个Activit的状态

    在Activity切换中一般有三种方式保存上一个Activity的状态数据.一.全局变量    public static int type = 0;二.SharedPreference      保 ...

  3. [BZOJ4530][Bjoi2014]大融合 LCT + 启发式合并

    [BZOJ4530][Bjoi2014]大融合 试题描述 小强要在N个孤立的星球上建立起一套通信系统.这套通信系统就是连接N个点的一个树. 这个树的边是一条一条添加上去的.在某个时刻,一条边的负载就是 ...

  4. 解决eclipse+MAVEN提示One or more constraints have not been satisfied.的问题

    应用版本:eclipse luna4.4.1 JDK:1.8 Maven:3.2.5 问题现象: 1.编译工程后总该是显示下面两个错误: One or more constraints have no ...

  5. TCP/IP协议原理【转载】

    前述        各种L2数据网具有不同的通信协议与帧结构,其网络节点设备可以是各种类型的数据交换机(X.25.FR.Ethernet和ATM等分组交换机):而L3数据网(IP网或internet) ...

  6. top对僵尸进程的处理

    ps        --forest        ASCII art process tree 2 怎样来清除僵尸进程:    1.改写父进程,在子进程死后要为它收尸.具体做法是接管SIGCHLD信 ...

  7. ubuntu14.04安装chrome

    到https://www.google.com/chrome/browser/desktop/index.html可下载指定版本的deb文件. 32bit: wget https://dl.googl ...

  8. Rotate String

    Given a string and an offset, rotate string by offset. (rotate from left to right) Example Given &qu ...

  9. tesseract3.02识别验证码需要注意的问题

    1.安装tesseract3.02后,在命令行里输入tesseract,看能否出现使用方法,不出现则是环境变量问题,可调整其顺序. 2.找到如下文件 C:\Python27\Lib\site-pack ...

  10. MySQL数据库备份和还原的常用命令

    其实很多情况下mysql备份就是采用了这些命令,例如: mysql导入和导出数据 linux自动定时备份web程序和mysql数据库 备份MySQL数据库的命令 mysqldump -hhostnam ...