zoukankan      html  css  js  c++  java
  • POJ 1742 Coins

    Description
    People in Silverland use coins.They have coins of value A1,A2,A3...An Silverland dollar.One day Tony opened his money-box and found there were some coins.He decided to buy a very nice watch in a nearby shop. He wanted to pay the exact price(without change) and he known the price would not more than m.But he didn't know the exact price of the watch.
    You are to write a program which reads n,m,A1,A2,A3...An and C1,C2,C3...Cn corresponding to the number of Tony's coins of value A1,A2,A3...An then calculate how many prices(form 1 to m) Tony can pay use these coins.

    题目大意:一个多重背包
    解题报告:恶心的卡常题
    几个优化:
    1.对于容量×价值>=m的我们可以直接当成完全背包来做,否则直接分组
    2.memset要删掉,在统计答案时就清空

    #include <algorithm>
    #include <iostream>
    #include <cstdlib>
    #include <cstring>
    #include <cstdio>
    #include <cmath>
    #define RG register
    #define il inline
    #define iter iterator
    #define Max(a,b) ((a)>(b)?(a):(b))
    #define Min(a,b) ((a)<(b)?(a):(b))
    using namespace std;
    typedef long long ll;
    const int N=3005,M=1e5+5;
    bool f[M];int n,m,val[N],tot=0,pv[105],pw[105];
    void work()
    {
    	ll tmp;
    	int k=1;tot=0;f[0]=1;
    	for(int i=1;i<=n;i++)scanf("%d",&pv[i]);
    	for(int i=1;i<=n;i++)scanf("%d",&pw[i]);
    	for(int i=1;i<=n;i++){
    		tmp=(ll)pv[i]*pw[i];
    		if(tmp>=m){
    			for(int j=pv[i];j<=m;j++)f[j]|=f[j-pv[i]];
    			continue;
    		}
    		k=1;
    		while(pw[i]>=k){
    			val[++tot]=pv[i]*k;
    		  pw[i]-=k;k<<=1;
    		}
    		if(pw[i])val[++tot]=pv[i]*pw[i];
    	}
    	for(int i=1;i<=tot;i++){
    		for(int j=m;j>=val[i];j--){
    			if(f[j])continue;
    			f[j]|=f[j-val[i]];
    		}
    	}
    	int ans=0;
    	for(int i=1;i<=m;i++)ans+=f[i],f[i]=0;
    	printf("%d
    ",ans);
    }
    
    int main()
    {
    	while(~scanf("%d%d",&n,&m) && n+m)work();
    	return 0;
    }
    
    
  • 相关阅读:
    Google官方关于Android架构中MVP模式的示例续-DataBinding
    值不值
    [译]Godot系列教程五
    [译]Google官方关于Android架构中MVP模式的示例
    遭遇Web print
    如何寻找有价值的点
    充电时间 Go中的数组、切片、map简单示例
    一段良好的程序永远不应该发生panic异常
    居然是Firefox没有抛弃我们
    macOS 升级到了10.12.1
  • 原文地址:https://www.cnblogs.com/Yuzao/p/7499599.html
Copyright © 2011-2022 走看看