zoukankan      html  css  js  c++  java
  • sgu 104 Little Shop of Flowers

        经典dp问题,花店橱窗布置,不再多说,上代码

    #include <cstdio>
    #include <cstring>
    #include <iostream>
    #include <cstdlib>
    #include <algorithm>
    #define N 150
    #define inf 0x7f7f7f7f
    using namespace std;
    
    int n, m;
    int val[N][N], f[N][N];
    int fa[N][N];
    
    void print(int now, int place)
    {
        if (now == 1) printf("%d ", place);
        else
        {
            print(now-1, fa[now][place]);
            if (now != n) printf("%d ", place);
            else printf("%d
    ", place);
        }
    }
    
    int main()
    {
        scanf("%d%d", &n, &m);
        memset(val, 0, sizeof(val));
        memset(f, -0x7f, sizeof(f));
        for (int i = 0; i <= m; ++i) f[0][i] = 0;
        for (int i = 1; i <= n; ++i)
            for (int j = 1; j <= m; ++j)
                scanf("%d", &val[i][j]);
        for (int i = 1; i <= n; ++i)
            for (int j = i; j <= m-n+i; ++j)
                for (int k = i-1; k < j; ++k)
                    if (f[i][j] < f[i-1][k] + val[i][j])
                    {
                        f[i][j] = f[i-1][k] + val[i][j];
                        fa[i][j] = k;
                    }
        int ans, place;
        ans = -inf;
        for (int i = n; i <= m; ++i)
            if (ans < f[n][i])
            {
                ans = f[n][i];
                place = i;
            }
        printf("%d
    ", ans);
        print(n, place);
    }
  • 相关阅读:
    MongoDB
    新公司管理经营
    剪辑理论
    色彩理论
    商业模式
    电影手法
    [好文转载] 每个程序员都应该学习使用Python或Ruby
    File类递归
    【CF1447】div2复盘
    【心得体会】11.9-11.15
  • 原文地址:https://www.cnblogs.com/handsomeJian/p/3731388.html
Copyright © 2011-2022 走看看