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. Maya导入Unity的教程

    原地址:http://www.cocoachina.com/gamedev/gameengine/2010/0601/1586.html 昨天已经发布了1Vr.Cn翻译的多维材质模型烘培入Unity  ...

  2. Linux常用指令(持续更新)

    (这些文章都是从我的个人主页上粘贴过来的,大家也可以访问我的主页 www.iwangzheng.com) PP真的是一位很有姿势的程序猿,有这样的邻居真好,榜样啊. pwd 当前路径 df  -kh ...

  3. BNUOJ 1037 精神控制

    XsuagrX喜欢到处唬人,各种唬.这不,经过刻苦修炼,他终于掌握了Bane Element的Ultra绝技加强版,恶魔掌控(快捷键F)(YY中&……).当XsugarX对某个人胡言乱语Q@# ...

  4. 反转字符串--C和Python

    将字符串反转,即“abcde”->"edcba" C语言实现: [转自http://www.kanzhun.com/mianshiti/456.html?sid=mail_1 ...

  5. msysgit ls 中文显示

    2013年10月17日 14:54:15 安装了新版的msysgit后,在其自带的 git bash 命令行下就可以输入中文汉字了 但是创建了中文名字命名的文件后,再用 ls 命令查询时会出现乱码的情 ...

  6. 解决虚拟机 正在决定eht0 的ip信息失败 无链接-- 虚拟机上linux redhat 上网问题

    对于虚拟机上,linux redhat上网的配置方式有三种 一.用setup命令进行配置(具体技巧可查setup命令的使用) 二.直接用 ifconfig eth0  ip地址进行配置 三.进入系统文 ...

  7. php抽象类的简单应用

    抽象类也是面向对象中的重要概念,和接口.继承的概念重要性相当,在面向对象的开发中,所有的对象都是通过类来描述的,但是反过来,并不是所有类都是用来描绘对象的,广义上讲如果一个类中没有足够信息来描述一个具 ...

  8. Java for LeetCode 023 Merge k Sorted Lists

    Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 解 ...

  9. oracle的数据库,随笔

    不多说,看代码 select b.*,a.kscj,a.paiming from (select t.kch,t.kcm,t.kscj,t.xh,        rank() over (order ...

  10. 虚拟机下安装ubuntu后root密码设置

    ctrl+alt+t:调出命令行. 问题描述: 在虚拟机下安装了ubuntu中要输入用户名,一般情况下大家都会输入一个自己的网名或绰号之类的,密码也在这时设置过了. 但是当安装成功之后,使用命令#su ...