UVa 165:stamps, continuous postage issues

Source: Internet
Author: User
Tags printf range

Topic Link:

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=108

Type: Backtracking

Original title:

The government of Nova Mareterrania requires that various legal documents have stamps and attached to them Ent can derive revenue from them. In terms of recent legislation, each class ' document is ' limited in ' number of stamps to it. The Government wishes to know I many different stamps, and of what values, they need to print to allow the widest choice of values to is made up under these conditions. Stamps are always valued in units of $.

This has been analysed by government mathematicians who have derived a formula for n (h,k), where H is the number of stamps This may being attached to a document, K is the number of denominations of stamps available, and N are the largest attainable value in a continuous sequence starting from $. For instance, if h=3, k=2 and the denominations are and $, we can make all of the values from $ to $ and $). However with the same values of H and K, but using and $ stamps we can make all the values from $ 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 is published in one of T He government reports but No-one can remember which one, and of the three researchers who started to search for the Formul A, two died of boredom and the third took a job as a lighthouse keeper the IT because more provided social.

The task has now been passed in to. You are doubt the existence of a formula in the "I" to write A, "for given values of H and K , would determine an optimum set of stamps and the value of N (h,k).

Input

Input would consist of several lines, each containing a value for H and K. The file is terminated by two zeroes (0 0). For technical reasons the sum of H and K are limited to 9. (The President lost his little finger in a shooting accident and cannot count past 9).

Output

Output would consist of a line for each value of H and K consisting of the the K stamp values in ascending order right justifie D in fields 3 characters wide, followed by a spaces and an arrow (->) and the value of N (h,k) right justified in a field 3 characters wide.

Sample input

3 2
0 0

Sample output

  1  3->  7

The main effect of the topic:

A country issues k stamps of different denominations, and stipulates that only h stamps can be affixed to each envelope. Formula N (h,k) means to select H stamps from a postage stamp of k value, which can be composed of a continuous 1,2,3,......n, and n is the sum of the maximum par value that can be reached. For example, when h=3,k=2, assuming that the two values are 1, 4, then they can compose a continuous 1 ... 6, although it can also be composed of 8,9,12, but they are discontinuous.

Analysis and Summary:

The issue of continuous postage, this is a very classic question. Dr. 's "Computer algorithm design and analysis of the third edition" p173 has introduced, but not enough detail, here reproduced a blog on the Dr. said that there is more in-depth detailed analysis:

Continuous postage Problem http://blog.csdn.net/shuangde800/article/details/7755254

Here's a summary of my own words:

First open an array Stampval "0...I" to save the individual face value, and then open a maxval[0...i] to save the current maximum value of all the values can be a continuous value.

Well, we can be sure that stampval[0] must be equal to 1. Because if not 1, a lot of numbers can not be pooled.

Then the corresponding, maxval[0] = 1*h. Number of stamps to be allowed to be affixed

Next is to determine the second, the third ... The face value of the K-stamp, how can this be determined?

For stampval[i+1], its value range is stampval[i]+1 ~maxval[i]+1.

Stampval[i]+1 good understanding, this time to take the face value than the last one, and this time the upper limit of the value of the last can be reached +1 of the maximum consecutive value, because if more than this, then

There will be a fault, that is, the number of the last maximum value of +1 cannot be formed. For example, suppose that you can put 3 stamps, 3 denominations, the front 2 denominations have been identified as 1, 2, the maximum continuous value can be 6, then the 3rd face value range of 3~7. What if it gets bigger than 7? Start to calculate to know, assuming take 8, then face value for 1,2,8, will not be able to combine 7!!

After you know this, you can know the approximate idea of backtracking, but not enough, how to get a given number of par can achieve the maximum consecutive face value?

The most intuitive and easy to think of is the direct recursive backtracking enumeration of all cases, you can know the maximum continuous value.

Here's the recursive function I wrote:

Cur currently uses a few tickets, n denominations, sum par and  
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 is a global array that is initialized to 0 when invoking recursion. Then use it to record the sum of the face values that have occurred.

Finally, you only need to start the enumeration from the subscript 1 of the occur array until it is not the true value of the maximum contiguous value that can be reached.

Because the amount of data in this problem is very small, this does not time out at all, and the speed is good:

165-stamps accepted C + + 0.068

Here's the code in this way:

#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];   
        Calculates the maximum number of consecutive denominations that can be reached for a given denomination and quantity//cur current number of tickets, n current denomination, sum denomination and 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) {max  
            Stampval = 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);  
the int main () {#ifdef local freopen ("Input.txt", "R", stdin);  
        Number of #endif//h, K denominations 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; }

The maximum continuous postage interval for computing x[1:i] is frequently used in this algorithm, and it is likely to time out if the amount of data is large.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.