zoukankan      html  css  js  c++  java
  • 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;
    }
    


  • 相关阅读:
    Mysql之存储过程与存储函数
    mysql-bin日志自动清理及手动删除
    mysql下面的binlog
    mysql下的数据备份与恢复
    查询mysql数据库中各个表所占空间大小以及索引大小
    mysql执行sql语句报错this is incompatible with sql_mode=only_full_group_by
    docker WARNING: IPv4 forwarding is disabled. 解决方法
    Linux平台修改环境变量的方式
    PuTsangTo
    (一) 从Angular1到Angular2的杂谈
  • 原文地址:https://www.cnblogs.com/dacc123/p/8228591.html
Copyright © 2011-2022 走看看