麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 學院 > 開發設計 > 正文

Sticks --dfs 剪枝

2019-11-14 09:19:26
字體:
來源:轉載
供稿:網友

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

解題報告

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

#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;}
上一篇:Base64

下一篇:SFML game move

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 一日本道久久久精品国产 | 美女91视频 | 午夜视频导航 | 一级黄色性感片 | 久久中文免费 | 亚洲男人一区 | omofun 动漫在线观看 | 北京一级毛片 | 成人免费看片a | aa级黄色片 | 久久男| 亚洲精品永久视频 | 亚洲欧美日韩一区二区三区在线观看 | 日本黄色大片免费 | 欧美精品一区二区久久久 | 性欧美极品xxxx欧美一区二区 | 成人做爽爽爽爽免费国产软件 | 草草视频在线播放 | 免费视频a| 久久久久久片 | 日韩黄在线观看 | 欧美一级黄色片免费观看 | 一级爱片| 欧美日韩亚洲国产精品 | av中文字幕免费在线观看 | 色屁屁xxxxⅹ免费视频 | 成人午夜在线免费观看 | 欧美乱淫 | 99爱福利视频在线观看 | xxxxxx打针视频vk | 久久99亚洲精品 | 天天色图片 | 看片一区 | 日韩精品久久久久久久电影99爱 | a黄网站| 午夜精品久久久久久毛片 | 激情小说激情图片激情电影 | 午夜国产在线 | 国产精品视频2021 | 国产免费高清 | xxxxhd73国产 |