• PAT 甲级 1068 Find More Coins(0,1背包)


    1068. Find More Coins (30)

    时间限制
    150 ms
    内存限制
    65536 kB
    代码长度限制
    16000 B
    判题程序
    Standard
    作者
    CHEN, Yue

    Eva loves to collect coins from all over the universe, including some other planets like Mars. One day she visited a universal shopping mall which could accept all kinds of coins as payments. However, there was a special requirement of the payment: for each bill, she must pay the exact amount. Since she has as many as 104 coins with her, she definitely needs your help. You are supposed to tell her, for any given amount of money, whether or not she can find some coins to pay for it.

    Input Specification:

    Each input file contains one test case. For each case, the first line contains 2 positive numbers: N (<=104, the total number of coins) and M(<=102, the amount of money Eva has to pay). The second line contains N face values of the coins, which are all positive numbers. All the numbers in a line are separated by a space.

    Output Specification:

    For each test case, print in one line the face values V1 <= V2 <= ... <= Vk such that V1 + V2 + ... + Vk = M. All the numbers must be separated by a space, and there must be no extra space at the end of the line. If such a solution is not unique, output the smallest sequence. If there is no solution, output "No Solution" instead.

    Note: sequence {A[1], A[2], ...} is said to be "smaller" than sequence {B[1], B[2], ...} if there exists k >= 1 such that A[i]=B[i] for all i < k, and A[k] < B[k].

    Sample Input 1:
    8 9
    5 9 8 7 2 3 4 1
    
    Sample Output 1:
    1 3 5
    
    Sample Input 2:
    4 8
    7 2 4 3
    
    Sample Output 2:
    No Solution
    0,1背包
    按照字典序最小的输出方案,先把物品从大到小排序,然后再背包,就可以保证选的是字典序最小的,记录路径用二维数组
    #include <iostream>
    #include <string.h>
    #include <stdlib.h>
    #include <algorithm>
    #include <stdio.h>
    #include <math.h>
    
    using namespace std;
    int n,m;
    const int maxn=1e4;
    int a[maxn+5];
    int dp[105];
    int s[maxn+5][105];
    void fun(int x,int y)
    {
        if(x>n)
            return;
        if(s[x][y]==1)
        {
            if(y-a[x]==0)
                printf("%d
    ",a[x]);
            else
                printf("%d ",a[x]);
            fun(x+1,y-a[x]);
        }
        else
            fun(x+1,y);
        
    }
    int main()
    {
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
        }
        sort(a+1,a+n+1);
        memset(dp,-1,sizeof(dp));
        memset(s,0,sizeof(s));
        dp[0]=0;
        s[0][0]=-1;
        for(int i=n;i>=1;i--)
        {
            for(int j=m;j>=a[i];j--)
            {
                if(dp[j-a[i]]!=-1)
                {
                    dp[j]=dp[j-a[i]]+a[i];
                    s[i][j]=1;
                }
               
            }
        }
        if(dp[m]==-1)
            printf("No Solution
    ");
        else
            fun(1,m);
        return 0;
    }
    


  • 相关阅读:
    memcache清理缓存
    JqGrid常见使用方法
    SQLyog 最新版本12.5-64bit 完美破解,亲测可用!
    Navicat Premium 最新版本12.1.16-64bit 完美破解,亲测可用!
    【JAVA基础】一:聊聊笔试常见到的 “==、equal” 比较是否相等的内在差别
    scrapy 爬虫返回json格式内容unicode编码转换为中文的问题解决
    Python3的桌面程序开发利器:Eric6的环境搭建、使用
    通过Redis、Memcache的 incr 原子操作防刷机制的使用差别
    从零开始搭建一个从Win7环境备份至CentOS7的SVN双机备份环境
    通过Quartz 配置定时调度任务:使用cron表达式配置时间点
  • 原文地址:https://www.cnblogs.com/dacc123/p/8228591.html
Copyright © 2020-2023  润新知