首页 > ACM题库 > HDU-杭电 > HDU 3905-Sleeping-动态规划-[解题报告]HOJ
2015
04-14

HDU 3905-Sleeping-动态规划-[解题报告]HOJ

Sleeping

问题描述 :

ZZZ is an enthusiastic ACMer and he spends lots of time on training. He always stays up late for training. He needs enough time to sleep, and hates skipping classes. So he always sleeps in the class. With the final exams coming, he has to spare some time to listen to the teacher. Today, he hears that the teacher will have a revision class. The class is N (1 <= N <= 1000) minutes long. If ZZZ listens to the teacher in the i-th minute, he can get Ai points (1<=Ai<=1000). If he starts listening, he will listen to the teacher at least L (1 <= L <= N) minutes consecutively. It`s the most important that he must have at least M (1 <= M <= N) minutes for sleeping (the M minutes needn`t be consecutive). Suppose ZZZ knows the points he can get in every minute. Now help ZZZ to compute the maximal points he can get.

输入:

The input contains several cases. The first line of each case contains three integers N, M, L mentioned in the description. The second line follows N integers separated by spaces. The i-th integer Ai means there are Ai points in the i-th minute.

输出:

The input contains several cases. The first line of each case contains three integers N, M, L mentioned in the description. The second line follows N integers separated by spaces. The i-th integer Ai means there are Ai points in the i-th minute.

样例输入:

10 3 3
1 2 3 4 5 6 7 8 9 10

样例输出:

49

通过这题看出DP的功底还是不行啊,仍需提高。

题意:一节课有N分钟,ZZ需要睡M分钟,每分钟都有其效益值,ZZ若听课,则必须连续听L分钟以上。问能获得的最大效益值。M分钟的睡眠可以不连续
题解:动态规划,状态设计:dp[i][j]表示第i分钟已经睡过j分钟可以获得的最大效益。状态转移可以分第i分钟听课或者睡觉。若第i分钟睡觉,dp[i][j] = max(dp[i-1][j],dp[i-1][j-1]),若听课,对于每个k( 0 <= k <= i – l),求dp[k][j] + sum[i] – sum[k]的最大值。求最大值的时候用一个数组维护,不用另写一个循环。在第i-1次循环时求得的最大值到第i次时,全都加上了a[i](第i分钟的效益值),[0,i-1-l]区间内的最大值可以通过上一轮循环(i-1)的最大值加上第i分钟的效益值得到,然后和dp[i-l][j]+sum[i]-sum[i-l]比较取较大值即为[0,i-l]区间内的最大值。sum[i]是前i分钟总的效益值

code:

#include <cstdio>
#include <cstring>
#define N 1010
#define Max( a, b ) ( a > b ? a : b )
using namespace std;

int dp[ N ];

int cmp[ N ], sum[ N ];

void init ( int n ) {
	memset ( cmp, 0, sizeof ( cmp ) );
	for ( int i = 1 ; i < n ; ++i ) {
		sum[ i ] += sum[ i - 1 ];
	}
}

int solve ( int n, int m, int l ) {
	init ( n );
	for ( int j = 0 ; j <= m ; ++j ) {
		int div = j;
		memset ( dp, 0, sizeof ( dp ) );
		for ( int i = j + 1 ; i <= n ; ++i ) {
			dp[ i ] = Max ( dp[ i ], cmp[ i - j ] );
			if ( i - l < j ) continue;
			if ( dp[ div ] + sum[ i - 1 ] - sum[ div - 1 ] <
				dp[ i - l ] + sum[ i - 1 ] - sum[ i - l - 1 ] ) {
				div = i - l;
			}
			dp[ i ] = Max ( dp[ i ], dp[ div ] + sum[ i - 1 ] - sum[ div - 1 ] );
			cmp[ i - j ] = Max ( cmp[ i - j ], dp[ i ] );
		}
	}
	return dp[ n ];
}

int main () {
	int n, m, l;
	while ( scanf ( "%d%d%d", &n, &m, &l ) != EOF ) {
		for ( int i = 0 ; i < n ; ++i ) {
			scanf ( "%d", &sum[ i ] );
		}
		printf ( "%d\n", solve ( n, m, l ) );
	}
	return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

参考:http://blog.csdn.net/accry/article/details/6657755


  1. #include <stdio.h>
    int main()
    {
    int n,p,t[100]={1};
    for(int i=1;i<100;i++)
    t =i;
    while(scanf("%d",&n)&&n!=0){
    if(n==1)
    printf("Printing order for 1 pages:nSheet 1, front: Blank, 1n");
    else {
    if(n%4) p=n/4+1;
    else p=n/4;
    int q=4*p;
    printf("Printing order for %d pages:n",n);
    for(int i=0;i<p;i++){
    printf("Sheet %d, front: ",i+1);
    if(q>n) {printf("Blank, %dn",t[2*i+1]);}
    else {printf("%d, %dn",q,t[2*i+1]);}
    q–;//打印表前
    printf("Sheet %d, back : ",i+1);
    if(q>n) {printf("%d, Blankn",t[2*i+2]);}
    else {printf("%d, %dn",t[2*i+2],q);}
    q–;//打印表后
    }
    }
    }
    return 0;
    }

  2. 嗯 分析得很到位,确实用模板编程能让面试官对你的印象更好。在设置辅助栈的时候可以这样:push时,比较要push的elem和辅助栈的栈顶,elem<=min.top(),则min.push(elem).否则只要push(elem)就好。在pop的时候,比较stack.top()与min.top(),if(stack.top()<=min.top()),则{stack.pop();min.pop();},否则{stack.pop();}.

  3. 嗯 分析得很到位,确实用模板编程能让面试官对你的印象更好。在设置辅助栈的时候可以这样:push时,比较要push的elem和辅助栈的栈顶,elem<=min.top(),则min.push(elem).否则只要push(elem)就好。在pop的时候,比较stack.top()与min.top(),if(stack.top()<=min.top()),则{stack.pop();min.pop();},否则{stack.pop();}.

  4. 第二个方法挺不错。NewHead代表新的头节点,通过递归找到最后一个节点之后,就把这个节点赋给NewHead,然后一直返回返回,中途这个值是没有变化的,一边返回一边把相应的指针方向颠倒,最后结束时返回新的头节点到主函数。