zoukankan      html  css  js  c++  java
  • ZOJ 3905 Cake(贪心+dp)

    动态规划题:dp[i][j]表示有i个Cake,给了Alice j个,先按照b排序,这样的话,能保证每次都能成功给Alice Cake,因为b从大到小排序,所以Alice选了j个之后,Bob最少选了j个,所以i>=2*j, 并且每次Alice选的时候Bob已经选过了。所以当i>=2 * j的时候Alice一定能选. 所以dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - 1] + ary[i].a);

    dp[i - 1][j]表示Alice不选第i个,dp[i - 1][j - 1] + ary[i].a表示Alice选第i个。

    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    
    using namespace std;
    const int maxn = 888;
    int dp[maxn][maxn];
    struct Node {
        int a, b;
        bool operator < (const Node &d) const
        {
            return b > d.b;
        }
    }ary[maxn];
    int main()
    {
        int T, n;
        scanf("%d", &T);
        while (T--)
        {
            scanf("%d", &n);
            for (int i = 1; i <= n; i++)
                scanf("%d %d", &ary[i].a, &ary[i].b);
            sort(ary + 1, ary + n + 1);
            memset(dp, 0, sizeof(dp));
            for (int i = 1; i <= n; i++)
            {
                for (int j = 1; j <= (i / 2); j++)
                {
                    dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - 1] + ary[i].a);
                }
            }
            printf("%d
    ", dp[n][n / 2]);
        }
        return 0;
    }
  • 相关阅读:
    centos 部署.NET CORE
    nginx 负载均衡
    graylog centos7 部署
    springboot 2.x centos 7.0 部署
    HashMap源代码阅读理解
    服务器安装redis
    java ---- gradle
    uboot-makefile总览
    makeFile
    Spring 推断构造方法
  • 原文地址:https://www.cnblogs.com/Howe-Young/p/4935056.html
Copyright © 2011-2022 走看看