pat 5-24 Find More Coins - dblank

pat 5-24 Find More Coins

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 10^4104 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 (le 10^4≤104, the total number of coins) and M (le 10^2≤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 V_1 le V_2 le cdots le V_kV1≤V2≤⋯≤Vk such that V_1 + V_2 + cdots + V_k =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 ge 1k≥1such that A[ii]=B[ii] for all i < ki<k, and A[kk] << B[kk].

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

题意:给你n个数,让你找到字典序最小的和为m的序列。

思路:其实就是一个01背包输出字典序最小的解,先将数从大到小排好,然后用二维的背包求解,在dp的过程中,标记visi为到达体积为j时选择了i物品。然后回溯输出方案就可以了。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<map>
#include<cstring>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<cmath>
using namespace std;
const double eps = 1e-10;
const int inf = 0x3f3f3f3f, N = 1e4 + 10, Mod = 100000007;
typedef long long ll;
int dpN, a[N], visN;
int cmp(int a, int b)
{
    return a > b;
}
int main()
{
    int n, m;
    scanf("%d%d", &n, &m);
    for(int i = 1; i<=n; i++)
        scanf("%d", &a[i]);
    memset(dp, 0, sizeof(dp));
    vis0 = 0;
    sort(a+1, a+n+1, cmp);
    for(int i = 1; i<=n; i++)
        for(int j = a[i]; j<=m; j++)
        {
            if(dpi-1 > dpi-1] + a[i])
            {
                dpi = dpi-1;
            }
            else
            {
                dpi = dpi-1] + a[i];
                visi = 1;
            }
        }
    int v = m, t = n;
    queue<int> ans;
    if(dpn!=m)
        printf("No Solutionn");
    else
    {
        while(v)
        {
            while(!vist)
                t--;
            ans.push(a[t]);
            v-=a[t];
            t--;
        }
        printf("%d", ans.front());
        ans.pop();
        while(!ans.empty())
        {
            printf(" %d", ans.front());
            ans.pop();
        }
        printf("n");
    }
    return 0;
}

 

相关文章

发表新评论