zoukankan      html  css  js  c++  java
  • ACM: 限时训练题解-Heavy Coins-枚举子集-暴力枚举

    1. Heavy Coins

     

    Bahosain has a lot of coins in his pocket. These coins are really heavy, so he always tries to get rid of some of the coins by using them when paying for the  taxi.

    Whenever Bahosain has to pay S pennies for the taxi driver, he tries to choose the maximum number of coin pieces to pay. The driver will accept receiving more than S pennies only if he can’t remove one or more of the given coins and still has S or more  pennies.

    For example, if Bahosain uses the coins of the following values: 2, 7 and 5 to pay 11 pennies, the taxi driver    will not accept this because the coin of value 2 can be removed. On the other hand, when Bahosain uses coins  of 7 and 5 to pay 11 pennies, the driver will accept  it.

    Note that the driver won’t give Bahosain any change back if he receives more than S pennies, and Bahosain doesn’t care!

    Input

     

    The first line of input contains T (1 ≤ T ≤ 1001), the number of test   cases.

    The first line of each test case contains two integers: N (1 ≤ N ≤ 10) and S (1 ≤ S ≤ 1000), where N is the number of coins in Bahosain’s pocket and S is the amount (in pennies) Bahosain has to pay for the taxi driver.

    The next line contains N space-separated integers between 1 and 100 that represent the values (in pennies) of the coins in Bahosain’s  pocket.

    Output

     

    For each test case, print a single line with the maximum number of coins Bahosain can use to pay for the driver.

    Sample Input

    Sample Output

    2

    3

    5

    9

    6

    4

    1   3

    5

    4

    7

    37

    7

    5   8

    8

    5

    10

    4

    Note

    In the first test case, Bahosain can pay in any of the following ways: (1, 3, 5), (3, 4, 4) or (1, 4, 4).

    /*
    题意:
    A有一大堆的硬币,他觉得太重了,想花掉硬币去坐的士;
    的士司机可以不找零,但是的士司机也不会多收零钱。
    怎么样才能使 A 花的零钱最多。 
     
    思路-暴力
    2^10*1000=1024000  102W 完全不会超时。
    直接暴力枚举 
    
    方法: 
    枚举子集,用二进制思想来枚举子集。 
     
    */ 
    
    #include"iostream"
    #include"algorithm"
    #include"cstdio"
    #include"cstring"
    #include"cmath"
    #define memset(a,b) memset(a,b,sizeof(a))
    #define MX 10000 + 50
    using namespace std;
    int maxx,n,m,a[MX];
    
    bool cmp(int x,int y) {
    	return x>y;
    }
    
    void dfs(int x,int sum,int num) {
    	if(sum>=m) {
    		maxx=max(maxx,num);	//如果花掉的硬币值已经够坐车了,更新最大花掉的硬币数 
    		return ;
    	}
    	if(x==n)return;  //如果已经查询了所有的硬币返回。 
    	dfs(x+1,sum+a[x],num+1);  //递归下去 搜索取下一个硬币的情况 
    	dfs(x+1,sum,num);        //搜索不取下一个硬币的情况
    }
    
    int main() {
    	int T,num;
    	cin>>T;
    	while(T--) {
    		scanf("%d%d",&n,&m);
    		for(int i=0; i<n; i++) {
    			scanf("%d",&a[i]);
    		}
    		maxx=0;
    		sort(a,a+n,cmp);  //按照硬币的枝排序; 
    		dfs(0,0,0);
    		printf("%d
    ",maxx);
    	}
    	return 0;
    }
    

      

  • 相关阅读:
    js 字符串indexOf方法封装
    js 冒泡排序
    CSS定位 position的三个属性 elative 、absolute、fixed :
    让父元素能感知浮动的子元素 #用伪元素清除浮动
    三个路由器的连接,中间路由的配置(静态路由)
    IDEA 添加tomcat出错: Error: Environment variable name is not set 我的解决方法
    通过基于AspectJ 注解的方式实现Spring AOP报 can't find referenced pointcut myPointCut 错误,我的解决方法
    C语言fopen函数打开文本文件与二进制文件的区别
    位运算的奇技淫巧 系列1
    位运算例子(以后会逐渐补充)
  • 原文地址:https://www.cnblogs.com/HDMaxfun/p/5709521.html
Copyright © 2011-2022 走看看