HDU 1712 ACboy needs your help(DP)

Problem Description

ACboy has N courses this term, and he plans to spend at most M days on study.Of course,the profit he will gain from different course depending on the days he spend on it.How to arrange the M days for the N courses to maximize the
profit?

Input

The input consists of multiple data sets. A data set starts with a line containing two positive integers N and M, N is the number of courses, M is the days ACboy has.

Next follow a matrix A[i][j], (1<=i<=N<=100,1<=j<=M<=100).A[i][j] indicates if ACboy spend j days on ith course he will get profit of value A[i][j].

N = 0 and M = 0 ends the input.

Output

For each data set, your program should output a line which contains the number of the max profit ACboy will gain.

Sample Input

2 2
1 2
1 3
2 2
2 1
2 1
2 3
3 2 1
3 2 1
0 0

Sample Output

3
4
6

题意:n个课程,最多有m天可以用,每个课程花费不同的天数得到的收益不同,第i个课程花费j天来学那么收益为a[i][j],问如何安排收益最大。

首先明确每个课程只会上一次。设dp[i][j]为前i个课程花费j天的最大收益,枚举当前i课程的所有需要的天数k,并选择学还是不学对应的a[i][k]。

于是有 dp[i][j] = max( dp[i][j] , dp[i-1][j] , dp[i-1][j-k]+a[i][k] )  (1<=k<=j) .

#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
using namespace std;
typedef long long LL;
const int MAX=0x3f3f3f3f;
int a[105][105] , dp[105][105],n,m;
int Max(int a,int b,int c) {
    return max( max(a,b) , c );
}
int main()
{
    while (~scanf("%d%d",&n,&m) && n+m) {
        for(int i=1;i<=n;i++)
            for(int j=1;j<=m;j++)
                scanf("%d",&a[i][j]);
        memset(dp,0,sizeof(dp));
        for(int i=1;i<=n;i++)
            for(int j=1;j<=m;j++)
                for(int k=1;k<=j;k++)
                    dp[i][j] = Max( dp[i][j] ,dp[i-1][j] ,dp[i-1][ j-k ]+a[i][k] );
        printf("%d\n",dp[n][m]);
    }
    return 0;
}



HDU 1712 ACboy needs your help(DP)

时间: 2024-10-11 18:00:14

HDU 1712 ACboy needs your help(DP)的相关文章

hdu 1712 ACboy needs your help (dp 分组背包)

#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; int a[200][200]; int dp[200]; int main() { int n,m; int i,j,k; while(scanf("%d%d",&n,&m)!=EOF) { if(n==0&&m==0

hdu 1024 Max Sum Plus Plus(DP)

转移方程dp[i][j]=Max(dp[i][j-1]+a[j],max(dp[i-1][k] ) + a[j] ) 0<k<j 此链接中有详解点击打开链接 #include<stdio.h> #include<algorithm> #include<iostream> using namespace std; #define MAXN 1000000 #define INF 0x7fffffff int dp[MAXN+10]; int mmax[MAXN

HDU 4669 Mutiples on a circle(DP)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4669 题意:给出一个长度为n的数字环A和数字m.问有多少子串(连续)使得这些子串的数字拼在一起是m的倍数? 思路:首先计算A[1]和A[n]不在一起的.这个简单,只要记录f[i][j]表示到第i个数字余数为j的个数即可,然后: 接着计算a[1]和a[n]在一起的情况. 我们首先预处理end[i]表示数字子串A[i,n]连在一起对m的余数.然后枚举i从[1,n-1],那么数字设数字串[1,i]的长度为

【HDU】 1160 FatMouse&#39;s Speed (DP)

一开始写的dfs进行记忆化结果不知道怎么进行路径的记录...改成循环就好了 dp[i] = max(dp[j]) + 1 , weight[j] < weight[j] && speed[j] > speed[i] 一开始进行一次排序使得重量递增,这样只需要考虑速度就好了 #include<cstdio> #include<algorithm> using namespace std; const int maxn = 10005; struct Mou

HDU 1231:最大连续子序列(DP)

最大连续子序列 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 18461    Accepted Submission(s): 8202 Problem Description 给定K个整数的序列{ N1, N2, ..., NK },其任意连续子序列可表示为{ Ni, Ni+1, ..., Nj },其中 1 <= i <= j

HDU 5410 CRB and His Birthday(DP)

CRB and His Birthday Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 83    Accepted Submission(s): 45 Problem Description Today is CRB's birthday. His mom decided to buy many presents for her l

HDU 1712 ACboy needs your help(分组背包入门题)

http://acm.hdu.edu.cn/showproblem.php?pid=1712 题意: 有个人学习n门课程,a[i][j]表示用j分钟学习第i门课程所能获得的价值,背包容量为一共有m时间,求最大价值. 思路: P06: 分组的背包问题 问题 有N件物品和一个容量为V的背包.第i件物品的费用是c[i],价值是w[i].这些物品被划分为若干组,每组中的物品互相冲突,最多选一件.求解将哪些物品装入背包可使这些物品的费用总和不超过背包容量,且价值总和最大. 算法 这个问题变成了每组物品有若

HDU 1712 ACboy needs your help (分组背包问题)

有n门课程,和m天时间,完成a[i][j]得到的价值为第i行j列的数字,求最大价值...... 看过背包九讲的话,做这个就容易多了,其实就是简单的分组背包问题. #include <iostream> #include <string.h> using namespace std; int A[105][105]; int dp[105]; int main() { int n, m; while(cin>>n>>m&&n+m) { int

HDU 1712 ACboy needs your help(分组背包)

题意:给你n的课程组,每个课程组有m个课程,每个课程有一个完成时间与价值.问在m天内每组课程组最多选择一个,这样可以得到的最大价值是多少 题解:分组背包,其实就是每个课程组进行01背包,再在课程组内部进行枚举课程,但是这儿必须将枚举课程放在最里层才能保证最多选择一个 #include<set> #include<map> #include<queue> #include<stack> #include<cmath> #include<vec