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. js实现文本框限制输入数字和小数点--兼容多个浏览器

    <html> <head> <meta http-equiv="content-Type" content="text/html;chars ...

  2. 百度图片爬虫-python版-如何爬取百度图片?

    上一篇我写了如何爬取百度网盘的爬虫,在这里还是重温一下,把链接附上: http://www.cnblogs.com/huangxie/p/5473273.html 这一篇我想写写如何爬取百度图片的爬虫 ...

  3. python4delphi import lxml pandas 出错的小结

    环境: 1.win10 64位 2.delphi xe8 3.python2.7 4.python4delphi  (svn 2015-03-21 发布的83版本号) 5.lxml 3.4.4(通过p ...

  4. Linux命令之exit - 退出当前shell【返回值状态】

    原文链接:http://codingstandards.iteye.com/blog/836625   (转载请注明出处) 用途说明 exit命令用于退出当前shell,在shell脚本中可以终止当前 ...

  5. macbook air 安装win7双系统

    转自: http://jingyan.baidu.com/article/6d704a13f99f1a28da51ca49.html 1)遇到“No bootable device-insert bo ...

  6. mysql中的unsigned

    unsigned   既为非负数,用此类型可以增加数据长度! 例如如果    tinyint最大是127,那    tinyint    unsigned    最大   就可以到    127 * ...

  7. object-c 继承多态 动态数据类型

    在c#中我们知道有继承的.同样在object-c中也有继承. 例如我们写一个人类(父),一个学生类.我们可以这么写: demo: @interface Person:NSobject{ NSStrin ...

  8. Android 中的selector

    今天做程序时,发现了selector 选择器不单单能用系统的自定义属性(比如,  <item android:state_selected="true" android:co ...

  9. JavaScript或jQuery模拟点击超链接和按钮

    有时候我们需要页面自动点击超链接或者按钮,可以用js或者jQuery利用程序去点击,方法很简单,按钮或超链接代码如下: <a href="url" target=" ...

  10. 基于Delphi的三层数据库系统的实现方法

    基于Delphi的三层数据库系统的实现方法   1  引言 当前的数据库应用系统中,按其结构划分为两类,一类是两层结构的数据库应系统,另一类是多层结构的数据库应用系统. 两层结构的数据库应用系统包括客 ...