NYOJ 293 Sticks
Sticks
- 描述
- 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的更多相关文章
- NYOJ 1007
在博客NYOJ 998 中已经写过计算欧拉函数的三种方法,这里不再赘述. 本题也是对欧拉函数的应用的考查,不过考查了另外一个数论基本定理:如何用欧拉函数求小于n且与n互质所有的正整数的和. 记eule ...
- NYOJ 998
这道题是欧拉函数的使用,这里简要介绍下欧拉函数. 欧拉函数定义为:对于正整数n,欧拉函数是指不超过n且与n互质的正整数的个数. 欧拉函数的性质:1.设n = p1a1p2a2p3a3p4a4...pk ...
- HDOJ 1051. Wooden Sticks 贪心 结构体排序
Wooden Sticks Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...
- POJ 2653 Pick-up sticks (线段相交)
题意:给你n条线段依次放到二维平面上,问最后有哪些没与前面的线段相交,即它是顶上的线段 题解:数据弱,正向纯模拟可过 但是有一个陷阱:如果我们从后面向前枚举,找与前面哪些相交,再删除前面那些相交的线段 ...
- NYOJ 333
http://www.cppblog.com/RyanWang/archive/2009/07/19/90512.aspx?opt=admin 欧拉函数 E(x)表示比x小的且与x互质的正整数的个数. ...
- hduoj 1455 && uva 243 E - Sticks
http://acm.hdu.edu.cn/showproblem.php?pid=1455 http://uva.onlinejudge.org/index.php?option=com_onlin ...
- NYOJ 99单词拼接(有向图的欧拉(回)路)
/* NYOJ 99单词拼接: 思路:欧拉回路或者欧拉路的搜索! 注意:是有向图的!不要当成无向图,否则在在搜索之前的判断中因为判断有无导致不必要的搜索,以致TLE! 有向图的欧拉路:abs(In[i ...
- POJ 2653 Pick-up sticks【线段相交】
题意:n根木棍随意摆放在一个平面上,问放在最上面的木棍是哪些. 思路:线段相交,因为题目说最多有1000根在最上面.所以从后往前处理,直到木棍没了或者最上面的木棍的总数大于1000. #include ...
- POJ1065Wooden Sticks[DP LIS]
Wooden Sticks Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 21902 Accepted: 9353 De ...
随机推荐
- css3のborder-radius
css3のborder-radius 今天主要练习了一下border-radius这个属性,这个是最常用的属性,所以先从它开始学习和总结吧. 我觉得需要注意以下几点: 1.书写规范: -webkit- ...
- js原生子级元素阻止父级元素冒泡事件
<html> <head> <style type="text/css"> #hide{ width:75%;height:80px;backg ...
- IE兼容rgba()透明度
一般浏览器的背景透明度可以直接设置 background:rgba(0,0,0,.5); -webkit-background:rgba(0,0,0,.5); -o-background:rgba(0 ...
- Azure 项目构建 – 构建稳定的直播和点播教学系统
本课程主要介绍了如何在 Azure 平台上快速构建和部署基于 Azure 虚拟机的点播和直播教学系统, 实践讲解如何使用 Azure 门户创建虚拟机,配置视频服务,连接 CDN 加速 等. 具体包括项 ...
- SQLServer外键查询删除信息
SELECT FK.NAME,FK.OBJECT_ID,OBJECT_NAME(FK.PARENT_OBJECT_ID) AS REFERENCETABLENAMEFROM SYS.FOREIGN_K ...
- Exoplanet: The hunt is on
原文 How many planets are out there? Today scientists believe that planets could outnumber the stars.F ...
- bunzip2命令
bunzip2——解压缩.bz2格式文件 命令所在路径:/usr/bin/bunzip2 示例1: # bunzip2 yum.log.bz2 解压当前目录下的yum.log.bz2为yum.log, ...
- 1898 ERROR nova.compute.manager
2018-06-10 21:03:54.045 1898 ERROR nova.compute.manager [instance: 15a6c26f-b8af-4a3e-a3df-8552c16e0 ...
- RMQ求区间最大最小值
#include<iostream> #include<cmath> #include<cstdio> #define N 50005 using namespac ...
- 使用prelu
一个使用方式:http://blog.csdn.net/xg123321123/article/details/52610919 还有一种是像relu那样写,我就是采用的这种方式,直接把名字从relu ...