Blue Bridge Cup--algorithm to train the troubles of the lake without name

Source: Internet
Author: User

1, my own submission to the practice system code ideas:

(1) If M < n,0 type scheme;

(2) The M-Shoe-man is written in M 1, then, after each 1, is the number of people who borrow shoes;

(3) Then, in the beginning, can borrow the shoes array a[] = {1, 2, 3, 4, ..., m};

(4) for the position after the first 1, the number of shoes that can be allowed to borrow is: 0~a[i];

(5) When I borrow the J pair of shoes after the position after 1, starting from I, all the way to M (note that the subscript in the dictation is starting from 1), all elements in a array must be reduced by J;

(6) Next, recursion: Go to the first i + a location to borrow shoes, but also need to borrow shoes to reduce the number of J;

(7) If the current number of people who need to borrow shoes is 0, return 1, if the current position in the back of the first 1, return 1 (because at this time must be m>=n, to this position, the shoe must be enough to borrow);

(8) memory. This, I did not do at first. Ran a bit the largest example, atthe time, his computer ran a few minutes have no results, hand over can also, can only show that the data is too water.

The idea of memory is that the person who also shoes, m, the position of the current borrow shoes (in which 1 behind), how many people need to borrow shoes, these three can only determine a state, search memory.

2, the code is as follows:

/** not "memory" can be past, can only be explained that the data is too water. */#include<bits/stdc++.h>using namespacestd;intm, N;intdp[ -][ -][ -];intSolveintIDX, vector<int> Vec,intLeft ) {    if(idx = = Vec.size ()-1)return 1; if(Left <=0)return 1; if(Dp[m][idx][left]! =-1)returnDp[m][idx][left]; intAns =0;  for(intj =0; J <= Vec[idx]; ++j) {if(Left >=j) {Vector<int>Nvec (VEC);  for(intk = idx, Sz = Nvec.size (); k < sz;++k) {Nvec[k]-=J; } ans+ = Solve (idx +1, Nvec, left-j); }} Dp[m][idx][left]=ans; returnans;}intMain () { while(~SCANF ("%d%d", &m, &N)) {memset (DP,-1,sizeof(DP)); if(M <N) {printf ("0\n"); Continue; } Vector<int>VEC;  for(inti =1; I <= m; ++i) vec.push_back (i); printf ("%d\n", Solve (0, VEC, N)); }    return 0;}

3, after the Internet to see other people's solution, recursive equation is very simple. It is:

(1) if M < N, returns 0, indicates that this state does not have an effective scheme;

(2) If n = 0, returns 1; It is an effective scheme for everyone to borrow their shoes.

(3) Otherwise, ........ ....... I don't understand, but I want to add ........ .....

4, the code is all over the Internet.

Blue Bridge Cup--algorithm to train the troubles of the lake without name

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.