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 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的更多相关文章
- 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 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 ...
- 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 ...
- ZOJ 3905 Cake(贪心+dp)
动态规划题:dp[i][j]表示有i个Cake,给了Alice j个,先按照b排序,这样的话,能保证每次都能成功给Alice Cake,因为b从大到小排序,所以Alice选了j个之后,Bob最少选了j ...
- ZOJ 3905 Cake
Cake Time Limit: 4 Seconds Memory Limit: 65536 KB Alice and Bob like eating cake very much. One ...
- 思维+multiset ZOJ Monthly, July 2015 - H Twelves Monkeys
题目传送门 /* 题意:n个时刻点,m次时光穿梭,告诉的起点和终点,q次询问,每次询问t时刻t之前有多少时刻点是可以通过两种不同的路径到达 思维:对于当前p时间,从现在到未来穿越到过去的是有效的值,排 ...
随机推荐
- Microsoft.ReportViewer.WebForms, Version=10.0.0.0的报错问题,解决方案
未能加载文件或程序集,或者web.config报错! 已解决:直接找到(默认在 路径/Microsoft Visual Studio 8/ReportViewer).把里面的3个DLL传上去就OK了! ...
- 淘宝(阿里百川)手机客户端开发日记第八篇 Handler的使用方法
首先,我们先看下API文档的说明: A Handler allows you to send and process Message and Runnable objects associated w ...
- asp.net日志跟踪方法
1. 页面级的配置 要在页面级启用跟踪功能,就要在@Page指令中设置Trace属性.如下所示: *************************************************** ...
- selenium webdriver的各种driver
selenium官方加上第三方宣布支持的驱动有很多种:除了PC端的浏览器之外,还支持iphone.Android的driver:大概记录一下selenium支持的各种driver的用途与说明. sel ...
- table动态添加删除一行和改变标题
<style type="text/css"> body{ font-size:13px; line-height:25px; } table{ border-top: ...
- Scanner 和 String 类的常用方法
Scanner类是在jdk1.5 之后有了这个: 常用格式是: Scanner sc = new Scanner(System.in); 从以下版本开始: 1.5 构造方法摘要 Scanner(Fil ...
- #ifdef 的技巧用法
-- int _tmain(int argc, _TCHAR* argv[]) { #ifdef DEBUG cout<<"DEBUG has been defined" ...
- ndk编译protobuf库
ndk_r9编译通过,里面带了自动生成代码的脚本(tool/createPBFile.bat). 下载地址
- php接口和多态的概念以及简单应用
接口是面向对象中的一个重要特性,也是面向对象开发不可缺少的一个概念,下面简单说一下接口的概念,先看一段简单的代码: interface ICanEat { public function eat($f ...
- Java for LeetCode 054 Spiral Matrix
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...