UVa 242 - Stamps and Envelope Size(DP)

给出一个s,然后给出n组邮票,问那一组可以凑出最大连续邮资。

对每一组邮票,求出当邮资为i时需要邮票数的最小值d[i],边界为d[0]=0、d[i]>s时break。类似于背包问题的求法,具体方法见代码。

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=1010;
int d[maxn],ans[20],num[20],a[20][20];
int main(){
    int s;
    while(~scanf("%d",&s)&&s){
        int n;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;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-10 06:39:35

UVa 242 - Stamps and Envelope Size(DP)的相关文章

UVa 242 Stamps and Envelope Size (无限背包,DP)

题意:信封上最多贴S张邮票.有N个邮票集合,每个集合有不同的面值.问哪个集合的最大连续邮资最 大,输出最大连续邮资和集合元素. 最大连续邮资是用S张以内邮票面值凑1,2,3...到n+1凑不出来了,最大连续邮资就是n.如果不止一个集合结果相 同,输出集合元素少的, 如果仍相同,输出最大面值小的. 析:这个题,紫书上写的不全,而且错了好几次,结果WA好几次. 首先这个和背包问题差不多,我们只用一维就好.dp[i]表示邮资为 i 时的最小邮票数,然后,如果dp[i] > s就该结束了. 其他的就很简

uva 10564 Paths through the Hourglass (DP)

uva 10564 Paths through the Hourglass Paths through the Hourglass Input: Standard Input Output: Standard Output Time Limit: 2 Seconds In the hourglass to the right a path is marked. A path always starts at the first row and ends at the last row. Each

UVA 10497 - Sweet Child Makes Trouble(DP+高精度)

题目链接:10497 - Sweet Child Makes Trouble 题意:n个物品,原来物品属于一个地方,现在要把物品重新放回去,问能放几种使得每个物品都与原来位置不同 思路:递推,一开始随便搞了个二维状态,dp[i][j]表示i个物品,有j个位置不同,那么dp[n][n]就是答案,递推式为: dp[i][j] = 1 (j == 0) dp[i][j] = (j - 1) * dp[i - 1][j - 1] + dp[i - 1][j] + (i - j + 1) * dp[i -

UVA 580 - Critical Mass(DP)

题目链接:580 - Critical Mass 题意:一个栈,里面可以放L和U,有三个连续的U就是不安全的,问共有几种不安全的情况 思路:dp,dp[i][j][k],表示放到第i个,最后两个状态为j,k表示有没有出现不安全.然后去记忆化搜索一下就可以了 然后还有一种做法是,先考虑安全的情况,在用总情况(1<<n 种)减去安全的情况,安全的情况的递推方式很容易dp[i]表示放第i个, dp[i] = dp[i - 1] + dp[i - 2] + dp[i - 3]. 不过这题都没给数据范围

uva 10635 Prince and Princess(DP)

uva 10635 Prince and Princess(DP) In an n x n chessboard, Prince and Princess plays a game. The squares in the chessboard are numbered 1, 2, 3 ... n*n, as shown below: Prince stands in square 1, make p jumps and finally reach square n*n. He enters a

UVA 1371 - Period(DP)

6.4 一些说明 数据属性可以重写同名的方法属性.这是为了避免在大型系统中产生问题的意外名称冲突.所以用一些减少冲突的常用方法是很有效果的.常用的方法包括:大写字母方法名称,用唯一的字符串来做为数据属性的名称(可以是个下划线_)或者用动词命名方法和用名字命名数据属性. 数据属性就像和对象的普通用户一样可以被方法引用.换句话说,类不能用来实现纯净的数据类型.事实上,在python中不能强制数据隐藏,一切基于约定.(另一方面,如C中写的,python的实现可以做到完全隐藏实现细节并且在必要是可以控制

[LeetCode] Interleaving String(dp)

Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example, Given: s1 = "aabcc", s2 = "dbbca", When s3 = "aadbbcbcac", return true. When s3 = "aadbbbaccc", return false. 分析:非常好的DP的训练题,

[LeetCode] Longest Consecutive Sequence(DP)

Given an unsorted array of integers, find the length of the longest consecutive elements sequence. For example,Given [100, 4, 200, 1, 3, 2], The longest consecutive elements sequence is [1, 2, 3, 4]. Return its length: 4. Your algorithm should run in

HDU - 4971 A simple brute force problem. (DP)

Problem Description There's a company with several projects to be done. Finish a project will get you profits. However, there are some technical problems for some specific projects. To solve the problem, the manager will train his employee which may