Dynamic Programming of missile interceptors

Baidu topic.

Read a lot of different explanations (although the effect is almost, but I was too weak so that some do not understand quq)

I think this question has the following key points:
  1. Clear meaning of the questions, to understand its essence, that is, to find the longest non-increasing number of columns

  2. This card is my long point, how to use dp?

    2.1 First of all, if you look from front to back, starting with the first digit, is more difficult to determine how to get the optimal solution (unless all try again)
    2.2 If you look from the back, to take each assume the numbers are minimal, sounds good, but is easily muddle data, such as 9 7 6 8 4 9 5 3 1, so as to take a forward backstepping is misleading when the head is such that it rests on the first 9, it is not OK
    2.3 If every position and the number of cycles to keep pace update it?
    Is the current number of reference, to find the longest increasing sequence from back to front , a further opening ANS array [] is used to record each number with its front long sequences can be constructed so that the groups may be the same string together like beads ( If the groups meet end to end relationship between the words).

code show as below:

#include<cstdio>
#include<cstring> 
int num[51],ans[51];

int main(){
	int i,j,n,max; 
	scanf("%d",&n);
	for(i=0;i<n;i++){
		scanf("%d",&num[i]);
	}
	//求最长非递增子序列
	memset(ans,0,sizeof(ans));
	for(i=0;i<n;i++){
		ans[i]=1;
		for(j=0;j<n;j++){ 
			if(num[i]<num[j]&&ans[i]<ans[j]+1){//保证了从大到小的顺序同时衔接上不同长度段 
				ans[i]=ans[j]+1;
			} 
		}
		if(i==0){
			max=ans[0];
		}else if(ans[i]>max){
			max=ans[i];
		} 
	} 
		printf("%d",max);
} 

Output is the answer.

Released three original articles · won praise 0 · Views 104

Guess you like

Origin blog.csdn.net/LucyHolmes/article/details/104435540