題目連結:
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=108
類型: 回溯
原題:
The government of Nova Mareterrania requires that various legal documents have stamps attached to them so that the government can derive revenue from them. In terms of recent legislation, each class of document is limited in the number of stamps that may be attached to it. The government wishes to know how many different stamps, and of what values, they need to print to allow the widest choice of values to be made up under these conditions. Stamps are always valued in units of $1.
This has been analysed by government mathematicians who have derived a formula for n(h,k), where h is the number of stamps that may be attached to a document, k is the number of denominations of stamps available, and n is the largest attainable value in a continuous sequence starting from $1. For instance, if h=3, k=2 and the denominations are $1 and $4, we can make all the values from $1 to $6 (as well as $8, $9 and $12). However with the same values of h and k, but using $1 and $3 stamps we can make all the values from $1 to $7 (as well as $9). This is maximal, so n(3,2) = 7.
Unfortunately the formula relating n(h,k) to h, k and the values of the stamps has been lost--it was published in one of the government reports but no-one can remember which one, and of the three researchers who started to search for the formula, two died of boredom and the third took a job as a lighthouse keeper because it provided more social stimulation.
The task has now been passed on to you. You doubt the existence of a formula in the first place so you decide to write a program that, for given values of h and k, will determine an optimum set of stamps and the value of n(h,k).
Input
Input will consist of several lines, each containing a value for h and k. The file will be terminated by two zeroes (0 0). For technical reasons the sum of h and k is limited to 9. (The President lost his little finger in a shooting accident and cannot count past 9).
Output
Output will consist of a line for each value of h and k consisting of the k stamp values in ascending order right justified in fields 3 characters wide, followed by a space and an arrow (->) and the value of n(h,k) right justified in a field 3 characters wide.
Sample input
3 20 0
Sample output
1 3 -> 7
題目大意:
某國家發行k種不同面值的郵票,並且規定每張信封上最多隻能貼h張郵票。 公式n(h,k)表示用從k中面值的郵票中選擇h張郵票,可以組成面額為連續的1,2,3,……n, n是能達到的最大面值之和。例如當h=3,k=2時, 假設兩種面值取值為1,4, 那麼它們能組成連續的1……6, 雖然也可以組成8,9,12,但是它們是不連續的了。
分析與總結:
連續郵資問題,這個算是很經典的一個問題了。王曉東的《電腦演算法設計與分析第三版》 p173有介紹, 但是講的不夠詳細, 這裡轉載了一篇部落格對王曉東講的有更深入詳細分析:
連續郵資問題 http://blog.csdn.net/shuangde800/article/details/7755254
以下是按照我自己的話來總結:
首先開一個數組stampVal【0...i】來儲存各個面值,再開一個maxVal[0...i]來儲存當前所有面值能組成的最大連續面值。
那麼,我們可以確定stampVal[0] 一定是等於1的。因為如果沒有1的話,很多數字都不能湊成。
然後相應的,maxVal[0] = 1*h. h為允許貼郵票的數量
接下去就是確定第二個,第三個......第k個郵票的面值了,這個該怎麼確定呢?
對於stampVal[i+1], 它的取值範圍是stampVal[i]+1 ~maxVal[i]+1.
stampVal[i]+1好理解, 這一次取的面值肯定要比上一次的面值大, 而這次取的面值的上限是上次能達到的最大連續面值+1, 是因為如果比這個更大的話, 那麼
就會出現斷層, 即無法組成上次最大面值+1這個數了。 舉個例子, 假設可以貼3張郵票,有3種面值,前面2種面值已經確定為1,2, 能達到的最大連續面值為6, 那麼接下去第3種面值的取值範圍為3~7。如果取得比7更大的話會怎樣呢? 動手算下就知道了,假設取8的話, 那麼面值為1,2,8,將無法組合出7 !!
知道了這個以後,就可以知道回溯的大概思路了, 但是還不夠, 怎樣取得給定的幾個面值能夠達到的最大連續面值呢?
最直觀容易想到的就是直接遞迴回溯枚舉所有情況, 便可知道最大連續值了。
下面是我寫的遞迴函式:
// cur當前用了幾張票, n面額數, sum面值之和 void dfs(int cur, int n, int sum){ if(cur >= h){ occur[sum] = true; return; } occur[sum] = true; for(int i=0; i<=n; ++i){ dfs(cur+1, n, sum+stampVal[i]); } }
occur是一個全域數組, 調用遞迴時先初始化為0。 然後用它來記錄出現過的面值之和。
最後只需要從occur數組的下標1開始枚舉,直到不是true值時就是能達到的最大連續面值。
由於這道題的資料量很小,所以這樣做完全不會逾時,速度也不錯:
165 - Stamps Accepted C++ 0.068
下面是用這種方法的代碼:
#include<cstring> #include<cstdio> #include<cstdlib> #include<iostream> #define MAXN 200 using namespace std; int h, k; int ans[MAXN], maxStampVal, stampVal[MAXN], maxVal[MAXN]; bool occur[MAXN]; // 計算給定面額和數量能夠達到的最大連續面值 // cur當前用了幾張票, n當前面額數, sum面額之和 void dfs(int cur, int n, int sum){ if(cur >= h){ occur[sum] = true; return; } occur[sum] = true; for(int i=0; i<=n; ++i){ dfs(cur+1, n, sum+stampVal[i]); } } void search(int cur){ if(cur >= k){ if(maxVal[cur-1] > maxStampVal){ maxStampVal = maxVal[cur-1]; memcpy(ans, stampVal, sizeof(stampVal)); } return ; } for(int i=stampVal[cur-1]+1; i<=maxVal[cur-1]+1; ++i){ memset(occur, 0, sizeof(occur)); stampVal[cur] = i; dfs(0, cur, 0); int num=0, j=1; while(occur[j++]) ++num; maxVal[cur] = num; search(cur+1); } } int main(){ #ifdef LOCAL freopen("input.txt", "r", stdin); #endif // h數量, k面額數 while(scanf("%d %d", &h, &k), h+k){ stampVal[0] = 1; maxVal[0] = h; maxStampVal = -2147483645; search(1); for(int i=0; i<k; ++i) printf("%3d", ans[i]); printf(" ->%3d\n", maxStampVal); } return 0; }
計算X[1:i]的最大連續郵資區間在本演算法中被頻繁使用到,如果資料量大的話,很可能就會逾時了。