zoukankan      html  css  js  c++  java
  • Necessary Coins

    Description



    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 ≤ 10 4), followed by n distinct integer numbers ai (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 x martian 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.

    Sample Input

    sample input
    sample output
    5 18
    1 2 3 5 10
    
    2
    5 10
    
    // 这个题我们转化为完全背包来做
    //每次我们把一个钱币去掉,然后判断是不是可以装满背包
    //如果可以则这个钱币不是必要的的,反之就是答案
    //ps:开始想错;用了set ==调了好久=>_<= ,对动态规划没有经验==
    //做题太少了。。。。
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<string>
    #include<stack>
    #include<vector>
    #include<queue>
    #include<algorithm>
    #include<cmath>
    #include<map>
    #include<set>
    #define maxn 10010
    #define LL long long 
    using namespace std ;
    int a[231] , b[312];
    int vi[maxn] ,ans[201];
    vector<int>q ;
    int dp[maxn] ;
    struct node
    {
     int val , id ;
     bool operator < ( const node & s )const 
     {
        return val < s.val || val == s.val && id < s.id ;
     }
    }qe[213];
    
    set<node>s[maxn] ;
    set<node>::iterator it ,ii ;
    int main()
    {
     int i , m , k , x , j , n, len ,mun ;
     while( scanf("%d%d" , &n , &x ) != EOF )
     {
      mun = 0 ;
      for( i = 1 ; i <= n ;i++ )
      {
       scanf("%d" , &a[i]) ;
      }
      for( k = 1 ; k <= n ;k++ )
      {
       memset(dp,-1,sizeof(dp)) ;
       dp[0] = 0 ;
       for( i = 1 ; i <= n ;i++ )
        for( j = x ; j >= 0 ;j-- )
        {   
         if( i == k )continue ;
         if( j - a[i] < 0) continue ;
         if(dp[j-a[i]] != 0 ) continue ;
         // dp[i] 为0 说明可以装满 i 的背包
         //因为不用求最大值,所以令为0就好了..
         //cout << i << " " << j << endl ;
         dp[j] = 0 ;
        }
        if(dp[x] != 0 )
        {  
         q.push_back(a[k]) ;
        }
    
      }
      sort(q.begin(),q.end()) ;
      cout << q.size() << endl ;
      if(q.size() == 0 ) continue ;
      for( i = 0 ; i < q.size()-1 ;i++ )
       printf("%d ",q[i]) ;
      cout << q[q.size()-1] << endl ;
     }
    }
    
  • 相关阅读:
    常见规格液晶显示器尺寸/点距/分辨率
    Disk genius(Diskgenius)修复硬盘分区表
    IIS上注册.Net
    PowerDesigner中如何导入表结构
    关于VS命名空间的引用
    启动Word时出现“复制文件或文件夹时出错"对话框?
    能够删除的安卓(Android)系统自带程序详细列表和说明
    五种方法 解决Windows最大终端连接数
    查看本机打开的端口
    RAID技术概述
  • 原文地址:https://www.cnblogs.com/20120125llcai/p/3354848.html
Copyright © 2011-2022 走看看