PAT 1068. Find More Coins

标准10背包

#include <cstdio>
#include <cstdlib>
#include <vector>
#include <algorithm>

using namespace std;
const int ROWS = 10002;
const int COLS = 102;

char dp[ROWS][COLS];

bool dfs(vector<int> &coins, vector<int> &path, int idx, int target) {
    if (dp[idx][target] != -1) {
        return dp[idx][target];
    }
    if (idx >= coins.size()) return false;// should not happen
    if (coins[idx] == target) {
        // we found one
        path.push_back(idx);
        return true;
    }
    if (idx == coins.size() - 1) {
        // no more coins to try
        return false;
    }
    path.push_back(idx);
    // try use current idx coin
    if (coins[idx] < target) {
        if (dfs(coins, path, idx + 1, target - coins[idx])) {
            return true;
        }
    }
    path.pop_back();
    // try not use current idx coin
    if (dfs(coins, path, idx + 1, target)) {
        return true;
    }
    dp[idx][target] = false;
    return dp[idx][target];
}

int main() {
    int N, M;
    scanf("%d%d", &N, &M);

    vector<int> coins(N);

    for (int i=0; i<N; i++) {
        scanf("%d", &coins[i]);
    }
    sort(coins.begin(), coins.end());

    for (int i=0; i<ROWS; i++) {
        for (int j=0; j<COLS; j++) {
            dp[i][j] = -1;
        }
    }

    // use current st. j-coins>=0
    //dp[i][j] = dp[i-1][j-coins[i]]
    // not use current
    //dp[i][j] = dp[i-1][j];
    vector<int> path;
    bool res = dfs(coins, path, 0, M);
    int len = path.size();
    if (res) {
        if (len > 0) {
            printf("%d", coins[path[0]]);
        }
        for (int i=1; i<len; i++) {
            printf(" %d", coins[path[i]]);
        }
    } else {
        printf("No Solution");
    }
    return 0;
}
时间: 2024-08-28 08:03:08

PAT 1068. Find More Coins的相关文章

PAT 1068 Find More Coins (30)

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: f

1068. Find More Coins (30)【背包】——PAT (Advanced Level) Practise

题目信息 1068. Find More Coins (30) 时间限制150 ms 内存限制65536 kB 代码长度限制16000 B 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 a

PAT 甲级 1068 Find More Coins (30 分) (dp,01背包问题记录最佳选择方案)***

1068 Find More Coins (30 分)   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

pat 1068 动态规划/Fina More Conis

1068. Find More Coins (30) 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 re

PAT 1068. 万绿丛中一点红

PAT 1068. 万绿丛中一点红 对于计算机而言,颜色不过是像素点对应的一个24位的数值.现给定一幅分辨率为MxN的画,要求你找出万绿丛中的一点红,即有独一无二颜色的那个像素点,并且该点的颜色与其周围8个相邻像素的颜色差充分大. 输入格式: 输入第一行给出三个正整数,分别是M和N(<= 1000),即图像的分辨率:以及TOL,是所求像素点与相邻点的颜色差阈值,色差超过TOL的点才被考虑.随后N行,每行给出M个像素的颜色值,范围在[0, 224)内.所有同行数字间用空格或TAB分开. 输出格式:

PAT 甲级 1068 Find More Coins

https://pintia.cn/problem-sets/994805342720868352/problems/994805402305150976 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

【PAT甲级】1068 Find More Coins (30 分)(背包/DP)

题意: 输入两个正整数N和M(N<=10000,M<=10000),接着输入N个正整数.输出最小的序列满足序列和为M. 代码: #define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>using namespace std;int a[10007];int dp[107];int vis[10007][107];int main(){ ios::sync_with_stdio(false); cin.tie(NULL); cout.ti

PAT (Advanced Level) Practice 1068 Find More Coins

题解 01背包板子 + 记录路径.这次的记录路径比较特殊,要从多组解中找到一组由尽量小价值的硬币组成的解.所以不能利用一维数组记录路径,path[目前重量] = 物品序号,因为这样最后只能记录一个可能符合或不符合要求解.所以应该利用二维数组记录路径,path[ 物品序号 ][ 目前重量 ] = 1,这样可以记录多组解.因为要求为找到最小的一组解,所以先将拥有的硬币从大到小排序,以便于进行01背包时,可以从大到小更新解. 代码 #include<bits/stdc++.h> using name

1068. Find More Coins (30)

动态背包 题意: 给定一系列的硬币值, 然后给定一个目标value, 从所有硬币中找出几个, 使得这几个硬币的和正好等于这个value, 而且这个硬币序列应该是满足硬币值字典序的最小序列. 分析: 属于典型的背包问题. 用动态规划(dp)做, 假设F(N, M)表示不超过面值M, 而且从前面N个硬币中挑选硬币值能得到的最大硬币面值总和, 那我们可以得到如下递归公式: F(N, M) = max{ F(N–1, M), F(N–1, M–c(N)) + c(N) },c(N)表示第N个硬币的面值