国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

Sticks --dfs 剪枝

2019-11-11 06:48:38
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

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

9 5 2 1 5 2 1 5 2 1 4 1 2 3 4 0

Sample Output

6 5

解題報(bào)告

還是太弱了,參考了別人的代碼。 這個(gè)題體現(xiàn)了剪枝的重要性。剪一刀(加一個(gè)限定條件)計(jì)算過(guò)程可以減少若干倍。

#include<stdio.h>#include<string.h>#include<algorithm>#define MAX_N 70using namespace std;int a[MAX_N];bool vis[MAX_N];int goal,sum,N;bool dfs(int now,int head,int cnt){ if(goal*cnt==sum) return true; for(int i=head;i<N;i++){ if(vis[i]||(i&&!vis[i-1]&&a[i]==a[i-1])) continue; if(now+a[i]==goal){ vis[i]=true; if(dfs(0,0,cnt+1)) return true; return vis[i]=false; } if(now+a[i]<goal){ vis[i]=true; if(dfs(now+a[i],i+1,cnt)) return true; vis[i]=false; if(!now) return false; } } return false;}bool cmp(int x,int y){return x>y;}int main(){ while(scanf("%d",&N)&&N!=0){ sum=0;int M=-1; for(int i=0;i<N;i++){ scanf("%d",&a[i]); M=max(a[i],M); sum+=a[i]; } sort(a,a+N,cmp); memset(vis,0,sizeof(vis)); for(goal=M;goal<=sum;goal++) if(sum%goal==0&&dfs(0,0,0)) break; printf("%d/n",goal); } return 0;}
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 菏泽市| 和龙市| 保靖县| 东辽县| 台北县| 沈丘县| 准格尔旗| 江都市| 古浪县| 洪江市| 南川市| 平武县| 綦江县| 陆河县| 耒阳市| 夏津县| 陕西省| 普格县| 舞钢市| 横山县| 嵩明县| 宣恩县| 开原市| 大埔县| 西乡县| 英超| 虹口区| 绿春县| 安国市| 萝北县| 公安县| 毕节市| 大邑县| 邻水| 惠水县| 峨山| 宣威市| 吕梁市| 焦作市| 澄迈县| 侯马市|