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; }



    
       
    
  • 相关阅读:
    日活跃用户统计函数
    统计学习方法(五)——决策树
    统计学习方法(四)——朴素贝叶斯法
    统计学习方法(三)——K近邻法
    统计学习方法(二)——感知机
    Hive UDAF开发之同时计算最大值与最小值
    hive UDAF开发入门和运行过程详解(转)
    Linux操作系统启动流程
    Linux目录的作用
    Linux分区
  • 原文地址:https://www.cnblogs.com/yjbjingcha/p/6915297.html
Copyright © 2011-2022 走看看