首页 > 其他分享 >HDU-1712

HDU-1712

时间:2022-11-21 20:22:10浏览次数:60  
标签:Case 1712 HDU int cin dp define

HDU-1712

思路

\(dp[i][j]\)表示从前\(i\)个科目中选,总共花\(j\)天所能得到的最大学分。

首先遍历科目\(i : 1 \rightarrow n\),再遍历所有天数\(j : 1 \rightarrow m\),再遍历当前科目\(i\)能花的时间\(k : 0 \rightarrow j\),有转移方程:

\[ dp[i][j] = max(dp[i][j], dp[i - 1][j - k] + v[i][k]) \]

于是有代码:

#include <bits/stdc++.h>
using namespace std;
#define _u_u_ ios::sync_with_stdio(false), cin.tie(nullptr)
#define cf int _o_o_;cin>>_o_o_;for (int Case = 1; Case <= _o_o_;Case++)
#define SZ(x) (int)(x.size())
inline void _A_A_();
signed main() {_A_A_();return 0;}

int v[110][110], dp[110][110];

inline void _A_A_() {
    int n, m;
    while (cin >> n>> m) {
        if (n == 0 && m == 0) break;
        memset(dp, 0,sizeof dp);
        for (int i = 1;i <= n;i++) {
            for (int j = 1;j <= m;j++) cin >> v[i][j];
        }
        for (int i = 1;i <= n;i++) {
            for (int j = 1;j <= m;j++) {
                for(int k = 0;k <= j;k++) { // 注意k为0时的意义表示第i个科目完全不学。是可以的。
                    dp[i][j] = max(dp[i][j] , dp[i - 1][j - k] + v[i][k]);
                }
            }
        }
        cout << dp[n][m]<< "\n";
    }
}

由于\(dp[i][j]\)完全由\(dp[i-1][j]\)转移过来,可以滚动数组。

#include <bits/stdc++.h>
using namespace std;
#define _u_u_ ios::sync_with_stdio(false), cin.tie(nullptr)
#define cf int _o_o_;cin>>_o_o_;for (int Case = 1; Case <= _o_o_;Case++)
#define SZ(x) (int)(x.size())
inline void _A_A_();
signed main() {_A_A_();return 0;}

using ll = long long;
// #define int long long
const int mod = 1e9 + 7;
const int maxn = 2e5 + 10;
const int N = 210, M = 5010;
const int inf = 0x3f3f3f3f;

int v[110][110], dp[110];

inline void _A_A_() {
    #ifdef LOCAL
    freopen("in.in", "r", stdin);
    #endif
    _u_u_;
    int n, m;
    while (cin >> n>> m) {
        if (n == 0 && m == 0) break;
        memset(dp, 0,sizeof dp);
        for (int i = 1;i <= n;i++) {
            for (int j = 1;j <= m;j++) cin >> v[i][j];
        }
        for (int i = 1;i <= n;i++) {
            for (int j = m;j >= 0;j--) {    // 由于自己滚动,需要从大到小。
                for(int k = 0;k <= j;k++) {
                    dp[j] = max(dp[j] , dp[j - k] + v[i][k]);
                }
            }
        }
        cout << dp[m]<< "\n";
    }
}

标签:Case,1712,HDU,int,cin,dp,define
From: https://www.cnblogs.com/FanWQ/p/16913074.html

相关文章

  • hduCoconuts(离散化)
    CoconutsTimeLimit:9000/4500MS(Java/Others)    MemoryLimit:65536/65536K(Java/Others)TotalSubmission(s):747    AcceptedSubmission(s):219Pr......
  • HDU-5015
    HDU-5015思路\(n\leq10\)是一个较为明显的提示,容易想到一列一列的转移,通过对\(a_{n,m}\)的寻找,发现\(a_{n,m}=\sum_1^{n}a_{i,m-1}+a_{0,m}\),其中\(\sum_1^{n}a_......
  • HDU-4549
    HDU-4549思路列出数列前几项,发现\(a\)和\(b\)的次数符合斐波那契数列。问题就转化为了求出\(1e9\)项斐波那契数列,可以使用矩阵快速幂加速。但是求出的第\(1e9\)个斐波那......
  • HDU-4565
    HDU-4565思路这里太难想了wwwCode#include<bits/stdc++.h>usingnamespacestd;#define_u_u_ios::sync_with_stdio(false),cin.tie(nullptr)#definecfint_o......
  • HDU 2191:悼念512汶川大地震遇难同胞——珍惜现在,感恩生活 (多重背包)
    悼念512汶川大地震遇难同胞——珍惜现在,感恩生活TimeLimit:1000/1000MS(Java/Others)    MemoryLimit:32768/32768K(Java/Others)TotalSubmission(s):2529......
  • 题解 HDU4035 【Maze】
    postedon2022-08-1712:33:51|under题解|sourceproblemhttps://vjudge.net/problem/HDU-4035SHY在一棵树上随机游走,从根节点出发,每次有\(k_u\)的几率回到根节......
  • HDU 3726 Graph and Queries
    DescriptionYouaregivenanundirectedgraphwithNvertexesandMedges.Everyvertexinthisgraphhasanintegervalueassignedtoitatthebeginni......
  • HDU 3468 Treasure Hunting
    DescriptionDoyouliketreasurehunting?Today,withoneofhisfriend,iSeaisonaventuretripagain.Asmostmoviesaid,theyfindsomanygoldhid......
  • HDU 5446 Unknown Treasure
    ProblemDescriptionOnthewaytothenextsecrettreasurehidingplace,themathematiciandiscoveredacaveunknowntothemap.Themathematicianenter......
  • HDU 5434 Peace small elephant
    ProblemDescriptionXiaoMinglikesplayingtheinternationalchess,especiallyliketheelephant(itcanmovebyslantwhilenobarrier),buthethink......