Day2-G-Sticks-POJ1011
Input
Output
Sample Input
9
5 2 1 5 2 1 5 2 1
4
1 2 3 4
0
Sample Output
6
5 简述:给你n个木棒及其长度,求其能组成的最短的相同长度的木棒的长度。
分析:既然是最短搜索,用DFS+回溯即可,找出其最大的木棒长度,其可能的长度在最大长与长度和之间,与F题不同,这题需要有三点剪枝才能AC
1.不重复访问。
2.若长度为a的木棒不满足题意,则大于等于它的都不会满足,例如:2 2 2 2 1,若第一个2不满足,则需要直接跳过到1.
3.因为我们将木棒降序排序,第一根(最大)必然会用上,如果第一根木棒未被用上直接剪枝退出即可
注意考虑其只合成一根木棒的情况,代码如下:
const int maxm = ;
int vis[maxm], n, buf[maxm], tmp, len;
bool comp(int a,int b) {
return a > b;
}
bool dfs(int cur,int pos,int t) {
if(t == tmp)
return true;
if(cur == len)
return dfs(, , t + );
int pre = ;
for (int i = pos; i < n; ++i) {
if(!vis[i]) { // first
int nlen = cur + buf[i];
if(pre != buf[i] && nlen <= len) { // second
pre = buf[i];
vis[i] = ;
if(dfs(nlen,i+,t))
return true;
vis[i] = ;
if(!pos) //third
return false;
}
}
}
return false;
}
int main() {
while(scanf("%d",&n) && n) {
int sum = ;
for(int i = ; i < n; ++i) {
scanf("%d", &buf[i]);
sum += buf[i];
}
sort(buf, buf + n,comp);
for (len = buf[]; len <= sum / ; ++len) { // 若大于sum/2必然只能合成一根木棒
if(sum % len)
continue;
tmp = sum / len;
memset(vis, , sizeof(vis));
if(dfs(,,))
break;
}
if(len > sum / )
printf("%d\n", sum);
else
printf("%d\n", len);
}
return ;
}
Day2-G-Sticks-POJ1011的更多相关文章
- python学习第二天 -----2019年4月17日
第二周-第02章节-Python3.5-模块初识 #!/usr/bin/env python #-*- coding:utf-8 _*- """ @author:chen ...
- DFS系列 POJ(自认为的讲解)
C - Sum It Up POJ1564 题意: 给你一个N,然后给你一堆数The numbers in each list appear in nonincreasing order, and t ...
- Storyboards Tutorial 03
这一节主要介绍segues,static table view cells 和 Add Player screen 以及 a game picker screen. Introducing Segue ...
- 文件图标SVG
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink ...
- poj1011 Sticks (搜索经典好题)
poj1011 Sticks 题目连接: poj1011 Description George took sticks of the same length and cut them randomly ...
- 【poj1011】 Sticks
http://poj.org/problem?id=1011 (题目链接) 题意 给出一大堆小棍子的长度,需要把他们拼成几根长度相等的大棍子,求大棍子的最短长度. Solution 经典搜索题,剪枝剪 ...
- POJ1011 Sticks
Description George took sticks of the same length and cut them randomly until all parts became at mo ...
- poj1011 Sticks(dfs+剪枝)
Sticks Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 110416 Accepted: 25331 Descrip ...
- poj1011 Sticks(DFS+剪枝)
题目链接 http://poj.org/problem?id=1011 题意 输入n根棍子的长度,将这n根棍子组合成若干根长度相同的棍子,求组合后的棍子的最小长度.这题是poj2362的加强版,思路与 ...
- poj1011 Sticks[剪枝题]
https://vjudge.net/problem/POJ-1011 此题很重要.★★★ 很欢(e)乐(xin)的一道搜索剪枝题..poj数据还是太水了,我后来想不出来剪枝方法了,就加了句掐了时间语 ...
随机推荐
- GO面向接口
Go 语言提供了另外一种数据类型即接口,它把所有的具有共性的方法定义在一起,任何其他类型只要实现了这些方法就是实现了这个接口. 实例 实例 /* 定义接口 */ type interface_name ...
- Nexus升级、license安装和恢复密码
原文链接:https://blog.csdn.net/ligang636/article/details/42386639 一.Nexus系列物理硬件1.1 Nexus 7010 1.2 Nexus ...
- 无线连接网络-FAST SSID Change
这篇随笔主要记录的是Apple设备连接思科无线可能出现的问题,尤其是在思科WLC3504下部署的无线网络,这种故障体现的尤为明显. For Single SSID To support Apple i ...
- Java学习资源 - 测试
JUnit注解解释 1. @Test : 测试方法,测试程序会运行的方法,后边可以跟参数代表不同的测试,如(expected=XXException.class) 异常测试,(timeout=xxx) ...
- FileUpload之FileItem类的常用方法
http://blog.csdn.net/chinaliuyan/article/details/7002014
- Java IO流详解(一)——简单介绍
文件在程序中是以流的形式来传输的.所以用Java来传输文件就得使用到Java IO流. 1.流的概念和作用 流:代表任何有能力产出数据的数据源对象或者是有能力接受数据的接收端对象<Thinkin ...
- VUE项目开发中使用WebSocket
初始化WebSocket initWebSocket(){ //初始化weosocket const wsuri = 'ws://10.100.45.8:8888/websocket';//ws地址 ...
- 循环语句(while语句和do...while语句)
1.while语句:如果条件成立,就继续循环,直到条件不成立为止.格式如下: while (条件) { 循环体(语句或语句块) } 2.do…while语句:如果条件成立, ...
- Jquery+ajax模板
$.ajax({ url:'', type:'POST', //GET async:true, //或false,是否异步 data:{ name ...
- Linux命令:top命令
top命令是Linux下常用的性能分析工具,能够实时显示系统中各个进程的资源占用状况,类似于Windows的任务管理器.下面详细介绍它的使用方法.top是一个动态显示过程,即可以通过用户按键来不断刷新 ...