首页 > 代码库 > NYOJ 293 Sticks
NYOJ 293 Sticks
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.
- 样例输入
95 2 1 5 2 1 5 2 141 2 3 40
- 样例输出
65
- 来源
- POJ
- 上传者
- 张云聪
解题:搜索+剪枝。。。NYOJ上这题比POJ 1011要难得多啊,先附上POJ 1011 AC代码 ,待会贴NYOJ 293 AC代码View Code1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5 #include <vector> 6 #include <climits> 7 #include <algorithm> 8 #include <cmath> 9 #define LL long long10 using namespace std;11 int d[70],n,ans,sum;12 bool vis[70];13 bool cmp(const int &a,const int &b) {14 return b < a;15 }16 bool dfs(int cur,int m,int len,int p) {17 if(m*len == sum-len) return true;18 if(cur == len) return dfs(0,m+1,len,0);19 for(int i = p; i < n; i++) {20 if(!vis[i] && cur+d[i] <= len) {21 vis[i] = true;22 if(dfs(cur+d[i],m,len,i+1)) return true;23 vis[i] = false;24 if(p == 0) return false;25 while(i+1 < n && d[i+1] == d[i]) i++;26 }27 }28 return false;29 }30 int main() {31 while(scanf("%d",&n),n) {32 int i;33 for(sum = i = 0; i < n; i++) {34 scanf("%d",d+i);35 sum += d[i];36 }37 ans = sum;38 sort(d,d+n,cmp);39 memset(vis,false,sizeof(vis));40 for(i = n; i > 1; i--)41 if(sum%i == 0 && dfs(0,0,sum/i,0)) {ans = sum/i;break;}42 printf("%d\n",ans);43 }44 return 0;45 }
NYOJ 293.。。哈..如果当前棍子已经达到预期的长度,却导致后面的无法拼凑出预期的长度,只能怪当前的棍子,所以立即返回。。。因为当前已经满足这个长度了,继续循环,只会导致与当前长度相等或者变短。。。。return 0说明了当前棍子的组合的不合理性View Code1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5 #include <vector> 6 #include <climits> 7 #include <algorithm> 8 #include <cmath> 9 #define LL long long10 using namespace std;11 int d[70],n,ans,sum;12 bool vis[70];13 bool cmp(const int &a,const int &b) {14 return b < a;15 }16 bool dfs(int cur,int m,int len,int p) {17 if(m*len == sum-len) return true;18 for(int i = p; i < n; i++) {19 if(!vis[i] && cur+d[i] <= len){20 vis[i] = true;21 if(cur+d[i] == len){22 if(dfs(0,m+1,len,0)) return true; 23 vis[i] = false;24 return 0;//比POJ上多的优化条件25 }else{26 if(dfs(cur+d[i],m,len,i+1)) return true;27 vis[i] = false;28 if(cur == 0) return false;29 while(i+1 < n && d[i] == d[i+1]) i++;30 }31 }32 }33 return false;34 }35 int main() {36 while(scanf("%d",&n),n) {37 int i;38 memset(d,0,sizeof(d));39 for(sum = i = 0; i < n; i++) {40 scanf("%d",d+i);41 sum += d[i];42 }43 ans = sum;44 sort(d,d+n,cmp);45 memset(vis,false,sizeof(vis));46 for(i = n; i > 1; i--){47 if(sum%i == 0 && dfs(0,0,sum/i,0)) {ans = sum/i;break;}48 }49 printf("%d\n",ans);50 }51 return 0;52 }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。