【USACO 3.1.6】郵票

來源:互聯網
上載者:User

標籤:style   class   blog   code   color   使用   

【描述】

已知一個N枚郵票的面值集合(如,{1分,3分})和一個上限K ——表示信封上能夠貼K張郵票。計算從1到M的最大連續可貼出的郵資。

例如,假設有1分和3分的郵票;你最多可以貼5張郵票。很容易貼出1到5分的郵資(用1分郵票貼就行了),接下來的郵資也不難:

  • 6 = 3 + 3
  • 7 = 3 + 3 + 1
  • 8 = 3 + 3 + 1 + 1
  • 9 = 3 + 3 + 3
  • 10 = 3 + 3 + 3 + 1
  • 11 = 3 + 3 + 3 + 1 + 1
  • 12 = 3 + 3 + 3 + 3
  • 13 = 3 + 3 + 3 + 3 + 1。

然而,使用5枚1分或者3分的郵票根本不可能貼出14分的郵資。因此,對於這兩種郵票的集合和上限K=5,答案是M=13。

 【格式】PROGRAM NAME: stampsINPUT FORMAT:(file stamps.in) 
第1行: 兩個整數,K和N。K(1 <= K <= 200)是可用的郵票總數。N(1 <= N <= 50)是郵票面值的數量。
第2行..檔案末: N個整數,每行15個,列出所有的N個郵票的面值,面值不超過10000。
 OUTPUT FORMAT:(file stamps.out)

單獨的一行,一個整數,從 1 分開始連續的可用集合中不多於 K 張郵票貼出的郵資數。

【分析】

普通的DP,注意審題。

 1 #include <iostream> 2 #include <cstdio> 3 #include <cmath> 4 #include <cstring> 5 #include <algorithm> 6 const int maxn=50+10;  7 const int INF=200+10; 8 using namespace std; 9 int S[maxn],f[2000000+10];10 int main()11 {12     int k,n,i,j;13     //檔案操作14     freopen("stamps.in","r",stdin);15     freopen("stamps.out","w",stdout);16     scanf("%d%d",&k,&n);17     for (i=1;i<=n;i++) scanf("%d",&S[i]);18     f[0]=0;19     for (i=1;i<=2000000;i++)20     {21         f[i]=INF;22         for (j=1;j<=n;j++) if (i-S[j]>=0) f[i]=min(f[i],f[i-S[j]]+1);23         if (f[i]>k) break;24     }25     printf("%d",i-1);26     return 0;27 }

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.