D - Sticks(3.4.2)
来源:互联网 发布:淄博seo外包公司 编辑:程序博客网 时间:2024/05/20 06:31
Description
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.
Input
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.
Output
The output should contains the smallest possible length of original sticks, one per line.
Sample Input
95 2 1 5 2 1 5 2 141 2 3 40
Sample Output
65
无语这道题愣是没提交对,原先自己写了个,结果WA,后来在网上找到一篇博客关于这道题的解法,无奈对照他的代码改了N遍几乎一模一样提交仍然是TLE。最后只好把他的代码交上AC了。是在不知道原因。
下面是我的代码:
#include <iostream>#include <algorithm>#include <cstring>using namespace std;int n, p[65];bool pj[65];bool cmp(int a,int b){ return a>b;}int solve( int per, int remain, int m ){ if( remain == 0 && m == 0) return per; else if( remain == 0 ) remain = per; for(int i = 0; i < n; i++ ) { if( pj[i] == true ) continue; if( p[i] <= remain ) { pj[i] = true; remain -= p[i]; m--; if( solve( per, remain, m ) != 0 ) return per; else pj[i] = false; if( remain == per || remain == p[i] ) return 0; if( p[i] == p[i+1] ) i++ ; } } return 0;}int main(){ while(cin>>n&&n) { int sum=0; int len, length = 0; for( int i = 0; i < n; i++ ) { cin >> p[i]; sum += p[i]; } sort( p, p + n , cmp ); memset( pj, 0, sizeof(pj) ); for( len = p[0]; len <= sum; len++ ) { if( sum % len == 0) { length = solve( len, 0, n ); if(length) break; } } cout<<length<<endl; } return 0;}
下面是网上AC的代码:
#include<iostream>#include<string.h>#include<algorithm>using namespace std;int n,num[69];int pd[69];int cmp(int a,int b){return a>b;}int dfs(int len,int s,int nu){int i; if(s==0&&nu==0)//如果剩下的长度和剩下的木棒数为0,则成功return len; if(s==0)//如果剩下的长度为0,则重新赋值s=len; for(i=0;i<n;i++) { if(pd[i]==1)continue; if(s>=num[i]) { pd[i]=1; if(dfs(len,s-num[i],nu-1))return len;pd[i]=0;//如果不成功,则当前棒不使用 if(num[i]==s||s==len)break; while(num[i]==num[i+1])i++; } }return 0;}int main(){ while(cin>>n&&n) {int i;int sum; sum=0;int len,k;for(i=0;i<n;i++){cin>>num[i];sum=sum+num[i];}sort(num,num+n,cmp);for(len=num[0];len<=sum;len++){memset(pd,0,sizeof(pd));if(sum%len==0){k=dfs(len,0,n);if(k)break;}}cout<<k<<endl; } return 0;}/*95 2 1 5 2 1 5 2 141 2 3 40*/
参考博客地址:http://blog.csdn.net/sunshumin/article/details/37913703
0 0
- D - Sticks(3.4.2)
- 《数据结构编程实验》 3.4.2Sticks
- Pick The Sticks【背包 CCPC D题】
- 南阳ccpc D题 Pick The Sticks
- Sticks
- sticks
- sticks
- sticks
- Sticks
- sticks
- Sticks
- Sticks
- Sticks
- Sticks
- Sticks
- Sticks
- Sticks
- Sticks
- 从源码中构建XWiki
- 编辑文章 - 博客频道 - CSDN.NET
- Openstack前传五(虚拟化技术原理)
- live555 iPhone静态库编译
- gnuplot 绘制时间序列图
- D - Sticks(3.4.2)
- 好的产品经理是怎样炼成的
- jstl总结
- Tomcat version 6.0 only supports J2EE 1.2, 1.3, 1.4, and Java EE 5 Web modules
- linux文档打不开 bad interpreter: No such file or directory
- 关于Run Loop
- cocos2dx学习笔记(简单的滚动数字表盘)
- hdu2199
- 因子和阶乘