• USACO rockers


      题目意思是有N首歌, M张光盘, 每张光盘的容量为T,现在要将这些歌曲按照时间顺序写入M张光盘中, 每首歌曲必须完整的存入一张光盘中,并且必须严格按照时间顺序写入, 问你这M张光盘最多可以容纳几首音乐?我们可以定义f[i][j]为前i个光盘写入1-j首歌的最大数量, 那么状态转移式子就可以写为f[i][j] = max(f[i-1][j], f[i-1][k]+num[k+1][j]) k<j  num[k+1][j]为将k+1-j的歌曲放入一张光盘最多放几首,代码如下:

    /*
        ID: m1500293
        LANG: C++
        PROG: rockers
    */
    #include <cstdio>
    #include <algorithm>
    #include <cstring>
    
    using namespace std;
    int f[30][30];   //前i个光盘结束于j首歌的最大歌曲数
    int num[30][30]; //一张光盘最多容纳几首i-j的歌曲
    int N, T, M;   //N首歌 T分钟 M张盘
    int time[30];
    
    int main()
    {
        freopen("rockers.in", "r", stdin);
        freopen("rockers.out", "w", stdout);
        scanf("%d%d%d", &N, &T, &M);
        for(int i=1; i<=N; i++)
            scanf("%d", &time[i]);
        for(int i=1; i<=N; i++)
            for(int j=i; j<=N; j++)
            {
                num[i][j] = 0;
                int tp[30];
                memcpy(tp, time, sizeof(time));
                sort(tp+i, tp+i+(j-i+1));
                int tpT = T;
                int res = 0;
                for(int k=i; k<=j; k++)
                {
                    if(tpT >= tp[k])
                        res++, tpT-=tp[k];
                }
                num[i][j] = res;
            }
        memset(f, 0, sizeof(f));
        for(int j=1; j<=N; j++) f[1][j] = num[1][j];
        for(int i=2; i<=M; i++)
            for(int j=1; j<=N; j++)
            {
                f[i][j] = max(f[i][j], f[i-1][j]);
                for(int k=1; k<j; k++)
                    f[i][j] = max(f[i][j], f[i-1][k]+num[k+1][j]);
            }
        printf("%d
    ", f[M][N]);
        return 0;
    }
  • 相关阅读:
    hdu 4027 Can you answer these queries? 线段树
    ZOJ1610 Count the Colors 线段树
    poj 2528 Mayor's posters 离散化 线段树
    hdu 1599 find the mincost route floyd求最小环
    POJ 2686 Traveling by Stagecoach 状压DP
    POJ 1990 MooFest 树状数组
    POJ 2955 Brackets 区间DP
    lightoj 1422 Halloween Costumes 区间DP
    模板 有源汇上下界最小流 loj117
    模板 有源汇上下界最大流 loj116
  • 原文地址:https://www.cnblogs.com/xingxing1024/p/5097079.html
Copyright © 2020-2023  润新知