zoukankan      html  css  js  c++  java
  • SGU 415. Necessary Coins ( 背包dp )

    题意大概是:给出N个硬币, 面值为a_i, 问要凑成X元哪些硬币是不可或缺的.1 ≤ N ≤ 200, 1 ≤ x ≤ 10^4

    直接枚举, 然后就是01背包了. 为了不让复杂度多乘个N, 我们就从左往右, 从右往左分别dp一次.这样判断一个硬币就是O(X).总时间复杂度O(NX)

    -----------------------------------------------------------------------------

    #include<bits/stdc++.h>
     
    using namespace std;
     
    const int maxn = 209;
    const int maxv = 10009;
     
    bool L[maxn][maxv], R[maxn][maxv];
    int v[maxn], N, V;
    vector<int> ans;
     
    int main() {
    memset(L, false, sizeof L);
    memset(R, false, sizeof R);
    cin >> N >> V;
    for(int i = 1; i <= N; i++)
       scanf("%d", v + i);
    L[0][0] = R[N + 1][0] = true;
    for(int i = 1; i <= N; i++) {
    for(int j = 0; j <= V; j++)
       L[i][j] = L[i - 1][j];
    for(int j = V; j >= v[i]; j--)
       L[i][j] |= L[i - 1][j - v[i]];
    }
    for(int i = N; i; i--) {
    for(int j = 0; j <= V; j++)
       R[i][j] = R[i + 1][j]; 
    for(int j = V; j >= v[i]; j--)
       R[i][j] |= R[i + 1][j - v[i]];
    }
    for(int i = 1; i <= N; i++) {
    bool F = false;
    for(int j = 0; j <= V; j++)
       if(L[i - 1][j] && R[i + 1][V - j]) {
       F = true;
       break;
    }
    if(!F) ans.push_back(v[i]);
    }
    printf("%d ", ans.size());
    for(int i = 0; i < ans.size(); i++)
       printf("%d ", ans[i]);
    return 0;
    }

    ----------------------------------------------------------------------------- 

    415. Necessary Coins

    Time limit per test: 1.25 second(s)
    Memory limit: 262144 kilobytes
    input: standard
    output: standard



    Vasya has been on vacation on Mars. He's a big fan of foreign coins, and thus has collected exactly one martian coin of each denomination, for a total of n coins: a1 martian dollars, a2 martian dollars, etc, an martian dollars. Unfortunately, he couldn't stand ordering the Pan Galactic Gargle Blaster at the Starport, and has to pay for it — it costs x martian dollars. Vasya is wondering which of his coins are absolutely necessary to do so (i.e., he is forced to abandon them). They don't offer change at the Starport Mars.

    Input
    The input file contains two integer numbers n and x (1 ≤ n ≤ 200, 1 ≤ x ≤ 104), followed by n distinct integer numbersai (1 ≤ ai ≤ x).

    Output
    On the first line of output, print the amount of denominations of coins that appear in any subset that sums to xmartian dollars. On the second line of output, print the denominations themselves, in any order, separated with single spaces. It is guaranteed that there exists at least one way to pay x martian dollars with the given coins.

    Example(s)
    sample input
    sample output
    5 18 1 2 3 5 10 
    2 5 10 
  • 相关阅读:
    Android下加载GIF图片
    拍照、相册及裁剪的终极实现(一)——拍照及裁剪功能实现
    阿里巴巴矢量库
    ActiveAndroid 管理数据库
    利用box-shadow制作loading图
    适用于移动端的地址选择器
    常用的不易记忆的css自定义代码
    关于js中一个对象当做参数传递是按值传递还是按引用传递的个人看法
    JavaScript之函数柯里化
    CSS3实现图片渐入效果
  • 原文地址:https://www.cnblogs.com/JSZX11556/p/4719198.html
Copyright © 2011-2022 走看看