12563,12563火車票

來源:互聯網
上載者:User

12563,12563火車票

自己花了兩個小時A出來的感覺就是不一樣啊,學習DP建議大家多煎熬一點,好好思考狀態是如何轉移的。

不過這道題我一開始理解錯題意了,不然也不會浪費這麼長時間 。 一開始以為是背包問題,後來才發現要求使得唱的曲目盡量多,在此前提下盡量晚離開KTV,我恰好弄顛倒了。

這樣我們就不難得出遞推方程 : 因為每首曲目只能唱一遍,所以這就使遞推變得有序了~ 那麼我們設cnt[i][j]表示唱前i首歌中的若干,且總時間不超過j 的最大歌曲數目 。

那麼cnt[i][j] = max(cnt[i-1][j],cnt[i-1][j-a[i]] + 1);    因為還要使總時間盡量長,所以當cnt[i][j] == cnt[i-1][j-a[i]] + 1 時,d[i][j] = max(d[i][j],d[i-1][j-a[i]] + a[i]);

細節參見代碼:

#include<bits/stdc++.h>using namespace std;const int maxn = 55;int T,n,t,Case = 0,d[maxn][180*maxn + 678],a[maxn],cnt[maxn][180*maxn + 678];int main() {    scanf("%d",&T);    while(T--){        scanf("%d%d",&n,&t);        for(int i=1;i<=n;i++) scanf("%d",&a[i]);        for(int i=1;i<=n;i++) {            for(int j=0;j<=t;j++) {                cnt[i][j] = (i == 1 ? 0 : cnt[i-1][j]);                d[i][j] = (i == 1 ? 0 : d[i-1][j]);                if(j > a[i]) {                    if(cnt[i][j] < cnt[i-1][j-a[i]] + 1) {                        cnt[i][j] = cnt[i-1][j-a[i]] + 1;                        d[i][j] = d[i-1][j-a[i]] + a[i];                    }                    else if(cnt[i][j] == cnt[i-1][j-a[i]] + 1) {                        d[i][j] = max(d[i][j],d[i-1][j-a[i]] + a[i]);                    }                }            }        }        printf("Case %d: %d %d\n",++Case,cnt[n][t]+1,d[n][t]+678);    }    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.