【HDU - 1455】Sticks (dfs + 剪枝)

题干:

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 file 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

解题报告:

     这题显然是需要优雅的剪枝的。去掉剪枝1会T,去掉剪枝2会280ms。这份代码是参考的网络,二分查找位置。其实这也可以优化一下,让我们凑每一根的时候都是从上一根+1的地方开始选,然后凑新的一根的时候,从头开始查找第一个vis不等于1的木棒,继续递归就行了。这种方法就跟【HDU - 1518】Square 的处理方法其实就差不多了。

 

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
const int MAX=10010;
int a[MAX],n;
bool vis[MAX];
int sum;
int div(int x) {int l=1,r=n;int mid = (l+r)/2;while(l<r) {mid=(l+r)/2;if(a[mid]>x) l=mid+1;else r=mid;}return l;
}
bool cmp(const int & a,const int & b) {return a>b;
}bool dfs(int cnt,int len,int d) {if(cnt==sum/len) return 1;int pos = lower_bound(a+1,a+n+1,len-d,cmp) - a; for(int i=pos; i<=n; i++) {if(vis[i]) continue;if(d+a[i]<len) {vis[i]=1;if(dfs(cnt,len,d+a[i])) return 1;vis[i]=0;if(d==0) return 0;//剪枝1}else if(d+a[i]==len) {vis[i]=1;if(dfs(cnt+1,len,0)) return 1;vis[i]=0;return 0;//剪枝2}}return 0;
}int main() {while(~scanf("%d",&n)) {if(n == 0) break;memset(vis,0,sizeof(vis));sum=0;for(int i=1; i<=n; i++) scanf("%d",a+i),sum+=a[i];sort(a+1,a+1+n,cmp);for(int i=a[1]; i<=sum; i++) {memset(vis,0,sizeof(vis));if(sum%i!=0) continue;if(dfs(0,i,0)) {printf("%d\n",i);break;}}}return 0;
}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/441695.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

java找不到符号类socket,编译报错+解决方法:错误: 找不到符号

public class ServerPlainTest { // 包内包外可见public static void main(String[] args) {try {ServerSocket ss new ServerSocket(8189);System.out.println("the server has startuped, waiting for connections.");while (true) { // accept multiple clients …

php hbase thrift,PHP使用Thrift操作Hbase

系统架构图HBase 启动 Thrift服务hbase启动thrift服务// 进入安装的hbase bin目录下// 执行hbase-daemon.sh start thrift2需要注意的是&#xff0c;这里启动的是thrift2服务&#xff0c;如果需要启动thrift服务只需要将thrift2改为thrift就可以了&#xff0c;具体thrift和thri…

【CodeForces - 761D 】Dasha and Very Difficult Problem (构造,思维)

题干&#xff1a; Dasha logged into the system and began to solve problems. One of them is as follows: Given two sequences a and b of length n each you need to write a sequence c of length n, the i-th element of which is calculated as follows: ci  bi -…

php excel下载打不开了,php下载excel无法打开的解决方法

php下载excel文件,1、在下载的过程中不要 输出任何非文件信息&#xff0c;比如 echo log信息。 否则下载后的文件无法打开&#xff0c;提示格式错误或者文件被破坏。2、 输出的excel格式一定要和后缀名保存一直&#xff0c;否也会提示格式错误或者文件被破坏if (file_exists(CA…

【CodeForces - 761B】Dasha and friends (思维,模拟,构造)

题干&#xff1a; Running with barriers on the circle track is very popular in the country where Dasha lives, so no wonder that on her way to classes she saw the following situation: The track is the circle with length L, in distinct points of which there…

php字符串变量,PHP 字符串变量

PHP 字符串变量字符串变量用于存储并处理文本。PHP 中的字符串变量字符串变量用于包含有字符的值。在创建字符串之后&#xff0c;我们就可以对它进行操作了。您可以直接在函数中使用字符串&#xff0c;或者把它存储在变量中。在下面的实例中&#xff0c;我们创建一个名为 txt 的…

【CodeForces - 761C】Dasha and Password (暴力可过,标解dp,字符串,有坑总结)

题干&#xff1a; After overcoming the stairs Dasha came to classes. She needed to write a password to begin her classes. The password is a string of length n which satisfies the following requirements: There is at least one digit in the string,There is a…

php4和php5的区别,什么是PHP 4和PHP 5之间的区别是什么-php是什么文件

&#xff1f;尽管PHP 5是故意设计成兼容尽可能与以前的版本&#xff0c;也有一些显著的变化。 其中的一些变化包括&#xff1a; 一个新的OOP模型基础上&#xff0c;Zend引擎2.0 改进MySQL支持的一个新推广 内置SQLite的原生支持 一个新的错误报告不断&#xff0c; E_STRICT &am…

【HDU - 2717】【POJ - 3278】Catch That Cow (经典bfs,类似dp)

题干&#xff1a; Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a point N (0 ≤ N ≤ 100,000) on a number line and the cow is at a point K (0 ≤ K ≤ 100,000) on the same number line. Farme…

php 向公众号发送消息,微信公众号之主动给用户发送消息功能

前一段时间项目中遇到一个稍微麻烦一点的问题。即客户要求&#xff0c;他在后台编辑好文章后要主动给每个用户都发送消息&#xff0c;并可以让用户点击直接进入文章页面。于是乎&#xff0c;当时脑子一热&#xff0c;想着没什么大的问题&#xff0c;so easy。模板消息不就得了。…

【CodeForces - 764A】Taymyr is calling you (找规律,水题)

题干&#xff1a; Comrade Dujikov is busy choosing artists for Timofeys birthday and is recieving calls from Taymyr from Ilia-alpinist. Ilia-alpinist calls every n minutes, i.e. in minutes n, 2n, 3n and so on. Artists come to the comrade every m minutes, …

ecshop php升级,升级-安装与升级- ECShop帮助

ECShop V2.6.2版本有GBK和UFT-8两种编码格式的程序&#xff0c;而低于ECShop V2.6.0 的版本只有UTF-8 编码的(这些版本包括 ECShop 2.5.1、ECShop 2.5.0、ECShop 2.1.5、ECShop 2.1.2b等)&#xff0c; 这些版本升级到ECShop V2.6.2均可以使用本程序升级。相同版本的升级只需要覆…

【CodeForces - 764B 】Timofey and cubes (模拟)

题干&#xff1a; Young Timofey has a birthday today! He got kit of n cubes as a birthday present from his parents. Every cube has a number ai, which is written on it. Timofey put all the cubes in a row and went to unpack other presents. In this time, Tim…

php页面转发,php如何实现页面路由转发

php实现页面路由转发的方法&#xff1a;首先配置nginx服务器&#xff0c;在【.htaccess】中写上nginx的语法&#xff1b;然后打开根目录的【index.php】&#xff0c;编写文件路由即可。php实现页面路由转发的方法&#xff1a;1、配置nginx服务器nginx服务器不会自动读取.htacce…

【CodeForces - 764D】Timofey and rectangles (四色定理 + 找规律 + 构造)

题干&#xff1a; One of Timofeys birthday presents is a colourbook in a shape of an infinite plane. On the plane n rectangles with sides parallel to coordinate axes are situated. All sides of the rectangles have odd length. Rectangles cannot intersect, bu…

php整站防注入程序,一个不错的php通用防注入程序

代码如下:function jk1986_checksql(){$bad_str "and|select|update||delete|insert|*";$bad_Array explode("|",$bad_str);/** 过滤Get参数 **/foreach ($bad_Array as $bad_a){foreach ($_GET as $g){if (substr_count(strtolower($g),$bad_a) > 0)…

【HDU - 5056】Boring count (尺取法)

题干&#xff1a; You are given a string S consisting of lowercase letters, and your task is counting the number of substring that the number of each lowercase letter in the substring is no more than K. Input In the first line there is an integer T , ind…

java图片上传被旋转,在其他大牛那看到的java手机图片上传旋转问题的解决方法...

// 将图片文件转化为字节数组字符串&#xff0c;并对其进行Base64编码处理//Base64所用包&#xff1a;org.apache.commons.codec.binary.Base64public static String encodeImgageToBase64(File imageFile) {byte[] data null;// 读取图片字节数组try {InputStream in new Fi…

【qduoj】奇数阶幻方 (构造)

题干&#xff1a; C语言_魔方阵 描述 魔方阵是一个古老的智力问题&#xff0c;它要求在一个mm的矩阵中填入1&#xff5e;m2的数字&#xff08;m为奇数&#xff09;&#xff0c;使得每一行、每一列、每条对角线的累加和都相等&#xff0c;如下为5阶魔方阵示例。 15 8 1 24 17…

【牛客161 - A】字符串(尺取法,桶标记法)

题干&#xff1a; 时间限制&#xff1a;C/C 1秒&#xff0c;其他语言2秒 空间限制&#xff1a;C/C 32768K&#xff0c;其他语言65536K 64bit IO Format: %lld 题目描述 小N现在有一个字符串S。他把这这个字符串的所有子串都挑了出来。一个S的子串T是合法的&#xff0c;当且仅…