Luogu P1020 [NOIP1999 Popularization Group] Missile Interception (dp template question)

Problem Description:

Insert picture description here


code show as below:

#include<bits/stdc++.h>
using namespace std;
const int maxn=100010;
int a[maxn],d1[maxn],d2[maxn],n;
int main()
{
    
    
	while(cin>>a[++n]);
	n--;
	d1[1]=a[1];//求不上升子序列 
	d2[1]=a[1];//求上升子序列 
	int len1=1,len2=1;
	for(int i=2;i<=n;i++){
    
    
		if(d1[len1]>=a[i]) d1[++len1]=a[i];
		else{
    
    
			int pos=upper_bound(d1+1,d1+1+len1,a[i],greater<int>())-d1;//找出第一个小于a[i]的数的位置 
			d1[pos]=a[i];//替换位置 
		}
		if(d2[len2]<a[i]) d2[++len2]=a[i];
		else{
    
    
			int pos=lower_bound(d2+1,d2+1+len2,a[i])-d2;//找出第一个大于等于a[i]的数的位置 
			d2[pos]=a[i];//替换位置 
		}
	}
	cout<<len1<<endl<<len2<<endl;
	return 0;
} 

to sum up:

Dynamic programming + dichotomy template question

lower_bound: find the first number greater than or equal to x in the sequence

upper_bound: find the first number greater than x in the sequence

Add greater() to handle descending sequences

Insert picture description here
PS: The picture is taken from a video of a big guy's station b.
In short, the front and back are symmetrical (personally understand it this way)

Guess you like

Origin blog.csdn.net/Lzhzl211/article/details/114650303