hdu 3905 Sleeping

來源:互聯網
上載者:User
Sleeping

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 125536/65536 K (Java/Others)
Total Submission(s): 1514    Accepted Submission(s): 578

Problem DescriptionZZZ 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. 

InputThe 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. 

OutputFor each test case, output an integer, indicating the maximal points ZZZ can get. 

Sample Input

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

Sample Output

49
 

Source2011 Multi-University Training Contest 7 - Host by ECNU 

Recommendxubiao 

題目大意:一節課有n分鐘,至少要睡m分鐘,每次醒來至少會清醒L分鐘,每分鐘有不同的價值,醒著才可以獲得,求可獲得的最大價值。
思路:
因為v[i]>0,所以獲得最大價值的時候只能睡m分鐘。
用dp做。
dp[i][j]表示在第i分鐘睡j分鐘可以獲得的最大價值,注意:在每一點的dp[i][j]都滿足題目要求。
用up[j]數組表示,睡j分鐘的時候,從當前i點開始到至少i-l分鐘都是清醒著所獲的最大價值。
當i點睡著的時候價值為 dp[i-1][j-1]
當i點醒著的時候價值為 up[j]

求兩者的最大值

#include <iostream>#include<string.h>#include<stdio.h>using namespace std;int dp[1010][1010],up[1010];int s[1010];int n,l,m;int ma(int a,int b){    return a>b?a:b;}int main(){    int i,j,a;    while(~scanf("%d%d%d",&n,&m,&l))    {        s[0]=0;        for(i=1; i<=n; i++)        {            scanf("%d",&s[i]);            s[i]+=s[i-1];        }        memset(dp,0,sizeof dp);        memset(up,0,sizeof up);        for(i=1; i<=n; i++)        {            a=s[i]-s[i-1];//擷取第i分鐘的分數            for(j=0; j<=i&&j<=m; j++)            {    //不用擔心j-1為負值。預設的dp[0][-1]為0.                dp[i][j]=dp[i-1][j-1];//第i分鐘睡覺的價值。下面考慮第i分鐘學習的最大價值                if(i-l>=j)//要保證在i之前至少有l分鐘聽課。不然第i分鐘不能聽課                  {                      up[j]=ma(up[j]+a,dp[i-l][j]+s[i]-s[i-l]);//up[j]表示在i之前至少有l-1分鐘聽課的最大價值                      dp[i][j]=ma(dp[i][j],up[j]);//dp[i - l][j] + sum[i] - sum[i - l]這樣就可以保證至少i-l到l清醒了。                  }                               //up[j]中值就始終為i之前至少有l-1分鐘聽課的最大價值            }        }        printf("%d\n",dp[n][m]);    }    return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.