zoukankan      html  css  js  c++  java
  • ZOJ 3905 Cake

    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

    解题:$动态规划,dp[i][j]表示有i个cake,选了j个给Bob,剩下的给Alice,由于预先按每块cake的b属性由高到低排序$

    $所以,可以保证每次加入的Cake 如果是给Alice的一定可以成功的给Alice,因i里面有j个的cake的b属性比当前cake的b属性大$

     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 const int maxn = 810;
     4 typedef long long LL;
     5 struct Cake{
     6     int a,b;
     7     bool operator<(const Cake &rhs)const{
     8         return b > rhs.b;
     9     }
    10 }cake[maxn];
    11 LL dp[2][maxn];
    12 
    13 int main(){
    14     int kase,n;
    15     scanf("%d",&kase);
    16     while(kase--){
    17         scanf("%d",&n);
    18         for(int i = 1; i <= n; ++i)
    19             scanf("%d%d",&cake[i].a,&cake[i].b);
    20         sort(cake + 1,cake + n + 1);
    21         memset(dp,0,sizeof dp);
    22         int cur = 0;
    23         for(int i = 1; i <= n; ++i){
    24             for(int j = ((i-1)>>1); j >= 0; --j){
    25                 dp[cur^1][i-j] = max(dp[cur^1][i-j],dp[cur][i-1-j]);
    26                 dp[cur^1][i-j-1] = max(dp[cur^1][i-j-1],dp[cur][i-1-j] + cake[i].a);
    27             }
    28             memset(dp[cur],0,sizeof dp[cur]);
    29             cur ^= 1;
    30         }
    31         printf("%lld
    ",dp[cur][n>>1]);
    32     }
    33     return 0;
    34 }
    View Code
  • 相关阅读:
    按行打印二叉树结点值
    Kafka消息队列(继续深挖)
    【Java IO模式】Java BIO NIO AIO总结
    JAVA线程池的执行过程
    CentOS7升级版本
    【转】KVM中打开virt-manager报错或者将其显示为中文界面的办法
    【转】linux kvm虚拟机配置及常见问题处理
    【转】关于Quartus ii无法识别Modelsim路径的问题
    【转】[git]error: pack-objects died of signal
    storm kafka整合
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/4869708.html
Copyright © 2011-2022 走看看