Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1466564
  • 博文数量: 218
  • 博客积分: 6394
  • 博客等级: 准将
  • 技术积分: 2563
  • 用 户 组: 普通用户
  • 注册时间: 2008-02-08 15:33
个人简介

持之以恒

文章分类

全部博文(218)

文章存档

2013年(8)

2012年(2)

2011年(21)

2010年(55)

2009年(116)

2008年(16)

分类:

2010-08-28 11:43:07

ACM1011--解题报告--转
 

Sticks

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.
问题分析:
#include "stdafx.h"
#include
#include
#include
#include
#include
#include
#include
using namespace std;

int data[100];//存储短棍长度
int save[100];//存储长棍已经拼接的长度
int thy;//短棍个数
int total;//短棍长度之和
int max_;//最长短棍长度
int v[100]; //可能的长棍长度
int vk;//可能的长棍长度的个数
int thx;//当前长棍个数
int length;//当前长棍长度
int fill_[10000]; //可能拼接的长度的集合==>用于剪枝

void input()
{
 int i;
 cin>>i;
 if(i==0)
 {
  exit(0);
 }
 thy=i;
 int j;
 max_=0;
 total=0;
 int kk=0;
 
//输入数据
 for(j=0;j<i;j++)
 {
  int temp;
  cin>>temp;
  if(temp>50)
  {
   thy--;
   continue;
  }
  data[kk]=temp;
  total+=data[kk];
  if(data[kk]>max_)
   max_=data[kk];
  kk++;
 }
 if(thy==0)return;
 
//将短棍的长度从大到小排序
 std::sort(data,data+thy);
 std::reverse(data,data+thy);
//大到小排序
 
//计算可能拼接的长度的集合,1表示可以,0表示不可以
 for(i=1;i<total;i++)
  fill_[i]=0;
 fill_[0] = 1;
 int tmax_ = 0;
 for(i=0;i<thy;i++)
 {
  for(j=tmax_;j>=0;j--)
   if(fill_[j]==1)
    fill_[j+data[i]]=1;
  tmax_+=data[i];
 }
}

 
 

//找到可能的原始长棍长度,也就找到了可能的原始长棍的个数
void factor()
{
 int i;
 vk=0;
 for(i = max_;i<=total;i++)
 {
  if(total%i==0)
   v[vk++]=i;
 }
}

 

void clear()
{
 int i;
 for(i=0;i<thx;i++)
  save[i]=0;
}


//将第K个短棍放入长棍
bool solve(int k)
{
 if(k==thy)
//K
  return true;
 int i;
 bool judge = false;
 for(i=0;i<thx;i++)
 {
  
//假设某一根长棍还没有填满的部分的长是ls,并且在还没有使用的短棍中恰好有一根的长度也是ls,
  
//那么我们可以直接把ls填到此根长棍上。
  if(save[i]+data[k] == length)
  {
   
//lk+第i根长棍已填的部分==length==>用于减少组合
   save[i]=length;
   bool t = solve(k+1);
   save[i]-=data[k];
   return t;
  }
  for(i=0;i<thx;i++)
//广度优先
  {
   if(fill_[length-save[i]]==0)
//剪枝
    return false;
   if(save[i]+data[k]<=length)
//
   {
    
//lk+第i根长棍已填的部分<=length
    save[i] += data[k];
//把lk填入第i根长棍
    if(solve(k+1) == true)
//DFS
     return true;
    save[i] -= data[k];
//把lk从第i根长棍中取出,放入其他的长棍,回溯
   }
  }
 }
 return false;
}


void main()
{
 while(1)
 {
  input();
  if(thy==0)
  {
   cout<<0<<endl;
   continue;
  }
  factor();
  int i=0;
  bool judge=false;
  while(i!=vk-1)
  {
   length=v[i++];
   thx=total/length;
   clear();
   if(solve(0))
   {
    judge=true;
    cout<<length<<endl;
    break;
   }
  }
  if(!judge)
  {
   cout<<total<<endl;
  }
 }
}


思路和优化:
基本思路:DFS+回溯
 
逐个填充长棍,填完一根长棍,再填下一根,注意排序(DFS的搜索次序)和剪枝
阅读(1103) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~