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 ...
随机推荐
- 搭建高可用mongodb集群—— 副本集
转自:http://www.lanceyan.com/tech/mongodb/mongodb_repset1.html 在上一篇文章<搭建高可用MongoDB集群(一)——配置MongoDB& ...
- Webservice相关的知识
一.利用jdk web服务api实现,这里使用基于 SOAP message 的 Web 服务 1.首先建立一个Web services EndPoint: package Hello; import ...
- leetcode395 Longest Substring with At Least K Repeating Characters
思路: 尺取法. 循环i:1~26,分别计算恰好包含i种字母并且每种字母出现的次数大于等于k个的最长子串长度. 没法直接使用尺取法,因为不满足区间单调性,但是使用如上的方法却是可以的,因为子串中包含的 ...
- 【数据库-Azure SQL Database】如何创建事务复制将本地数据同步到 SQL Azure
Azure SQL DB 可以被配置成为 SQL Server 事务复制的一个订阅者( subscriber ). 主要应用场景有两种: 将您的数据迁移到 Azure SQL DB, 并且没有宕机时间 ...
- sql service 查询分析数据库
--学会通配符 https://blog.csdn.net/blackfwhite/article/details/80382849 --学会变量中的变量 https://www.cnblogs.co ...
- vijos 1448 校门外的树 (不是05年普及组那题)
描述 校门外有很多树,有苹果树,香蕉树,有会扔石头的,有可以吃掉补充体力的……如今学校决定在某个时刻在某一段种上一种树,保证任一时刻不会出现两段相同种类的树,现有两个操作:K=1,K=1,读入l.r表 ...
- leetcode_1053. Previous Permutation With One Swap
1053. Previous Permutation With One Swap https://leetcode.com/problems/previous-permutation-with-one ...
- iOS,APP退到后台,获取推送成功的内容并且语音播报内容。
老铁,我今天忙了一下午就为解决这个问题,网上有一些方法,说了一堆关于这个挂到后台收到推送并且获得推送内容的问题,有很多人都说APP挂到后台一会就被杀死.但实际上可以有办法解决的. WechatIMG3 ...
- 1653: Champion of the Swordsmanship
1653: Champion of the Swordsmanship Time Limit: 1 Sec Memory Limit: 64 MBSubmit: 11 Solved: 8[Subm ...
- 第1节 flume:15、flume案例二,通过自定义拦截器实现数据的脱敏
1.7.flume案例二 案例需求: 在数据采集之后,通过flume的拦截器,实现不需要的数据过滤掉,并将指定的第一个字段进行加密,加密之后再往hdfs上面保存 原始数据与处理之后的数据对比 图一 ...