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. .Net应用导入、导出Excel文件

    本次阐述的导入和导出都围绕此Demo进行

  2. 让linux下的eclipse支持GBK编码

    原文链接:http://leaze.blog.51cto.com/83088/195584 今天,把windows下的工程导入到了Linux下eclipse中,由于以前的工程代码,都是GBK编码的,而 ...

  3. c语言中的赋值

    int s,i,len,err=0 这个是只给err赋了值还是这四个都有?

  4. iOS Category实现原理 (补充)

    iOS Category实现原理 (补充) load 和 initialize load load方法会在程序启动就会调用,当装载类信息的时候就会调用. 调用顺序看一下源代码.在 objc-loadm ...

  5. HTML5文档结构 摘要

    <!DOCType html>--声明文档结构类型 <html lang=zh-cn> <head> <meta charset=utf-8> < ...

  6. vba控制图表,excel图表,一键完成

    来源http://club.excelhome.net/thread-1417686-1-1.html 官方教程链接 https://docs.microsoft.com/zh-cn/office/v ...

  7. (WWWWWWWWWW)codevs 3305 水果姐逛水果街Ⅱ

    写这么长了不A有点舍不得.. 想A又调不出来.. 于是乎就存一下.. 屠龙宝刀点击就送 #include <cstdio> #include <vector> #define ...

  8. 使用Google Colab训练神经网络(二)

    Colaboratory 是一个 Google 研究项目,旨在帮助传播机器学习培训和研究成果.它是一个 Jupyter 笔记本环境,不需要进行任何设置就可以使用,并且完全在云端运行.Colaborat ...

  9. 分享一个Delphi跨平台Http库的封装,一个Delphi跨平台TCP库的封装

    { 单元名:跨平台的TCP客户端库封装 作者:5bug 网站:http://www.5bug.wang } unit uCPTcpClient; interface uses System.Class ...

  10. shell脚本,编程题练习。

    题目是:将 文件file为 b+b+b+b+b+b+b+b 变为 b+b=b+b=b+b=b+b 解答方法如下: