Sticks
来源:互联网 发布:mac系统卡死怎么办 编辑:程序博客网 时间:2024/05/22 14:07
算法:dfs+剪枝;
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
Source
Central Europe 1995
;
代码:
#include <iostream> #include <cstring>#include <iomanip>#include <algorithm>#include <string>#include <stdio.h>using namespace std;int n,flag;int dis[70],vis[70];int cmp(int x,int y){return x>y;} void dfs(int ps,int dep,int ssum,int x){if(flag) return ;if(ssum==0){int k=0;while(vis[k]) k++;vis[k]=1;dfs(ps,dep+1,ssum+dis[k],k+1);vis[k]=0; return ;//}if(ssum==ps){if(dep==n) {flag=1;return;}else dfs(ps,dep,0,0);//return;}for(int i=x;i<n;i++){if(!vis[i]&&ssum+dis[i]<=ps){if(!vis[i-1]&&dis[i]==dis[i-1]) continue;vis[i]=1;dfs(ps,dep+1,ssum+dis[i],i+1);vis[i]=0; }}return ;}int main(){int i,j,k,m,sum;while(cin>>n&&n){flag=sum=0;for(i=0;i<n;i++){cin>>dis[i];sum+=dis[i];}sort(dis,dis+n,cmp);for(i=dis[n-1];i<=sum/2;i++){if(sum%i==0){memset(vis,0,sizeof(vis)); dfs(i,0,0,0);}if(flag) {cout<<i<<endl;break;}}if(!flag) cout<<sum<<endl;}return 0;}
- Sticks
- sticks
- sticks
- sticks
- Sticks
- sticks
- Sticks
- Sticks
- Sticks
- Sticks
- Sticks
- Sticks
- Sticks
- Sticks
- Sticks
- sticks
- Sticks
- 1011 Sticks
- My97DatePicker日期控件使用
- 学习方法总结
- java线程(2)——模拟生产者与消费者
- DBCP连接池配置参数说明
- Java基础-数组的基本操作(1)
- Sticks
- 游戏逻辑底层,MainLoop&&FSM&&MSG(三)
- 对自己找工作的建议
- Java泛型详解
- js中的“十二行诗” ----12行JS代码的DoS攻击分析及其防御
- C文件流I/O操作:FILE类型
- 我的MyEclipse文档注释
- 使用 AMD、CommonJS 及 ES Harmony 编写模块化的 JavaScript
- oracle查询数据库名、实例名等