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 ...
随机推荐
- .Net应用导入、导出Excel文件
本次阐述的导入和导出都围绕此Demo进行
- LN : Eden Polymorphic And OOP Design Pattern Abstract Factory
Appreciation to our TA, +7, who designed this task. Client.cpp #include <iostream> #include &l ...
- Vue.js - day6
注意: 有时候使用npm i node-sass -D装不上,这时候,就必须使用 cnpm i node-sass -D 在普通页面中使用render函数渲染组件 在webpack中配置.vue组件页 ...
- Wrapper class package.jaxws.methodName is not found. Have you run APT to generate them?解决方案
使用JAX-WS 2.X基于Web容器发布WebService报错,错误信息类似于: Wrapper class package.jaxws.methodName is not found. Have ...
- Android LitePal介绍与使用说明
LitePal for Android LitePal是一个Android开源库,它使开发者使用SQLite数据库变得非常容易. 你可以不用写一句SQL语句就可以完成大部分数据库操作,包括创建表,更新 ...
- POJ 1998 Cube Stacking
Description Farmer John and Betsy are playing a game with N (1 <= N <= 30,000)identical cubes ...
- 原创 :xftp SFTP子系统申请已拒绝 请确保SSH链接的SFTP子系统设置有效
在出现这个错误时候 如果你的远程连接没有问题 那么就执行下面的命令 service sshd restart 搞定!
- vue2.0动画
相对于vue1.0来说,vue2.0的动画变化还是挺大的, 在1.0中,直接在元素中加 transition ,后面跟上名字. 而在vue2.0中,需要把设置动画的元素.路由放在<transit ...
- 1653: Champion of the Swordsmanship
1653: Champion of the Swordsmanship Time Limit: 1 Sec Memory Limit: 64 MBSubmit: 11 Solved: 8[Subm ...
- JS实现跑马灯效果(向左,向上)
<html> <head> <title>JS实现跑马灯效果</title> <style> * { font-size:12px; fon ...