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 ;
     }
    }
    
  • 相关阅读:
    Codeforces Round #497 (Div. 2) B. Turn the Rectangles
    Codeforces Round #497 (Div. 2) A. Romaji
    [DFS] [洛谷] P1219 八皇后 O(1)判断
    [归并] [STL] [洛谷] P1309 瑞士轮
    [贪心] [洛谷] P1803 凌乱的yyy / 线段覆盖
    [模拟] [洛谷] P1071 潜伏者
    java.io.EOFException: End of File Exception between local host is: "master/192.168.***.***"; destination host is: “master”:9000; :
    hadoop 无法打开网页和重新格式化问题
    hadoop平台环境搭建
    无法创建新虚拟机: 无法打开配置文件“F:BigDatavm12centos01centos01.vmx”: 拒绝访问。
  • 原文地址:https://www.cnblogs.com/20120125llcai/p/3354848.html
Copyright © 2011-2022 走看看