zoukankan      html  css  js  c++  java
  • HDU1712 分组背包 xingxing在努力

      这个题的意思是有一个人有N门课程,M天的复习时间, 现在给你他复习第i门课程j天的收益A[i][j],请你求出复习M天的最大收益, 考虑每一门课程可以复习0 1 2 -- M天,且他们是互斥的,那么就可以用分组背包解决这个问题,代码如下:

    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    
    using namespace std;
    const int maxn = 100 + 10;
    const int inf = 0x3f3f3f3f;
    int f[maxn];    //复习前k组用的时间是j时的最大收益
    int A[maxn][maxn];
    int N, M;                //N门课  M天
    
    int main()
    {
        while(scanf("%d%d", &N, &M) == 2)
        {
            if(N==0 && M==0) break;
            for(int i=1; i<=N; i++)
                for(int j=1; j<=M; j++)
                    scanf("%d", &A[i][j]);
            //for(int i=0; i<=M; i++)
            //    f[i] = -inf;
            //f[0] = 0;
            memset(f, 0, sizeof(f));
            for(int i=1; i<=N; i++)
                for(int j=M; j>=0; j--)
                    for(int k=1; k<=M; k++)          //复习第i门课k天
                        if(j>=k && f[j-k]>=0 && f[j]<f[j-k]+A[i][k])
                            f[j] = f[j-k] + A[i][k];
            /*int res = 0;
            for(int i=1; i<=M; i++)
                res = max(res, f[i]);
            printf("%d
    ", res);*/
            printf("%d
    ", f[M]);
        }
        return 0;
    }
  • 相关阅读:
    modal
    NSSpeechSynthesizer 文字变语音
    AVFoundation 初识
    语系/地区码
    Mac 平台下安装 OpenVC
    19-iOS图形性能
    01-产品发布10个大坑
    18-NSString之Strong和copy
    17-xcode6插件开发入门
    16-不能错过的Xcode插件
  • 原文地址:https://www.cnblogs.com/xingxing1024/p/5013166.html
Copyright © 2011-2022 走看看