日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

HDU Problem - 1455 Sticks

發布時間:2024/4/18 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 HDU Problem - 1455 Sticks 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題目鏈接

Problem 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 file 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

AC

  • 剪枝的運用
  • 首先從最長的木棒枚舉,滿足木棒總和是枚舉木棒的倍數
  • 然后dfs看能否把所有木棒用完,如果用完就找到了一個最小的合適值
  • 開始剪枝
  • 如果進行一系列的搜索之后,發現當前這個木棒未完成匹配,而且當前所要匹配的是整個目標,這就不用接著往下dfs
    舉個例子:木棒分別是 6 5 5 3 2 2, 你要找6, 第一個可以,接著匹配下一個6,找到5時發現沒有匹配的這時候直接退出就行,說明6不能構成完整的
#include <bits/stdc++.h> using namespace std;int a[65]; bool vis[65]; bool cmp(int a, int b) {return a > b; } bool dfs(int n, int tar, int temp, int len, int now) {if (len == n) return true;if (temp == 0) temp = tar, now = 0;for (int i = now; i < n; ++i) {if (vis[i]) continue;if (a[i] <= temp) {vis[i] = true;if (dfs(n, tar, temp - a[i], len + 1, i + 1))return true;else{vis[i] = false;if (temp == tar)return false;}}} return false; } int main() {int n;while(scanf("%d", &n), n) {int sum = 0;for (int i = 0; i < n; ++i) {scanf("%d", &a[i]);sum += a[i];}sort(a, a + n, cmp);int tar = a[0];while (1) {memset(vis, false, sizeof(vis));while (sum % tar) tar++;if(dfs(n, tar, tar, 0, 0)) break;tar++;}printf("%d\n", tar);}return 0; }

總結

以上是生活随笔為你收集整理的HDU Problem - 1455 Sticks的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。