sp201604
Published on 2017-09-02 11:31 in 暂未分类 with sp201604

luogu题解 P5020 【货币系统 】

思路

判断钱数是否可以转化为其他钱数的和

与楼下不同,我没有用sort。而是用了一个数组来特判。

思路其实只是简单dp。

详见代码。

#include<cstdio>

using namespace std;

int t;

int main()

{

	scanf("%d",&t);
    
	while(t>0)
    
	{
    
		t--;
        
		int n;
        
		scanf("%d",&n);
        
		int a[30000],d[30000]={0},c=0,f[30000]={0};
        
		for(int i=1;i<=n;i++)
        
		{
        
			scanf("%d",&a[i]);
            
			if(a[i]>c)c=a[i];
            
			d[a[i]]=-1;//注意,d数组开了特判,不用sort原因
            
		}
        
		int jss=n;
        
		for(int i=1;i<=c;i++)//哈希
        
		{
        
			int dd=0;//确保只减一次
            
			for(int j=1;j<=n;j++)
            
			{
            
				if(i-a[j]==0)f[i]=a[j];//f[i]可以设为bool
                
                //f[i]=a[j]中a[j]无实际意义
                if(i-a[j]>0)
                
				if(f[i-a[j]]!=0)
                
				{
                
					if(d[i]==-1&&dd==0)jss--,dd=1;
                    
					f[i]=f[i-a[j]]+a[j];
                    
				}
                
			}
            
		}
        
		printf("%d\n",jss);
        
	}
    
    
}

加油,我们离神犇会越来越近,祝大家noip2018RP++;

posted @ 2019-02-01 18:03  sp201604  阅读(159)  评论(0编辑  收藏  举报