UVa 242-stamps and Envelope Size (DP)

Source: Internet
Author: User

Gives a s , and then give n Set of stamps, ask the group to get the maximum continuous postage.
For each set of stamps, find out the minimum number of stamps required when postage is I d[i] , the boundary is d [ 0 = 0 、 d [ i > s Time break. Similar to the knapsack problem of the method, the specific methods see Code.

#include <cstdio>#include <cstring>#include <algorithm>Using namespace Std;constintmaxn=1010;intd[maxn],ans[ -],num[ -],a[ -][ -];intMain () {int s; while(~SCANF ("%d",&s) &&s){intN;SCANF ("%d", &n); for(intI=1; i<=n;++i) {scanf ("%d", &num[i]); for(intj=1; j<=num[i];++j) scanf ("%d", &a[i][j]); memset (D,0x3f, sizeof (d)); d[0]=0; for(intj=1; j<maxn;++j) { for(intk=1; k<=num[i]&&j-a[i][k]>=0; ++k) D[j]=min (d[j],d[j-a[i][k]]+1);if(d[j]>s) {ans[i]=j-1; Break;} }        }intbest=-1,POS=0; for(intI=1; i<=n;++i) {if(ans[i]>best) Best=ans[i],POS=i;Else if(ans[i]==best) {if(num[i]<num[POS])POS=i;Else if(num[i]==num[POS]) {bool Ok=false; for(intJ=num[i];j>0;--j)if(a[i][j]!=a[POS][j]) {ok=a[i][j]<a[POS][J]; Break; }if(OK)POS=i; }            }        }printf("Max coverage =%4d :", ans[POS]); for(intI=1; i<=num[POS];i++)printf("%3d", a[POS][i]);printf("\ n"); }return 0;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

UVa 242-stamps and Envelope Size (DP)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.