Cake


Time Limit: 4 Seconds      Memory Limit: 65536 KB

Alice and Bob like eating cake very much. One day, Alice and Bob went to a bakery and bought many cakes.

Now we know that they have bought n cakes in the bakery. Both of them like delicious cakes, but they evaluate the cakes as different values. So they decided to divide those cakes by following method.

Alice and Bob do n / 2 steps, at each step, Alice choose 2 cakes, and Bob takes the cake that he evaluates it greater, and Alice take the rest cake.

Now Alice want to know the maximum sum of the value that she can get.

Input

The first line is an integer T which is the number of test cases.

For each test case, the first line is an integer n (1<=n<=800). Note that n is always an even integer.

In following n lines, each line contains two integers a[i] and b[i], where a[i] is the value of ith cake that Alice evaluates, and b[i] is the value of ith cake that Bob evaluates. (1<=a[i]b[i]<=1000000)

Note that a[1]a[2]..., a[n] are n distinct integers and b[1]b[2]..., b[n] are n distinct integers.

Output

For each test case, you need to output the maximum sum of the value that Alice can get in a line.

Sample Input

1
6
1 6
7 10
6 11
12 18
15 5
2 14

Sample Output

28

Author: HUA, Yiwei

题意:给出n个二元组,若选取(ai, bi),就必须去掉一个(aj,bj),并且bj比bi大,问最终取得的ai的和最大为多少

分析:按照bi排序后,即便为,若选择第i个数,则必须去掉编号大于i的一项

倒着dp,dp[i][j]表示后i位取了j个的最大和,显然j<=(n-i+1)/2

简单dp

 #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 = ;
typedef pair<int, int> II;
II Data[N];
int n, Arr[N], Dp[N][N]; 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;
//scanf("%d", &TestNumber);
TestNumber = Getint();
while(TestNumber--) {
n = Getint();
For(i, , n) {
Data[i].sd = Getint();
Data[i].ft = Getint();
}
Solve();
}
} inline void Solve() {
sort(Data+, Data++n);
For(i, , n) Arr[i] = Data[i].sd; For(i, , n)
For(j, , n) Dp[i][j] = -INF;
Dp[n][] = ;
Ford(i, n-, )
For(j, , (n-i+)/) {
Dp[i][j] = Dp[i+][j];
if(j) Dp[i][j] = max(Dp[i][j], Dp[i+][j-]+Arr[i]);
} printf("%d\n", Dp[][n/]);
} int main() {
Input();
//Solve();
return ;
}

ZOJ 3905 Cake ZOJ Monthly, October 2015 - C的更多相关文章

  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 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 ...

  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. ZOJ 3905 Cake(贪心+dp)

    动态规划题:dp[i][j]表示有i个Cake,给了Alice j个,先按照b排序,这样的话,能保证每次都能成功给Alice Cake,因为b从大到小排序,所以Alice选了j个之后,Bob最少选了j ...

  8. ZOJ 3905 Cake

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

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

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

随机推荐

  1. Android之开启手机系统自带铃声

    /** * 开启手机系统自带铃声 */ private void startAlarm() { mMediaPlayer = MediaPlayer.create(this, getSystemDef ...

  2. ubuntu apt-get update 失败解决

    在执行 sudo apt-get update 之后  会出现如下错误 这是要检测网络是否有问题 因为我之前只使用nfs挂载的时候,在虚拟机  编辑-> 虚拟网络编辑器里面->更改设置里面 ...

  3. vs2013秘钥

    Ultimate:BWG7X-J98B3-W34RT-33B3R-JVYW9 Premium:FBJVC-3CMTX-D8DVP-RTQCT-92494  YKCW6-BPFPF-BT8C9-7DCT ...

  4. python时间转换

    #设a为字符串 import time a = "2011-09-28 10:00:00" #中间过程,一般都需要将字符串转化为时间数组 time.strptime(a,'%Y-% ...

  5. myeclipse高版本对应tomcat低版本解决办法

    今天在帮同事调试程序的时候,冒出来一个异常,网上搜搜,结果如下: 将项目部署好后,启动tomcat后报错,java.lang.NoClassDefFoundError: org/apache/juli ...

  6. CodeForces - 417B (思维题)

    Crash Time Limit: 1000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u Submit Status ...

  7. Powershell 批量替换文件

    Powershell 批量替换文件 ##作者:Xiongpq ##时间:2015-06-10 18:50 ##版本:2.0 ##源文件目录 ##源文件目录的所有文件都会覆盖目标目录的同名文件,源文件目 ...

  8. [MAC] Mac下的SVN命令行

    转载自: http://www.cnblogs.com/snandy/p/4072857.html Mac自带了SVN命令行,如我的升级到10.10(OSX yosemite)后命令行版本为1.7.1 ...

  9. 菜菜买气球(codevs 2851)

    题目描述 Description 六一儿童节到了,菜菜爸爸带着菜菜来到了游乐园,菜菜可高兴坏了.这不,菜菜看到了一排卖气球的,便吵着闹着要买气球. 不过这些卖气球的也奇怪,他们都站成了一排,而且每个人 ...

  10. 一、HTML和CSS基础--HTML+CSS基础课程--第2部分

    第三章 与浏览器交互,表单标签 使用表单标签,与用户交互
网站怎样与用户进行交互?答案是使用HTML表单(form).表单是可以把浏览者输入的数据传送到服务器端,这样服务器端程序就可以处理表单传过来的 ...