zoukankan      html  css  js  c++  java
  • 1068 Find More Coins (30分)(记忆化搜索 || dp)

    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: for each bill, she must pay the exact amount. Since she has as many as 1 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 (≤, the total number of coins) and M (≤, 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 k1 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

    哭泣...
    一看就知道是dp,然后就是怎么推都推不出来...,看数据不多遂用dfs硬钢,我服了唉。

    这里说一下dfs的细节哈。
    由于要求字典序最小的解,因此面值越小的硬币越多越好,这样的话直接搜索搜到的第一个满足条件的就是
    最优解。

     1 #include <cstdio>
     2 #include <cstring>
     3 #include <vector>
     4 using namespace std;
     5 
     6 const int maxn = 100 + 5;
     7 int val[maxn];
     8 int n, m;
     9 int now;
    10 
    11 int pre[maxn];
    12 int num[maxn];
    13 
    14 bool ans;
    15 vector <int> vec;
    16 
    17 void dfs(int u) {
    18     if(now > m || ans) return;
    19     if(now == m) {
    20         ans = true;
    21         while(true) {
    22             if(u == -1) break;
    23             for(int i = 1; i <= num[u]; i ++) {
    24                 vec.push_back(u);
    25             }
    26             u = pre[u];
    27         }
    28         return;
    29     }
    30     for(int v = u + 1; v <= 100; v ++) {
    31         for(int i = val[v]; i > 0; i --) {
    32             now += v * i;
    33             pre[v] = u;
    34             num[v] = i;
    35             dfs(v);
    36             now -= v * i;
    37         }
    38     }
    39 }
    40 
    41 int main() {
    42     memset(pre, -1, sizeof pre);
    43     int coins, min = maxn;
    44     scanf("%d %d", &n, &m);
    45     for(int i = 0; i < n; i ++) {
    46         scanf("%d", &coins);
    47         if(coins < min) min = coins;
    48         if(coins <= 100) val[coins] ++;
    49     }
    50     for(int i = val[min]; i >= 0; i --) {
    51         now = min * i;
    52         num[min] = i;
    53         dfs(min);
    54     }
    55     if(!ans) printf("No Solution
    ");
    56     else {
    57         for(int i = vec.size() - 1; i >= 0; i --) {
    58             printf("%d", vec[i]);
    59             if(i) printf(" ");
    60         }
    61     }
    62     return 0;
    63 }
    dp解法,看起来是个裸完全背包。
     1 #include <cstdio>
     2 #include <vector>
     3 #include <algorithm>
     4 using namespace std;
     5 
     6 const int maxn = 10000 + 5, maxm = 100 + 5;
     7 
     8 bool cmp(int a, int b) {
     9     return a > b;
    10 }
    11 
    12 int dp[maxm], val[maxn];
    13 bool vis[maxn][maxm];
    14 
    15 int main() {
    16     int n, m;
    17     scanf("%d %d", &n, &m);
    18     for(int i = 1; i <= n; i ++) {
    19         scanf("%d", &val[i]);
    20     }
    21     sort(val + 1, val + n + 1, cmp);
    22     for(int i = 1; i <= n; i ++) {
    23         for(int j = m; j >= val[i]; j --) {
    24             if(dp[j] <= dp[j - val[i]] + val[i]) {
    25                 vis[i][j] = true;
    26                 dp[j] = dp[j - val[i]] + val[i];
    27             }
    28         }
    29     }
    30     if(dp[m] != m) printf("No Solution
    ");
    31     else {
    32         vector <int> ans;
    33         int idx = n;
    34         while(m) {
    35             if(vis[idx][m]) {
    36                 ans.push_back(val[idx]);
    37                 m -= val[idx];
    38             }
    39             idx --;
    40         }
    41         for(int i = 0; i < ans.size(); i ++) {
    42             if(i) printf(" ");
    43             printf("%d", ans[i]);
    44         }
    45     }
    46     return 0;
    47 }
     
  • 相关阅读:
    结构体struct和typedef后面接指针的含义
    C++中关于指针初始化和使用NULL的理解
    (虚)继承类的内存占用大小
    为什么构造函数不能为虚函数
    C++中变量自动初始化的问题
    CY7C68013A的一点总结
    Altium designer总结
    在Linux系统上限制远程登录的IP
    linux系统如何限制其他用户登录
    使用秘钥对登录Linux系统
  • 原文地址:https://www.cnblogs.com/bianjunting/p/13063115.html
Copyright © 2011-2022 走看看