您的位置:首页 > 其它

OJ_3950_Cake

2016-03-22 18:33 281 查看
CakeTime Limit: 4 Seconds     Memory Limit: 65536 KB提示:贪心思想+dpAlice 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 thatn is always an even integer.In following n lines, each line contains two integers a[i] andb[i], where a[i] is the value of ith cake that Alice evaluates, andb[i] is the value of ith cake that Bob evaluates. (1<=a[i],b[i]<=1000000)Note that a[1], a[2]..., aare n distinct integers andb[1], b[2]..., bare 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
#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<string>#include<set>#include<cmath>#include<stack>#include<map>using namespace std;const int N=805;int dp;struct node{int A,B;bool operator<(const node& X)const{return B>X.B;}}que;int main(){int t;int n;scanf("%d",&t);while(t--){scanf("%d",&n);for(int i=1;i<=n;i++){scanf("%d%d",&que[i].A,&que[i].B);}memset(dp,0,sizeof(dp));sort(que+1,que+1+n);for(int i=1;i<=n;i++){for(int j=1;j<=n;j++){dp[i][j]=dp[i-1][j];if(i/2>=j)dp[i][j]=max(dp[i][j],dp[i-1][j-1]+que[i].A);}}printf("%d\n",dp[n>>1]);}return 0;}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: