zoukankan      html  css  js  c++  java
  • SSLZYC 邮票

    题目大意:
    你被提供一套不同面额的邮票,并规定最多能在一封信上粘贴的邮票数,你的目标是算出最大的可连续贴出的面值集合的元素个数。


    思路:
    这是一道完全背包的问题。我们用f[j]表示面值为j的时候最少所需使用的邮票数,则得到了状态转移方程:
    f[j+a[i]]=min(f[j+a[i]],f[j]+1)
    然后枚举可能的邮票面值并记录就可以AC啦!


    代码:

    #include <cstdio>
    #include <cstring>
    #include <iostream>
    using namespace std;
    
    int k,n,m,a[21],f[10001],maxn,h,maxm;
    
    int main()
    {
        freopen("stamp.in","r",stdin);
        freopen("stamp.out","w",stdout);
        scanf("%d%d",&k,&m);
        for (int i=1;i<=k;i++)
        {
            scanf("%d",&a[i]);
            if (a[i]*m>maxn) maxn=a[i]*m;  //计算邮票的最大值
        }
        memset(f,0x7f,sizeof(f));
        f[0]=0;
        for (int i=1;i<=k;i++)
         for (int j=0;j<=maxn;j++)
          f[j+a[i]]=(f[j+a[i]])<(f[j]+1)?(f[j+a[i]]):(f[j]+1);      //相当于f[j+a[i]]=min(f[j+a[i]],f[j]+1)
        for (int i=1;i<=maxn;i++)
        {
            if (f[i]<=m) h++;
            else h=0;
            if (h>maxm) maxm=h;
        }
        printf("%d\n",maxm);
        return 0;
    }
  • 相关阅读:
    P5107 能量采集
    P4655 [CEOI2017]Building Bridges
    P1129 [ZJOI2007]矩阵游戏
    P5299 [PKUWC2018]Slay the Spire
    P1625求和 giao精大杂烩
    背包
    根号分治
    CF963B
    国王游戏
    P6006 USACO 3SUM G
  • 原文地址:https://www.cnblogs.com/hello-tomorrow/p/9313127.html
Copyright © 2011-2022 走看看