1204 Sticks

时间限制:1000MS  内存限制:10000K
提交次数:0 通过次数:0

题型: 外判编程题   语言: 无限制

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
//C++答案代码如下:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
bool anUsed[65];
int L,N,stick[65];
bool cmp(int a,int b)
{
return a>b;
}
bool DFS(int nUnusedSticks,int nLeft)
{
int i,j,k;
if(!(nUnusedSticks||nLeft)) return 1;//假如都为0,则拼合完成 
if(!nLeft) nLeft=L;//新建一个要拼合的棍 
for(i=0;i<N;++i)
if(!anUsed[i]&&stick[i]<=nLeft){//剪枝 
if(i>0&&!anUsed[i-1]&&stick[i]==stick[i-1]) continue;//剪枝 
anUsed[i]=1;
if(DFS(nUnusedSticks-1,nLeft-stick[i])) return 1;
anUsed[i]=0;//可以重新再使用 
if(stick[i]==nLeft||nLeft==L) return 0;//下一次的递归深入中,刚建了一个新棍,结果后面没有符合条件的棍,
//返回这一层时stick[i]==nLeft,而这一层也要返回 
}            //或者这一层刚开了一个新的棍,但剩余棍中没有符合的棍即nLeft==L,这也需要返回上层 
return 0;
}
int main()
{
int i,totalLength;
while(scanf("%d",&N),N){
for(totalLength=i=0;i<N;++i){
scanf("%d",&stick[i]);
totalLength+=stick[i];
}
sort(stick,stick+N,cmp);//一次剪枝 
for(L=stick[0];L<=(totalLength>>1);++L)
if(totalLength%L==0){ //二次剪枝 
memset(anUsed,0,sizeof(anUsed));
if(DFS(N,L)) break;
if(L>(totalLength>>1)) printf("%d\n",totalLength);//三次剪枝
else printf("%d\n",L);
}
return 0;    
}

转载于:https://www.cnblogs.com/arcfat/archive/2012/10/23/2735787.html

1011 Sticks相关推荐

  1. poj 1011 Sticks 搜索

    发一道老早写的题. 1 #include<iostream> 2 #include <algorithm> 3 using namespace std; 4 int a[65] ...

  2. POJ - 1011 Sticks(dfs+剪枝)(好题!!)

    题目链接:点击查看 题目大意:乔治拿来一组等长的木棍,将他们随机砍断,使得每一节木棍的长度都不超过50个单位长度,然后他又想将这些木棍恢复成砍断之前的状态,但他忘记了初始时有多少根木棍以及木棍的初始长 ...

  3. POJ 1011 Sticks

    POJ_1011 做完这个题目,让我不仅学到了一些别人剪枝的策略,更重要的是让我意识到了在搜索中剪枝的重要性. 所谓剪枝,其实就是对程序的优化,尽可能地避免程序执行无用的操作.就这个题目而言,程序的优 ...

  4. poj 2362 Square

    #include <iostream> //参照poj 1011 sticks#include <algorithm>using namespace std;int stick ...

  5. 《挑战程序设计竞赛(第2版)》习题册攻略

    本项目来源于GitHub 链接: 项目GitHub链接 1 前言 项目为<挑战程序设计竞赛(第2版)>习题册攻略,已完结.可配合书籍或笔记,系统学习算法. 题量:约200道,代码注释内含详 ...

  6. ACM题集以及各种总结大全(转)

    ACM题集以及各种总结大全! 虽然退役了,但是整理一下,供小弟小妹们以后切题方便一些,但由于近来考试太多,顾退役总结延迟一段时间再写!先写一下各种分类和题集,欢迎各位大牛路过指正. 一.ACM入门 关 ...

  7. 搜索题,留着以后慢慢刷

    转过来,留着以后慢 慢 刷555.. 简单搜索 (1)深度优先搜索 (poj2488,poj3009,poj1321) (2)广度优先搜索 (poj3278,poj1426,poj3126,poj30 ...

  8. POJ的题目分类(两个版本)

    版本一: 简单题 1000A+B Problem 1001Exponentiation 1003 Hangover 1004 Financial Management 1005 I Think I N ...

  9. POJ前面的题目算法思路【转】

    1000 A+B Problem 送分题 49% 2005-5-7 1001 Exponentiation 高精度 85% 2005-5-7 1002 487-3279 n/a 90% 2005-5- ...

最新文章

  1. 运维基础(3)备份篇
  2. glance was not installed properly
  3. webstorm创建代码模板
  4. 理解 C++ 的 Memory Order 以及 atomic 与并发程序的关系
  5. 周末也需要学习 分享一个 Flutter 波浪波动效果的登录页面的背景 Flutter ClipPath实现的波动
  6. @kafkalistener中id的作用_无意中测试了下MySQL里面的join操作,发现还是存在理解偏差...
  7. 自己制作精美的App Store 软件截屏
  8. [高光谱] Hyperspectral-Classification-master 网络模型解析
  9. hadoop无法停止
  10. (转载)C/C++:sizeof('a')的值为什么不一样?
  11. 分享磨砺营威哥讲解-Android开发过程中内存优化的几点总结
  12. 【转】阿里巴巴性能测试规划思路
  13. PotPlayer播放器及安装说明
  14. 计算机程序员求职信英语作文,英文程序员求职信
  15. selenium执行js脚本
  16. C++活到老学到老 auto
  17. 基础心理学MOOC|记忆系统以及遗忘规律
  18. VC删除注册表键值项
  19. 张宇1000题高等数学 第十六章 无穷级数
  20. 第一部分 思科九年 一(7)

热门文章

  1. 分布式、微服务、云架构
  2. nlogn求最长不上升子序列
  3. OAuth 2.0介绍
  4. linux Centos下磁盘分区及文件系统创建与挂载
  5. pgAdminIII使用图解
  6. Android Studio 1.1.0汉化初步出炉!
  7. pku1067----取石子游戏(博弈)
  8. text html template判断,template-web中循环 判断 计算
  9. 巡查准确率怎么算_【达睿原创】需求预测准确率,你怎么看 ?
  10. 服务器在线看视频无法播放,上传到服务器的视频不能在线播放怎么办?