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

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

Sticks --dfs 剪枝

2019-11-11 07:27:02
字體:
來(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ā)表
主站蜘蛛池模板: 久久精品久久精品国产大片 | 成人 日韩 | 欧美成人激情 | 在线成人亚洲 | 暖暖免费观看高清完整版电影 | 99亚洲精品| 国产成人在线一区二区 | 91午夜免费视频 | 大学生一级毛片在线视频 | 成人视屏在线 | 国产一级毛片网站 | 国产中出视频 | 久久精品视频8 | 天天草天天爱 | 精品国产一区二区久久 | 久久激情免费视频 | 免费观看黄色一级视频 | 国产亚洲精品视频中文字幕 | 国产噜噜噜噜久久久久久久久 | 蜜桃视频观看麻豆 | 欧美亚洲综合在线 | 久久精品视频8 | 国产精品久久久久av | 成年性羞羞视频免费观看 | 国产成年人视频 | 亚洲尻逼视频 | 国产69精品久久久久99尤 | 中文字幕在线观看精品 | 中文字幕亚洲一区二区三区 | 一级大片在线观看 | omofun 动漫在线观看 | 一区在线看| 国产精品一区二av18款 | 青青草最新网址 | 日韩av电影免费看 | 在线观看中文字幕av | 亚洲一级片在线观看 | 午夜精品视频免费观看 | av视在线 | 久久久精品网 | 美女视频在线观看黄 |