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数据还是太水了,我后来想不出来剪枝方法了,就加了句掐了时间语 ...
随机推荐
- Linux下运行SuperSocket记录
Linux下运行SuperSocket程序步骤 需要将原程序的配置文件中的内容拷贝一份,改名成SuperSocket.SocketService.exe.config1.Linux中运行.Net程 ...
- 吴裕雄 PYTHON 神经网络——TENSORFLOW 学习率的设置
import tensorflow as tf TRAINING_STEPS = 10 LEARNING_RATE = 1 x = tf.Variable(tf.constant(5, dtype=t ...
- 第三节:Vuejs常用特性2和图书案例
一. 常用特性2 1. 监听器 用watch来响应数据的变化, 一般用于异步或者开销较大的操作, watch 中的属性 一定是data 中 已经存在的数据!!! 当需要监听一个对象的改变时,普通的wa ...
- 信息论相关概念:熵 交叉熵 KL散度 JS散度
目录 机器学习基础--信息论相关概念总结以及理解 1. 信息量(熵) 2. KL散度 3. 交叉熵 4. JS散度 机器学习基础--信息论相关概念总结以及理解 摘要: 熵(entropy).KL 散度 ...
- Android抓包分析-fiddler版
本文介绍的是如何使用Fiddler工具抓取Android应用的HTTP协议的数据包 工具 Genymotion模拟器 笔记本电脑一台(Win7) Fiddler(v4.6.2),下载地址:http:/ ...
- JS变量声明提升和函数声明提升
JS代码在执行的时候会先找出执行代码中定义的变量和函数,对其进行声明. 例1:console.log(a); var a = 4; 此时输出undefined.a变量在执行console.log(a) ...
- Spark教程——(10)Spark SQL读取Phoenix数据本地执行计算
添加配置文件 phoenixConnectMode.scala : package statistics.benefits import org.apache.hadoop.conf.Configur ...
- Manthan, Codefest 19 (open for everyone, rated, Div. 1 + Div. 2)B(SET)
#define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>using namespace std;int a[2007];set<int& ...
- Nacos client 客户端cpu占用100% 问题排查和解决方案
Nacos version:1.1.3client version:1.0.0 dependency: 'org.springframework.cloud:spring-cloud-alibaba- ...
- JDBC 操作插入表出现javax.sql.rowset.serial.SerialBlob cannot be cast to oracle.sql.BLOB
/** * 接口方法 */ public void excuteInputDB(SynchServiceConfig synchServiceConfig) throws Exception { tr ...