Sticks

时间限制:3000 ms  |  内存限制:65535 KB
难度:5
 
描述
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.
 
输入
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.
输出
The output should contains the smallest possible length of original sticks, one per line.
样例输入
9
5 2 1 5 2 1 5 2 1
4
1 2 3 4
0
样例输出
6
5
来源
POJ
上传者
张云聪

解题:搜索+剪枝。。。NYOJ上这题比POJ 1011要难得多啊,先附上POJ 1011 AC代码 ,待会贴NYOJ 293 AC代码
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <vector>
#include <climits>
#include <algorithm>
#include <cmath>
#define LL long long
using namespace std;
int d[],n,ans,sum;
bool vis[];
bool cmp(const int &a,const int &b) {
return b < a;
}
bool dfs(int cur,int m,int len,int p) {
if(m*len == sum-len) return true;
if(cur == len) return dfs(,m+,len,);
for(int i = p; i < n; i++) {
if(!vis[i] && cur+d[i] <= len) {
vis[i] = true;
if(dfs(cur+d[i],m,len,i+)) return true;
vis[i] = false;
if(p == ) return false;
while(i+ < n && d[i+] == d[i]) i++;
}
}
return false;
}
int main() {
while(scanf("%d",&n),n) {
int i;
for(sum = i = ; i < n; i++) {
scanf("%d",d+i);
sum += d[i];
}
ans = sum;
sort(d,d+n,cmp);
memset(vis,false,sizeof(vis));
for(i = n; i > ; i--)
if(sum%i == && dfs(,,sum/i,)) {ans = sum/i;break;}
printf("%d\n",ans);
}
return ;
}

NYOJ 293.。。哈..如果当前棍子已经达到预期的长度,却导致后面的无法拼凑出预期的长度,只能怪当前的棍子,所以立即返回。。。因为当前已经满足这个长度了,继续循环,只会导致与当前长度相等或者变短。。。。return 0说明了当前棍子的组合的不合理性
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <vector>
#include <climits>
#include <algorithm>
#include <cmath>
#define LL long long
using namespace std;
int d[],n,ans,sum;
bool vis[];
bool cmp(const int &a,const int &b) {
return b < a;
}
bool dfs(int cur,int m,int len,int p) {
if(m*len == sum-len) return true;
for(int i = p; i < n; i++) {
if(!vis[i] && cur+d[i] <= len){
vis[i] = true;
if(cur+d[i] == len){
if(dfs(,m+,len,)) return true;
vis[i] = false;
return ;//比POJ上多的优化条件
}else{
if(dfs(cur+d[i],m,len,i+)) return true;
vis[i] = false;
if(cur == ) return false;
while(i+ < n && d[i] == d[i+]) i++;
}
}
}
return false;
}
int main() {
while(scanf("%d",&n),n) {
int i;
memset(d,,sizeof(d));
for(sum = i = ; i < n; i++) {
scanf("%d",d+i);
sum += d[i];
}
ans = sum;
sort(d,d+n,cmp);
memset(vis,false,sizeof(vis));
for(i = n; i > ; i--){
if(sum%i == && dfs(,,sum/i,)) {ans = sum/i;break;}
}
printf("%d\n",ans);
}
return ;
}

NYOJ 293 Sticks的更多相关文章

  1. NYOJ 1007

    在博客NYOJ 998 中已经写过计算欧拉函数的三种方法,这里不再赘述. 本题也是对欧拉函数的应用的考查,不过考查了另外一个数论基本定理:如何用欧拉函数求小于n且与n互质所有的正整数的和. 记eule ...

  2. NYOJ 998

    这道题是欧拉函数的使用,这里简要介绍下欧拉函数. 欧拉函数定义为:对于正整数n,欧拉函数是指不超过n且与n互质的正整数的个数. 欧拉函数的性质:1.设n = p1a1p2a2p3a3p4a4...pk ...

  3. HDOJ 1051. Wooden Sticks 贪心 结构体排序

    Wooden Sticks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) To ...

  4. POJ 2653 Pick-up sticks (线段相交)

    题意:给你n条线段依次放到二维平面上,问最后有哪些没与前面的线段相交,即它是顶上的线段 题解:数据弱,正向纯模拟可过 但是有一个陷阱:如果我们从后面向前枚举,找与前面哪些相交,再删除前面那些相交的线段 ...

  5. NYOJ 333

    http://www.cppblog.com/RyanWang/archive/2009/07/19/90512.aspx?opt=admin 欧拉函数 E(x)表示比x小的且与x互质的正整数的个数. ...

  6. hduoj 1455 && uva 243 E - Sticks

    http://acm.hdu.edu.cn/showproblem.php?pid=1455 http://uva.onlinejudge.org/index.php?option=com_onlin ...

  7. NYOJ 99单词拼接(有向图的欧拉(回)路)

    /* NYOJ 99单词拼接: 思路:欧拉回路或者欧拉路的搜索! 注意:是有向图的!不要当成无向图,否则在在搜索之前的判断中因为判断有无导致不必要的搜索,以致TLE! 有向图的欧拉路:abs(In[i ...

  8. POJ 2653 Pick-up sticks【线段相交】

    题意:n根木棍随意摆放在一个平面上,问放在最上面的木棍是哪些. 思路:线段相交,因为题目说最多有1000根在最上面.所以从后往前处理,直到木棍没了或者最上面的木棍的总数大于1000. #include ...

  9. POJ1065Wooden Sticks[DP LIS]

    Wooden Sticks Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 21902   Accepted: 9353 De ...

随机推荐

  1. xml和TreeView

    1.TreeView代码代码: private void Form1_Load(object sender, EventArgs e) {<br> //代码为TreeView添加子项 tr ...

  2. java代码(ascii与字母互转)

    package test; /** * Java中将一个字符与对应Ascii码互转 * 1 byte = 8bit 可以表示 0-127 */ public class GetCharAscii { ...

  3. C语言中的二级指针(双指针)

    原创作品,转载请标明出处http://blog.csdn.net/yming0221/article/details/7220688 C语言更多查看 C语言使用注意事项(一) C语言使用注意事项(二) ...

  4. 关于在filter中获取WebApplicationContext的实践

    网上很多说法,诸如: <param-name>contextConfigLocation</param-name> <param-value> classpath: ...

  5. LinuxShell(脚本如何编译问题)

    想学shell的同学请记住: 如果你写好脚本后不给脚本执行权限那也是不行的: 添加执行权限: chmod +x 脚本名.sh 在Linux shell中有一个脚本编译命令: bash -v 脚本名.s ...

  6. MVC视图特性

    在主界面的视图中可以使用viewdata,引用主界面的分布视图界面也可以调用主界面的分部视图,但是分部视图不可以定义viewdata并使用 例子如下: // // GET: /Home/ public ...

  7. sourcegrid统计报表画法以及EXCEL导出内容代码完全版

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  8. mybatis 原理研究

    1. mybatis 是使用JDBC来实现的, 所以需要我们首先了解JDBC 的查询 ①加载JDBC驱动 ②建立并获取数据库连接 ③设置sql语句的传递参数 ④执行sql语句并获得结果 ⑤对结果进行转 ...

  9. Cayley凯莱定理——一一对应

    定理 过$n$个有标志顶点的树的数目等于$n^{n-2}$. 此定理说明用$n-1$条边将$n$个已知的顶点连接起来的连通图的个数是$n^{n-1}$.也可以这样理解,将n个城市连接起来的树状网络有$ ...

  10. Hermite 矩阵的特征值不等式

    将要学习 关于 Hermite 矩阵的特征值不等式. Weyl 定理 以及推论.   Weyl 定理 Hermann Weyl 的如下定理是大量不等式的基础,这些不等式要么涉及两个 Hermite 矩 ...