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 ;
     }
    }
    
  • 相关阅读:
    图像处理之霍夫变换(直线检測算法)
    Linux makefile 教程 很具体,且易懂
    POJ 2001 Shortest Prefixes(字典树)
    android performClick使用
    Java工厂模式
    C#异步调用
    答读者问(5):有关数学对程序猿的作用、研发工作岗位要求和实习对找工作的影响等问题
    Android系统源码导入到eclipse
    开源项目与许可证
    Java实现第十届蓝桥杯矩形切割
  • 原文地址:https://www.cnblogs.com/20120125llcai/p/3354848.html
Copyright © 2011-2022 走看看