• 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 ;
     }
    }
    
  • 相关阅读:
    python学习笔记- day10-4【问题:为什么python的多线程不能利用多核CPU?】
    python学习笔记-day10-3【日志模块logging】
    python学习笔记-day10-2【多进程,多线程】
    python学习笔记-day10-01-【 类的扩展: 重写父类,新式类与经典的区别】
    day12-HTML基础之DOM操作
    day12-HTML基础之CSS
    HTTP原理
    day12-HTML
    day11-Jenkins
    day11-python学习笔记(二十六)yaml,ddt
  • 原文地址:https://www.cnblogs.com/20120125llcai/p/3354848.html
Copyright © 2020-2023  润新知