码迷,mamicode.com
首页 > 其他好文 > 详细

ZOJ 3905 Ant ZOJ Monthly, October 2015 - C

时间:2015-10-15 23:24:37      阅读:264      评论:0      收藏:0      [点我收藏+]

标签:

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

技术分享
 1 #include <cstdio>
 2 #include <cstring>
 3 #include <cstdlib>
 4 #include <cmath>
 5 #include <ctime>
 6 #include <iostream>
 7 #include <algorithm>
 8 #include <map>
 9 #include <set>
10 #include <vector>
11 #include <deque>
12 #include <queue>
13 using namespace std;
14 typedef long long LL;
15 typedef double DB;
16 #define Rep(i, n) for(int i = (0); i < (n); i++)
17 #define Repn(i, n) for(int i = (n)-1; i >= 0; i--)
18 #define For(i, s, t) for(int i = (s); i <= (t); i++)
19 #define Ford(i, t, s) for(int i = (t); i >= (s); i--)
20 #define rep(i, s, t) for(int i = (s); i < (t); i++)
21 #define repn(i, s, t) for(int i = (s)-1; i >= (t); i--)
22 #define MIT (2147483647)
23 #define MLL (1000000000000000000LL)
24 #define INF (1000000001)
25 #define mk make_pair
26 #define ft first
27 #define sd second
28 #define clr(x, y) (memset(x, y, sizeof(x)))
29 #define sqr(x) ((x)*(x))
30 #define sz(x) ((int) (x).size())
31 #define puf push_front
32 #define pub push_back
33 #define pof pop_front
34 #define pob pop_back
35 inline void SetIO(string Name) {
36     string Input = Name+".in", Output = Name+".out";
37     freopen(Input.c_str(), "r", stdin);
38     freopen(Output.c_str(), "w", stdout);
39 }
40 
41 const int N = 810;
42 typedef pair<int, int> II;
43 II Data[N];
44 int n, Arr[N], Dp[N][N];
45 
46 inline int Getint() {
47     int Ret = 0;
48     char Ch =  ;
49     while(!(Ch >= 0 && Ch <= 9)) Ch = getchar();
50     while(Ch >= 0 && Ch <= 9) {
51         Ret = Ret*10+Ch-0;
52         Ch = getchar();
53     }
54     return Ret;
55 }
56 
57 inline void Solve();
58 
59 inline void Input() {
60     int TestNumber;
61     //scanf("%d", &TestNumber);
62     TestNumber = Getint();
63     while(TestNumber--) {
64         n = Getint();
65         For(i, 1, n) {
66             Data[i].sd = Getint();
67             Data[i].ft = Getint();
68         }
69         Solve();
70     }
71 }
72 
73 inline void Solve() {
74     sort(Data+1, Data+1+n);
75     For(i, 1, n) Arr[i] = Data[i].sd;
76     
77     For(i, 1, n)
78         For(j, 1, n) Dp[i][j] = -INF;
79     Dp[n][0] = 0;
80     Ford(i, n-1, 1)
81         For(j, 0, (n-i+1)/2) {
82             Dp[i][j] = Dp[i+1][j];
83             if(j) Dp[i][j] = max(Dp[i][j], Dp[i+1][j-1]+Arr[i]);
84         }
85     
86     printf("%d\n", Dp[1][n/2]);
87 }
88 
89 int main() {
90      Input();
91      //Solve();
92     return 0;
93 }
View Code

 

ZOJ 3905 Ant ZOJ Monthly, October 2015 - C

标签:

原文地址:http://www.cnblogs.com/StupidBoy/p/4883800.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!