ZOJ 3908 Number Game ZOJ Monthly, October 2015 - F
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 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).
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的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 思维+multiset ZOJ Monthly, July 2015 - H Twelves Monkeys
题目传送门 /* 题意:n个时刻点,m次时光穿梭,告诉的起点和终点,q次询问,每次询问t时刻t之前有多少时刻点是可以通过两种不同的路径到达 思维:对于当前p时间,从现在到未来穿越到过去的是有效的值,排 ...
- ZOJ 2971 Give Me the Number;ZOJ 2311 Inglish-Number Translator (字符处理,防空行,strstr)
ZOJ 2971 Give Me the Number 题目 ZOJ 2311 Inglish-Number Translator 题目 //两者题目差不多,细节有点点不一样,因为不是一起做的,所以处 ...
- ZOJ Monthly, October 2010 ABEFI
ZOJ 3406 Another Very Easy Task #include <cstdio> #include <cstring> const int N = 10000 ...
随机推荐
- [Effective JavaScript 笔记]第42条:避免使用轻率的猴子补丁
41条对违反抽象原则行为的讨论之后,下面聊一聊终极违例.由于对象共享原型,因此每一个对象都可以增加.删除或修改原型的属性.这个有争议的实践通常称为猴子补丁. 猴子补丁示例 猴子补丁的吸引力在于其强大. ...
- JdbcTemplate三种常用回调方法
JdbcTemplate针对数据查询提供了多个重载的模板方法,你可以根据需要选用不同的模板方法. 如果你的查询很简单,仅仅是传入相应SQL或者相关参数,然后取得一个单一的结果,那么你可以选择如下一组便 ...
- :( Call to a member function Table() on a non-object 错误位置
:( Call to a member function Table() on a non-object 错误位置 $Model不是模板,是你自己先前链接数据库返回的对象...我的是改为$Form
- ■Ascii逐字解码法注入,mysql5.0一下版本手工注入
/*By:珍惜少年时*/ 逐字解码法,不一定非要猜字段内容.库名,表名,字段,data,都能猜. 环境过滤了select.union(mysql5.0以下的版本就不支持union所以也可以用此方法), ...
- APScheduler —— Python化的Cron
APScheduler全程为Advanced Python Scheduler,是一款轻量级的Python任务调度框架.它允许你像Cron那样安排定期执行的任务,并且支持Python函数或任意可调用的 ...
- Linux netstat +awk 看攻击IP
netstat -n | awk '/^tcp/ {n=split($(NF-1),array,":");if(n<=2)++S[array[(1)]];else++S[a ...
- Valid Perfect Square
Given a positive integer num, write a function which returns True if num is a perfect square else Fa ...
- iOS 一个工程中引用其他工程时编译的Architecture问题
当引用了其他工程时,在编译时报错,提示你编译指令架构不对,你需要查看一下这几个工程的Architecture部分是否又冲突,比如主工程设置Valid Architecture为armv7 而 另一个子 ...
- iOS constraint被应用于view上的时间
在viewdidload时,constraint是没有被应用的,之后在layoutSubviews时,系统应用了constraint.但是我感觉在viewWillLayoutSubviews函数时就已 ...
- java\c程序的内存分配
JAVA 文件编译执行与虚拟机(JVM)介绍 Java 虚拟机(JVM)是可运行Java代码的假想计算机.只要根据JVM规格描述将解释器移植到特定的计算机上,就能保证经过编译的任何Java代码能够在该 ...