http://acm.hdu.edu.cn/showproblem.php?pid=1455

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=243

uva开头描述:

307 - Sticks

Time limit: 3.000 seconds

hduoj 开头描述:

E - Sticks

Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu

 
 
题目描述:
 

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 file contains blocks of 2 lines. The first line contains the number of sticks parts after cutting. 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 分析:
uva会TLE , hduoj AC


AC代码:
 #include<algorithm>
#include<cstring>
#include<cstdio>
#include<iostream>
using namespace std; const int N = 1e4+;
int len[N],sum,L,T;
int used[N]; int cmp(const void *a,const void *b)
{
return *(int *)b-*(int *)a;
} bool DFS(int m,int left)
//m为剩余的木棒数,left为当前正在拼接的木棒和假定的木棒长度L还缺少的长度
{
if(m == && left == )
return true;
if(left == )//一根刚刚拼完
left = L;
for(int i=; i<T; i++)
{
if(!used[i] && len[i]<=left)
{
if(i>)//如果前者已经出现过的不能用,则当前的也不能用
{
if(!used[i-] && len[i] == len[i-])
continue;
}
used[i] = ;
if(DFS(m-,left-len[i]))
return true;
else
{
used[i] = ;
if(len[i] == left || left == L)
return false;
}
}
}
return false;
} int main()
{
while(scanf("%d",&T) && T)
{ sum = ;
for(int i=;i<T;i++)
{
scanf("%d",&len[i]);
sum = sum + len[i];
}
//sort(len,len+T,cmp); sort 超时
qsort(len,T,sizeof(int),cmp); //从大到小排序
for(L = len[];L<=sum/;L++)
{
if(sum%L)
continue;
memset(used,,sizeof(used));
if(DFS(T,L))
{
printf("%d\n",L);
break;
}
}
if(L>sum/)
printf("%d\n",sum);
}
return ;
}

hduoj 1455 && uva 243 E - Sticks的更多相关文章

  1. uva 10003 Cutting Sticks 【区间dp】

    题目:uva 10003 Cutting Sticks 题意:给出一根长度 l 的木棍,要截断从某些点,然后截断的花费是当前木棍的长度,求总的最小花费? 分析:典型的区间dp,事实上和石子归并是一样的 ...

  2. UVA 10003 Cutting Sticks 区间DP+记忆化搜索

    UVA 10003 Cutting Sticks+区间DP 纵有疾风起 题目大意 有一个长为L的木棍,木棍中间有n个切点.每次切割的费用为当前木棍的长度.求切割木棍的最小费用 输入输出 第一行是木棍的 ...

  3. uva 215 hdu 1455 uvalive5522 poj 1011 sticks

    //这题又折腾了两天 心好累 //poj.hdu数据极弱,找虐请上uvalive 题意:给出n个数,将其分为任意份,每份里的数字和为同一个值.求每份里数字和可能的最小值. 解法:dfs+剪枝 1.按降 ...

  4. uva 10003 Cutting Sticks(区间DP)

    题目连接:10003 - Cutting Sticks 题目大意:给出一个长l的木棍, 再给出n个要求切割的点,每次切割的代价是当前木棍的长度, 现在要求输出最小代价. 解题思路:区间DP, 每次查找 ...

  5. UVa 10003 - Cutting Sticks(区间DP)

    链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  6. UVA 10003 Cutting Sticks(区间dp)

    Description    Cutting Sticks  You have to cut a wood stick into pieces. The most affordable company ...

  7. UVA 10003 Cutting Sticks 切木棍 dp

    题意:把一根木棍按给定的n个点切下去,每次切的花费为切的那段木棍的长度,求最小花费. 这题出在dp入门这边,但是我看完题后有强烈的既是感,这不是以前做过的石子合并的题目变形吗? 题目其实就是把n+1根 ...

  8. uva 10003 Cutting Sticks (区间dp)

    本文出自   http://blog.csdn.net/shuangde800 题目链接:  打开 题目大意 一根长为l的木棍,上面有n个"切点",每个点的位置为c[i] 要按照一 ...

  9. UVA 10003 Cutting Sticks

    题意:在给出的n个结点处切断木棍,并且在切断木棍时木棍有多长就花费多长的代价,将所有结点切断,并且使代价最小. 思路:设DP[i][j]为,从i,j点切开的木材,完成切割需要的cost,显然对于所有D ...

随机推荐

  1. 源码解析-Volley(转自codeKK)

    Volley 源码解析 本文为 Android 开源项目源码解析 中 Volley 部分项目地址:Volley,分析的版本:35ce778,Demo 地址:Volley Demo分析者:grumoon ...

  2. PDO连接数据库

    PDO连接数据库 PDO简介和配置 php.ini extension=php_pdo.dll extension=php_pdo_myysql.dll PDO连接MYSQL new PDO(&quo ...

  3. BZOJ1024&丝帛搜索

    题意: 还想二分的,发现并不用...那么直接搜索就水过吧... /*================================================================= ...

  4. Shader实例:2D流光

    准备: 1.一张背景图 2.一张流光图 3.一张过滤图 like this: 效果: 代码: Shader "Custom/2d_flow" { Properties { _Mai ...

  5. 立flag

    lixintong这半年来一直浪啊浪啊都不认真做题!!!!!!简直是太堕落啦!!lixintong非常讨厌这样的lixintong !!! 鉴于lixintong NOIP 完全爆炸啦! lixint ...

  6. Hive_数据倾斜处理

    Hive中三种join map join (小表join大表,将小表加入到内存) 设置map join: hive.auto.convert.join=true hive.mapjoin.smallt ...

  7. Unity学习疑问记录之将图切割保存

    http://blog.csdn.net/poem_of_sunshine/article/details/43036553

  8. apidoc

    1.安装node http://nodejs.cn/download/ 下载二进制包,解压,配置环境 export NODE_HOME=/usr/local/nodeexport PATH=$NODE ...

  9. 球谐光照(Spherical Harmonics Lighting)及其应用-实验篇

    简介 之前在一篇实时深度图优化的论文中看到球谐光照(Spherical Harmonics Lighting)的应用,在查阅了许许多多资料之后还是无法完全理解,我个人觉得如果之前对实时渲染技术不是很了 ...

  10. js 滚动的文字(走马灯)

    // 滚动的文字 function marqueeScroll() { //var $target = $(".marquee_outer:visible"); if($(&quo ...