ZOJ 3905 Cake ZOJ Monthly, October 2015 - C

本文探讨了一种蛋糕分配算法,通过动态规划解决Alice和Bob如何公平分配蛋糕的问题,旨在最大化Alice所得蛋糕价值总和。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

 

转载于:https://www.cnblogs.com/StupidBoy/p/4883800.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值