uva242,Stamps and Envelope Size

这题紫薯上翻译错了

应该是:如果有多个并列,输出邮票种类最少的那个,如果还有并列,输出最大面值最小的那个

坑了我一个下午

dp[p][q]==1表示可以用不超过q张组成面额p

结合记忆化,p从1开始枚举,一直枚举找到dp[p][q]=0的时候就可以了

这题应该归类成一种背包吧

注意dp初始化的时候应该初始化为-1(我就因为粗心,tle好久)

最后输出的时候比较恶心

最终的修改后的代码

实验证明,先读入所有数据后再处理比边读数据边处理要快

/*
 * Author:  Bingo
 * Created Time:  2015/3/4 13:54:40
 * File Name: uva242.cpp
 */
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <time.h>
using namespace std;
const int maxint = 1000000;
int S,T,n;
int map[20][20];
int dp[1200][20];
//int ans[20][20];
int ans,ans_num,ans_max,ans_case;
int fun(int p,int q,int c){
    if (dp[p][q]!=-1) return dp[p][q];
    else if (p==0){
        dp[p][q]=1;
        return 1;
    }else if (q==0) {
        dp[p][q]=0;
        return 0;
    }else {
        for (int i=1;i<=map[c][0];i++) {
            if (p>=map[c][i]&&fun(p-map[c][i],q-1,c)){
                dp[p][q]=1;
                return 1;
            }
        }
    }
    dp[p][q]=0;
    return 0;
}
int cmp(int a,int b){//比较最大连续邮资相同的集合
    if(map[a][0]<map[b][0])return a;
    if(map[b][0]<map[a][0])return b;
    for(int i=map[a][0];i>0;i--){
        if(map[a][i]<map[b][i])return a;
        if(map[b][i]<map[a][i])return b;
    }
    return a;
}
int main(){
    while (cin>>S&&S){
        cin>>T;
        int mycase=0;
        ans=0;ans_num=maxint;ans_max=maxint;
        while (T--){
            cin>>n;
            mycase++;
            memset(dp,-1,sizeof(dp));
            map[mycase][0]=n;
            for (int i=1;i<=n;i++) {
                cin>>map[mycase][i];
            }
            int p;
            for (p=1;;p++) {
                if (fun(p,S,mycase)==0) break;
            }
            int t=p-1;
            if (t>ans){
                ans=t;
                ans_case=mycase;
            }else if (t==ans){
                ans_case=cmp(mycase,ans_case);
            }
        }
        printf("max coverage =%4d :", ans);
        for(int i=1;i<=map[ans_case][0];i++){
            printf("%3d",map[ans_case][i]);
        }
        printf("\n");
    }
    return 0;
}

时间: 2024-07-29 05:40:08

uva242,Stamps and Envelope Size的相关文章

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

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

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][

length()方法,length属性和size()的方法的区别

length()方法,length属性和size()的方法的区别: length()方法是针对字符串来说的,要求一个字符串的长度就要用到它的length()方法: length属性是针对Java中的数组来说的,要求数组的长度可以用其length属性: java中的size()方法是针对泛型集合说的,如果想看这个泛型有多少个元素,就调用此方法来查看! 这个例子来演示这两个方法和一个属性的用法: public static void main(String[] args) { String []li

为什么java里面经常作List判断的时候,既要判断list不为null,又要判断size&gt;0呢,岂不是多此一举吗?

List list=new ArrayList();1.如果是以上这样的话,lis不为null 但是size=0 这样集合对于之后的判断是没用的, 甚至是报异常,如list.get(0.......n)这样取值判断,就会报异常. 2.如果先判断size 再判断null 如:if(list.size>0){};这种情况如果list 等于null 时那么list.size会报空指针异常 所以要双重判断这样写最好if(list !=null && list.size>0){};

uva 242

242 - Stamps and Envelope Size Time limit: 3.000 seconds  Stamps and Envelope Size  Philatelists have collected stamps since long before postal workers were disgruntled. An excess of stamps may be bad news to a country's postal service, but good news

【翻译自mos文章】在一个使用uniform size的 本地管理的表空间中建立一个表,为什么会忽略INITIAL 参数?

翻译:Why Does a Table Created in a Locally Managed Tablespace With Uniform Extents Ignore INITIAL? (文档 ID 753662.1) 在一个使用uniform size的 本地管理的表空间中建立一个表,为什么会忽略INITIAL 参数? 适用于: Oracle Database - Enterprise Edition - Version 8.1.5.0 to 11.1.0.7 [Release 8.1

连接字符串中Min Pool Size的理解是错误,超时时间已到,但是尚未从池中获取连接。出现这种情况可能是因为所有池连接均在使用,并且达到了最大池大小。

Min Pool Size的理解是错误的 假设我们在一个ASP.NET应用程序的连接字符串中将Min Pool Size设置为30: <add name="cnblogs" connectionString="Data Source=.;Initial Catalog=cnblogs;Min Pool Size=30" providerName="System.Data.SqlClient"/> 访问一下应用程序,然后用Windows

QFileSystemModel只显示名称,不显示size,type,modified

Qt 提供的 QFileSystemModel可以提供文件目录树预览功能,但是预览的都自带了Name,size,type, modified等信息.我现在只想显示name这一列,不想显示size,type,modified的信息. 解决办法 办法1:修改QFileSystemModel 写一个子类,继承自QFileSystemModel,然后在需要显示size,type,modified的地方,把这些信息屏蔽掉.我知道Qt的Model显示数据,主要是在data这个函数中,然后Model的列是通过

OpenCV+python 彩色图像通道拆分与组合并判断size,shape函数的用法与区别

1.关于python中size与shape的用法,我一直是一头雾水,今天总结下,size既可以用作属性亦可以当做函数来使用,如a.size,np.szie(a),它是用来判断数组中所有元素的个数 ,而shap与size类似,既可以用作属性亦可以当做函数来使用,如b.shape,np.shape(b),他是用来判断数组的维度 2.关于图像的加载与显示,plt.imshow与cv2.imshow显示的时候会有色差的问题,因为OpenCV显示的BGR,而plt.imshow显示的是RGB 3.通道的分