hdu 1455(DFS+好题+经典)
Sticks
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 9414 Accepted Submission(s): 2776
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 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.
5 2 1 5 2 1 5 2 1
4
1 2 3 4
0
5
题意:给多组数据,每组数据代表一些小木棍,能否将它们全部用完组成(尽量)多个相同长度的长棍,并输出长棍的长度。
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
#include <stdlib.h>
using namespace std; int n,sum,num;
int v[];
bool vis[];
int cmp(int a,int b){
return a>b;
}
bool dfs(int len,int l,int cnt,int pos){
if(cnt==num) return true;
for(int i=pos;i<n;i++){
if(vis[i]) continue;
if(len==l+v[i]){
vis[i] = true;
if(dfs(len,,cnt+,)) return true;
vis[i] = false;
return false; ///加了这个才能过poj,这个代表如果当前长度的木棍以后的木棍都拼不出这个长度了,那么就没必要试这个长度了,(后面都比当前小...)直接跳出..ORZ
}else if(len>l+v[i]){
vis[i] = true;
if(dfs(len,l+v[i],cnt,i+)) return true;
vis[i] = false;
if(l==) return false;
while(v[i]==v[i+])i++;
}
}
return false;
}
int main(){
while(scanf("%d",&n)!=EOF,n){
sum = ;
for(int i=;i<n;i++){
scanf("%d",&v[i]);
sum+=v[i];
}
sort(v,v+n,cmp);
int ans = sum;
for(int i=v[];i<=sum;i++){
if(sum%i!=) continue;
num = sum/i;
memset(vis,false,sizeof(vis));
if(dfs(i,,,)) {
ans = i;
break;
}
}
printf("%d\n",ans);
}
}
hdu 1455(DFS+好题+经典)的更多相关文章
- [HDU]1016 DFS入门题
题目的意思就是在1到n的所有序列之间,找出所有相邻的数相加是素数的序列.Ps:题目是环,所以头和尾也要算哦~ 典型的dfs,然后剪枝. 这题目有意思的就是用java跑回在tle的边缘,第一次提交就tl ...
- hdu 1455 Sticks
Sticks Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Statu ...
- Snacks HDU 5692 dfs序列+线段树
Snacks HDU 5692 dfs序列+线段树 题意 百度科技园内有n个零食机,零食机之间通过n−1条路相互连通.每个零食机都有一个值v,表示为小度熊提供零食的价值. 由于零食被频繁的消耗和补充, ...
- POJ 1321 棋盘问题(DFS板子题,简单搜索练习)
棋盘问题 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 44012 Accepted: 21375 Descriptio ...
- poj1564 Sum It Up dfs水题
题目描述: Description Given a specified total t and a list of n integers, find all distinct sums using n ...
- HDU 5143 DFS
分别给出1,2,3,4 a, b, c,d个 问能否组成数个长度不小于3的等差数列. 首先数量存在大于3的可以直接拿掉,那么可以先判是否都是0或大于3的 然后直接DFS就行了,但是还是要注意先判合 ...
- Educational Codeforces Round 12补题 经典题 再次爆零
发生了好多事情 再加上昨晚教育场的爆零 ..真的烦 题目链接 A题经典题 这个题我一开始推公式wa 其实一看到数据范围 就算遍历也OK 存在的问题进制错误 .. 思路不清晰 两个线段有交叉 并不是端点 ...
- 咸鱼的ACM之路:DFS水题集
DFS的核心就是从一种状态出发,转向任意的一个可行状态,直到达到结束条件为止.(个人理解) 下面全是洛谷题,毕竟能找到测试点数据的OJ我就找到这一个....在其他OJ上直接各种玄学问题... P159 ...
- HDU 1312 Red and Black(DFS,板子题,详解,零基础教你代码实现DFS)
Red and Black Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...
随机推荐
- 项目管理---git----快速使用git笔记(六)------本地开发与远程仓库的交互----常用git命令
无论是我们自己把本地的项目新建了一个远程仓库 还是 从远程仓库获取到了 本地,现在我们都在本地有了一份项目代码,服务器上对应有项目代码的信息. 现在我们就开始进行交互操作了. 也就是说明一些在 正常开 ...
- 一篇博文将JavaScript尽收眼底
简介 这篇文章是为专业程序员介绍的JavaScript语言的,它是一种小巧的语言,如果你熟悉其他的编程语言,那么这篇文章对你来讲不是那么难以理解. JavaScript不是Java,他们是两门完全不同 ...
- Linux IO Scheduler
一直都对linux的io调度算法不理解,这段时间一直都在看这方面的内容,下面是总结和整理的网络上面的内容.生产上如何建议自己压一下.以实际为准. 每个块设备或者块设备的分区,都对应有自身的请求队列(r ...
- HDU 5650 异或
so easy Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Sub ...
- 清北学堂模拟赛d6t6 棋盘迷宫
3.棋盘迷宫(boardgame.pas/c/cpp)(boardgame.in/out)时间限制:5s/空间限制:256M[题目描述]小 A 和小 Z 是非常要好的朋友, 而且他们都对迷宫游戏非常有 ...
- bzoj 3714 [PA2014]Kuglarz 最小生成树
[PA2014]Kuglarz Time Limit: 20 Sec Memory Limit: 128 MBSubmit: 1335 Solved: 672[Submit][Status][Di ...
- ACM1325Is it A tree?
通过这道简单而又坑人的题目,练习并查集和set 容器的使用: Is It A Tree? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: ...
- Object.defineProperty 与 属性描述符
为JavaScript对象新增或者修改属性,有两种不同方式:直接使用=赋值或者使用Object.defineProperty 定义,使用后者的话还可以设置属性的描述符. Object.definePr ...
- Small Multiple
题目描述 Find the smallest possible sum of the digits in the decimal notation of a positive multiple of ...
- zk-web
Ref:https://github.com/qiuxiafei/zk-web zk-web是一个用clojure with noir and boostrap写的Zookeeper WEB UI管理 ...