zoukankan      html  css  js  c++  java
  • poj1011 Sticks

    Description

    George took sticks of the same length and cut them randomly until all parts became at most 50 units long. Now he wants to return sticks to the original state, but he forgot how many sticks he had originally and how long they were originally. Please help him and design a program which computes the smallest possible original length of those sticks. All lengths expressed in units are integers greater than zero.

    Input

    The input contains blocks of 2 lines. The first line contains the number of sticks parts after cutting, there are at most 64 sticks. The second line contains the lengths of those parts separated by the space. The last line of the file contains zero.

    Output

    The output should contains the smallest possible length of original sticks, one per line.

    Sample Input

    9
    5 2 1 5 2 1 5 2 1
    4
    1 2 3 4
    0
    

    Sample Output

    6
    

    5

    经典搜索题,需要很多剪枝。

    #include<iostream>
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #include<math.h>
    #include<vector>
    #include<map>
    #include<set>
    #include<queue>
    #include<stack>
    #include<string>
    #include<algorithm>
    using namespace std;
    int a[100],vis[100],len,gen,n,ji;
    bool cmp(int a,int b){
    	return a>b;
    }
    
    int dfs(int t,int pos,int cha) //表示正在拼第t根,接下来从pos位置开始拼,拼完这根木棒还需多少长度 
    {
    	int i,j;
    	if(cha==0){
    		if(t==gen)return 1;
    		for(i=1;i<=n;i++){
    			if(!vis[i]){
    				vis[i]=1;
    				if(dfs(t+1,i+1,len-a[i]))return 1; //当前这根一定是剩下最长的一根,且一定要拼 
    				vis[i]=0;break;
    			}
    		}
    	}
    	else{
    		for(i=pos;i<=n;i++){
    			if(i>1 && !vis[i-1] && a[i]==a[i-1])continue; //如果这根木棒和上一根木棒的长度相同,但是上一根没有成功,那么直接跳过这根 
    			
    			if(!vis[i] && a[i]<=cha){
    				vis[i]=1;
    				if(dfs(t,i+1,cha-a[i]))return 1;
    				vis[i]=0;
    				if(a[i]==cha)break; //如果当前这根木棒的长度恰好等于所差的长度,但是搜索不成功,那么就一定不能。 
    			}
    		}
    	}
    	return 0;
    }
    
    int main()
    {
    	int m,i,j,flag,sum;
    	while(scanf("%d",&n)!=EOF && n!=0)
    	{
    		sum=0;ji=0;
    		for(i=1;i<=n;i++){
    			scanf("%d",&a[i]);
    			if(a[i]&1)ji++;
    			sum+=a[i];
    		}
    		if(i==1){
    			printf("%d
    ",sum);continue;
    		}
    		sort(a+1,a+1+n,cmp);  //从大到小排列 
    		flag=0;memset(vis,0,sizeof(vis));
    		for(len=a[1];len<=sum/2;len++){
    			if(sum%len==0){ //必须要整除才行 
    				gen=sum/len;
    			    if(len&1){   //如果总长度是奇数,但个数小于总根数,那么直接下一个循环 
        				if(ji<gen)continue;
        			}
        			if(~len&1){  //如果总长度是偶数,但是有奇数个奇数,那么直接下一个循环 
    			    	if(ji&1)continue;
    			    }
    				if(dfs(1,1,len)){
    					flag=1;printf("%d
    ",len);break;
    				}
    			}
    		}
    		if(!flag)printf("%d
    ",sum);
    	}
    	return 0;
    }


    
                
    
  • 相关阅读:
    Python将文件夹下的文件名写入excel方便统计
    Python利用openpyxl带格式统计数据(2)- 处理mysql数据
    Python利用openpyxl带格式统计数据(1)- 处理excel数据
    spfa 算法(队列优化的Bellman-Ford算法)
    bellman_ford算法(边数限制的最短路,边权可能为负)
    堆优化dijkstra
    朴素dijkstra
    1547. 切棍子的最小成本(区间dp)
    1546. 和为目标值的最大数目不重叠非空子数组数目(前缀和+dp)
    32场双周赛(模拟,模拟,前缀和加状态压缩)
  • 原文地址:https://www.cnblogs.com/herumw/p/9464690.html
Copyright © 2011-2022 走看看