Timus 1260. Nudnik photographer Dynamic Planning

Source: Internet
Author: User

Topic Portal

To finish the problem. The DP image is a state in transition, pushed from one (more) current state to the next (many) states.

This topic means that n people want to take pictures, and the Age of N is 1 to n. Standing in a row, the leftmost segment is 1 years old, and the age difference between the two adjacent persons will not exceed 2 years. Ask how many total queuing scenarios there are.

Solution:

The status of individual affiliation can be pushed from the state of I-1. We know that the first person will be at least adjacent to i-1 or i-2. Therefore, [i-1] and [i-2] are used as states. Can be divided into four different states:

①[i-1][i-2] i.e. i-1,i-2 adjacent and i-1 in front of i-2

②[I-2][I-1]

③[i-1][i-2] ....   And [I-2][i-1] .... That is, [I-1][i-2] is adjacent but not at the end (the analysis shows whether I-1,i-2 is close to the next state at the end, and if there is no such state, the state is omitted from the i-1 push to i)

④[i-2] ... [I-1] That is i-1,i-2 non-sequential, the analysis of i-1 must be at the end.

We can quickly conclude that when I am added to the first person, the status ② can be introduced status ①, state ①④ can be launched status ②, the status ①③ can be launched ③,①④ can launch ②.

Thus the state-state transition equation:

DP[1][I]=DP[2][I-1];
DP[2][I]=DP[2][I-1]+DP[4][I-1];
DP[3][I]=DP[1][I-1]+DP[3][I-1];
DP[4][I]=DP[1][I-1];

Code

#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespacestd;intMain () {intdp[5][ -]; Memset (DP,0,sizeof(DP)); dp[1][1]=1; dp[2][2]=1;  for(intI=3; i<= -; i++) {dp[1][i]=dp[2][i-1]; dp[2][i]=dp[2][i-1]+dp[4][i-1]; dp[3][i]=dp[1][i-1]+dp[3][i-1]; dp[4][i]=dp[1][i-1]; }    intN;  while(~SCANF ("%d",&N)) {cout<<dp[1][n]+dp[2][n]+dp[3][n]+dp[4][n]<<Endl; }    return 0;}

Later, it feels simpler to have another code on the Web.

The seat is DP, when the first is 1, the second is 2, the combination is dp[i-1]; When the first one is 1, the second is 3, the third one is 2, the combination is dp[i-3]; and the last case is 1357 ... 8642.

So the DP equation is dp[i] = dp[i-1]+dp[i-3]+1.

#include <stdio.h>intN;intdp[ -];intMain () {scanf ("%d", &N); dp[1] =1; dp[2] =1; dp[3] =2; dp[4] =4;  for(inti =5; I <= -; ++i) {Dp[i]= dp[i-1]+dp[i-3]+1; } printf ("%d", Dp[n]); return 0;}

Timus 1260. Nudnik photographer Dynamic Planning

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.