2016-level algorithm third time on machine-g.winter is coming

Source: Internet
Author: User

904 Winter is coming idea

Problem. First simplify the problem, \ (n\) a 0 and \ (m\) 1 rows into a column, the continuous 0 can not exceed x, continuous 1 can not exceed Y, to arrange the number of methods.

It's obvious to think that this is dynamic planning. The quickest way to think of it is \ (dp[i][j][x][y]\) indicates that I have a North border soldier J Savage in the arrangement, and at the end there are x consecutive North soldiers or y consecutive Savage Soldier scheme number. This method is obviously correct, but the light \ (dp[200][200][10][10]\) array is already very close to the memory limit of the subject, guaranteeing the MLE. The state transfer method is a large analog, four-layer for loop, each time an additional person is placed at the end to discuss various situations. Specific code visible MLE reference code, better understanding.

But this method is already close to the answer, just a little bit of optimization. Can be found that the third and four dimensions of a lot of free space is wasted, we do not need to use two dimensions to record a few 0 or 1, you can turn the four-dimensional into a sign, 0 for the north, 1 for the Savage Army, and the third dimension records the last number of consecutive people, so that the space becomes the original 1/ 6, as a simple optimization, the idea has not changed. Specifically visible optimization code, thanks here for Tonny.

The subject can continue to optimize, in another way, dp[i][j][k]: I have a North border soldier J Savage to participate in the arrangement, the third dimension k is the sign (0 for the north, 1 for Savage Army) the number of permutations. The state transition equation becomes: \ (Dp[i][j][0]=∑ (dp[i-k][j][1])%mod\) , where \ (K∈[1,min (i,x)]\) . Similarly, \ (Dp[i][j][1]=∑ (dp[i][j-k][0])%mod\) , where \ (K∈[1,min (j,y)]\) . Believe you will soon be able to understand, here with \ (dp[i-k][j][1]\) to represent the last K 0, equivalent to the same time the three or four-dimensional merger, ingenious extremely. The best reference code can be seen in detail.

Analysis

When the subject does not have the card time, the card is the memory. The goal is to have the idea of optimizing when solving a problem (the actual goal is to turn it into a problem from a medium problem).

DP can only be sensed, inexpressible. When you do DP problem must clear the idea, is generally first regardless of space, after all, to space for time, most of the questions are the first card time card space.

Take the case as an example to briefly explain the DP, will not speak again. Remember that the DP has two elements: optimal substructure and sub-problem overlap, see the introduction of the algorithm 225 pages. In this problem, the optimal substructure is obviously prepared, and the least number of permutations is composed of the least number of permutations with a shorter one. DP Multilayer loops are also regular, because the overlap of sub-problems, you have to work out the problem before you can calculate deeper. Here I and J from the small to the Earth, to ensure that the addition is already calculated, will not have problems, if this problem plus a dp[i+1][j][1], it is obviously wrong, because this has not been calculated. The state transition equation is sometimes subtle and requires some mathematical reasoning.

Best Reference Code
////Created by Alvinzh on 2017/10/24.//Copyright (c) alvinzh. All rights reserved.//#include <cstdio>#include <cstring>#include <iostream>#define MOD 1000007using namespaceStdintN, m, x, y;intdp[205][205][2];intMain () { while(~SCANF ("%d %d %d %d", &n, &m, &x, &y)) {memset (DP,0,sizeof(DP)); for(inti =0; I <= x; ++i) dp[i][0][0] =1; for(inti =0; I <= y; ++i) dp[0][i][1] =1; for(inti =1; I <= N; ++i) { for(intj =1; J <= M; ++J) { for(intK =1; K <= min (i,x); ++K) dp[i][j][0] = (dp[i][j][0] + dp[i-k][j][1])% MOD; for(intK =1; K <= min (j,y); ++K) dp[i][j][1] = (dp[i][j][1] + dp[i][j-k][0])% MOD; }} printf ("%d\n", (dp[n][m][0] + dp[n][m][1])% MOD); }}/ * Merge the 34th dimension as long as we ensure that the last successive segment does not exceed X or Y when the state shifts, and that the third dimension is used to record the last successive 0 or 1. * Dp[i][j][k]: Already have I a North Border soldier J Savage participates in the arrangement, K for the sign bit (0 represents the North border Army, 1 represents Savage Army) the arrangement method number.  */
Optimization Reference Code
/*Author: Meng Yao (12593)Result:ac submission_id:403884Created At:sun Nov 23:05:10 gmt+0800 (CST)problem:904 time:73 memory:11232*/#include <cstdio>#include <cstring>Longf[205][205][ the][2];LongM =1000007;LongN,m,x,y;intMain () { while(~SCANF ("%ld%ld%ld%ld", &n,&m,&x,&y)) {memset (F,0,sizeof(f)); f[0][0][0][0]=1; for(LongI=0; i<=n; i++) { for(Longj=0; j<=m; J + +) {if(i) { for(Longk=1; k<=x; k++) f[i][j][k][0]= (f[i][j][k][0]+f[i-1][j][k-1][0])%M; for(Longk=0; k<=y; k++) f[i][j][1][0]= (f[i][j][1][0]+f[i-1][j][k][1])%M; }if(j) { for(Longk=1; k<=y; k++) f[i][j][k][1]= (f[i][j][k][1]+f[i][j-1][k-1][1])%M; for(Longk=0; k<=x; k++) f[i][j][1][1]= (f[i][j][1][1]+f[i][j-1][k][0])%M; }            }        }Longans=0; for(Longk=1; k<=x; k++) ans= (ans+f[n][m][k][0])%M; for(Longk=1; k<=y; k++) ans= (ans+f[n][m][k][1])%M; printf"%ld\n", ans); }}
MLE Code
////Created by Alvinzh on 2017/10/24.//Copyright (c) alvinzh. All rights reserved.//#include <cstdio>#include <cstring>#define MOD 1000007intN, m, x, y;intdp[205][205][ A][ A];intMain () { while(~SCANF ("%d %d %d %d", &n, &m, &x, &y)) {memset (DP,0,sizeof(DP)); dp[0][0][0][0] =1; for(inti =0; I <= N; ++i) { for(intj =0; J <= M; ++J) { for(intK =0; K <= x; ++K) { for(intL =0; L <= y; ++L) {if(Dp[i][j][k][l] = =0)Continue;if(I! = n && k! = x)//At the end of the North border soldier{Dp[i+1][j][k+1][0] + = Dp[i][j][k][l]; Dp[i+1][j][k+1][0]%= MOD; }if(J! = M && L! = y)//End is Savage soldier{Dp[i][j+1][0][l+1] + = Dp[i][j][k][l]; Dp[i][j+1][0][l+1]%= MOD; }                    }                }            }        }intAns =0; for(inti =1; I <= x; ++i) ans = (ans + dp[n][m][i][0])% MOD; for(inti =1; I <= y; ++i) ans = (ans + dp[n][m][0][i])% MOD; printf"%d\n", ans); }}/** Dp[i][j][x][y] indicates that I have a North border soldier J Savage involved in the arrangement, and at the end there are x consecutive North soldiers or y consecutive Savage Soldier scheme number.  */

2016-level algorithm third time on machine-g.winter is coming

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.