First of all, the description of purple book is not clear. The maximum continuous postage is from 111 to 111, and the increment is 111. 1,2,3,4,51,2,3,4,51,2,3,4,51,2,3,4,5 are, while 2,3,4,5,62,3,4,5,62,3,4,5,6 are not
Pay attention to the output format
state transition
We know that the state of this question must be transferred from the smaller sum to the larger sum, and if three stamps are pasted, then the state of pasting two corresponding stamps plus one can be transferred. Therefore, it is not necessary to enumerate the number of stamps attached to each stamp, but to enumerate the sum of stamps
Let d(j)d(j)d(j) be the minimum number of stamps required for current stamps and jjj, kkk is the subscript of a stamp, and iii is the current group iii stamps
d[j]=min(d[j],d[j−a[i][k]]+1),j−a[i][k]>=0d[j]=min(d[j],d[j-a[i][k]]+1),j-a[i][k]>=0d[j]=min(d[j],d[j−a[i][k]]+1),j−a[i][k]>=0
Boundaries and answers
The boundary is d[0]=0d[0]=0d[0]=0
Here we use a closing technique, that is, when the minimum number of stamps required for a certain denomination jjj is greater than sss, the answer is j − 1j-1j − 1
#include <set> #include <map> #include <stack> #include <queue> #include <math.h> #include <cstdio> #include <string> #include <bitset> #include <cstring> #include <sstream> #include <iostream> #include <algorithm> #include <unordered_map> using namespace std; #define fi first #define se second #define pb push_back #define ins insert #define lowbit(x) (x&(-x)) #define mkp(x,y) make_pair(x,y) #define mem(a,x) memset(a,x,sizeof a); typedef long long ll; typedef long double ld; typedef unsigned long long ull; typedef pair<int,int> P; const double eps=1e-8; const double pi=acos(-1.0); const int inf=0x3f3f3f3f; const ll INF=1e18; const int Mod=1e9+7; const int maxn=1005; int num[15],a[15][15],ans[15]; int d[maxn]; int main(){ //freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); //ios::sync_with_stdio(0),cin.tie(0),cout.tie(0); int s,n; while(~scanf("%d",&s) && s){ scanf("%d",&n); for(int i=1;i<=n;i++){ scanf("%d",&num[i]); for(int j=1;j<=num[i];j++) scanf("%d",&a[i][j]); memset(d,0x3f,sizeof d); d[0]=0; for(int j=1;j<maxn;j++){ for(int k=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; } } } int best=-1,pos=0; for(int i=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(int j=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(int i=1;i<=num[pos];i++) printf("%3d",a[pos][i]); printf("\n"); } return 0; }