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 ;
     }
    }
    
  • 相关阅读:
    《设计原本》读书笔记01
    SQL SERVER存储过程的几种示例
    SQLSERVER2008 存储过程基本语法
    (转)C#程序开发中经常遇到的10条实用的代码
    (转)C#正则表达式Regex类的用法
    常用正则表达式
    (转)通过WMI获取网卡MAC地址、硬盘序列号、主板序列号、CPU ID、BIOS序列号
    checkbox:全选与反全选
    checkbox:获取所有已选中的值
    Quartz(任务调度)- Cron
  • 原文地址:https://www.cnblogs.com/20120125llcai/p/3354848.html
Copyright © 2011-2022 走看看