Stamps and Envelope Size UVA

编程入门 行业动态 更新时间:2024-10-26 04:27:23

完全背包问题

附一篇完全背包问题很好的讲解blogs./Kalix/p/7622102.html

01背包也值得一看blogs./Kalix/p/7617856.html

代码也好理解

#include<iostream>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iomanip>
#include<assert.h>
#include<vector>
#include<list>
#include<map>
#include<set>
#include<sstream>
#include<stack>
#include<queue>
#include<string>
#include<bitset>
#include<algorithm>
#pragma warning(disable:4996)
#define me(s)  memset(s,0,sizeof(s))
#define _for(i,a,b) for(int i=(a);i<(b);++i)
#define _rep(i,a,b) for(int i=(a);i<=(b);++i)
#define FOR(i,n) for(int i=(n);i>=0;i--)
using namespace std;
typedef pair <int, int> pii;
typedef long long ll;
typedef unsigned long long llu;
const int inf = 0x3f3f3f3f;
const int dr[] = { 0, -1, 0, 1, -1, -1, 1, 1 };
const int dc[] = { -1, 0, 1, 0, -1, 1, -1, 1 };
const int MOD = 1e9 + 7;
const double pi = acos(-1.0);
const double eps = 1e-15;
const int maxn = 25;
int s, n, m;
int a[maxn];
int dp[1005];
int ans[maxn];
int main()
{while (cin >> s && s){int best = 0, Max =  inf, num = inf;cin >> n;for (int i = 0; i < n; i++){cin >> a[0];for (int j = 1; j <= a[0]; j++)cin >> a[j];memset(dp, 100, sizeof(dp));dp[0] = 0;int now = 0;for (int j = 1; j <= s * a[a[0]] + 1; j++){for (int k = 1; k <= a[0] && j >= a[k]; k++)dp[j] = min(dp[j], dp[j - a[k]] + 1);if (dp[j]>s){now = j - 1;break;}}if (now > best)   {best = now; num = a[0];Max = a[a[0]];memcpy(ans, a, sizeof(a));}else if (now == best)   {if (a[0] < num)   {num = a[0];Max = a[a[0]];memcpy(ans, a, sizeof(a));}else if (a[a[0]] < Max)  {Max = a[a[0]];memcpy(ans, a, sizeof(a));}}}printf("max coverage =%4d :", best);for (int i = 1; i <= num; i++)printf("%3d", ans[i]);puts("");}
}

 

更多推荐

Envelope,Stamps,UVA,Size

本文发布于:2023-05-29 21:34:36,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/353331.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Envelope   Stamps   UVA   Size

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!