题意:信封上最多贴S张邮票。有N个邮票集合,每个集合有不同的面值。问哪个集合的最大连续邮资最 大,输出最大连续邮资和集合元素。
最大连续邮资是用S张以内邮票面值凑1,2,3...到n+1凑不出来了,最大连续邮资就是n。如果不止一个集合结果相 同,输出集合元素少的,
如果仍相同,输出最大面值小的。
析:这个题,紫书上写的不全,而且错了好几次,结果WA好几次。
首先这个和背包问题差不多,我们只用一维就好。dp[i]表示邮资为 i 时的最小邮票数,然后,如果dp[i] > s就该结束了。
其他的就很简单了,主要是我没理解题意。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #include <cmath> #include <iostream> #include <cstring> #include <set> #include <queue> #include <algorithm> #include <vector> #include <map> #include <cctype> #include <cmath> #include <stack> #define freopenr freopen("in.txt", "r", stdin) #define freopenw freopen("out.txt", "w", stdout) using namespace std; typedef long long LL; typedef pair<int, int> P; const int INF = 0x3f3f3f3f; const double inf = 0x3f3f3f3f3f3f; const LL LNF = 0x3f3f3f3f3f3f; const double PI = acos(-1.0); const double eps = 1e-8; const int maxn = 15 + 5; const int mod = 1e9 + 7; const int dr[] = {-1, 0, 1, 0}; const int dc[] = {0, 1, 0, -1}; const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"}; int n, m; const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; inline int Min(int a, int b){ return a < b ? a : b; } inline int Max(int a, int b){ return a > b ? a : b; } inline LL Min(LL a, LL b){ return a < b ? a : b; } inline LL Max(LL a, LL b){ return a > b ? a : b; } inline bool is_in(int r, int c){ return r >= 0 && r < n && c >= 0 && c < m; } int a[maxn][maxn]; int dp[1024]; int ans[maxn]; int main(){ while(scanf("%d", &n) == 1 && n){ scanf("%d", &m); for(int i = 0; i < m; ++i){ scanf("%d", &a[i][0]); for(int j = 1; j <= a[i][0]; ++j) scanf("%d", &a[i][j]); fill(dp, dp+1024, INF); dp[0] = 0; for(int j = 1; j < 1024; ++j){ for(int k = 1; k <= a[i][0] && j-a[i][k] >= 0; ++k){ dp[j] = Min(dp[j], dp[j-a[i][k]]+1); } if(dp[j] > n){ ans[i] = j-1; break; } } } int anss = -1, cnt = 0; for(int i = 0; i < m; ++i){ if(ans[i] > anss){ anss = ans[i]; cnt = i; } else if(ans[i] == anss && a[i][0] < a[cnt][0]) cnt = i; else if(ans[i] == anss && a[i][0] == a[cnt][0]){ bool ok = false; for(int j = a[i][0]; j >= 0; --j) if(a[i][j] < a[cnt][j]){ ok = true; break; } else if(a[i][j] > a[cnt][j]) break; if(ok) cnt = i; } } printf("max coverage =%4d :", anss); for(int i = 1; i <= a[cnt][0]; ++i) printf("%3d", a[cnt][i]); printf("\n"); } return 0; }
时间: 2024-10-08 08:33:46