Diligent teacher Yang

Topic description

Mr. Yang believes that his learning ability curve is an arch. Diligently, he made a learning list according to the order of time, with a total of n knowledge points. However, the knowledge in the list is not necessarily to be learned. You can choose to learn it without changing the order, and each knowledge point corresponds to a difficulty value. Mr. Yang hopes that the difficulty of the knowledge points learned later must not be lower than the difficulty of the previous knowledge point (ai<=aj when i<j), and there may be a critical point, after the critical point, the knowledge he hopes to learn later The difficulty of the point must not be higher than the difficulty of the previous knowledge point (ai>=aj when i<j). Teacher Yang wants to learn as much knowledge as possible. Question: How much knowledge can Mr. Yang learn at most?

Enter description:

The first line: an integer n (0<n<500000) The next line: n integers, the ith integer ai (0<=ai<500000) represents the difficulty of the ith problem.

Output description:

One integer per line indicates how much knowledge Mr. Yang can learn at most.
Example 1

enter

5
1 4 2 5 1

output

4 

Very good question, find the longest increasing subsequence.
Do it in two.
 1 #include <bits/stdc++.h>
 2 #define INF 0x3f3f3f3f
 3 #define ll long long
 4 using namespace std;
 5 const int N = 5e5+10;
 6 int a[N], dp1[N], dp2[N], f[N];
 7 int main() {
 8     int n, MAX = 0;
 9     cin >> n;
10     for(int i = 0; i < n; i ++) cin >> a[i];
11     memset(f,INF,sizeof(f));
12     for(int i = 0; i < n; i ++) {
13         *upper_bound(f,f+n,a[i]) = a[i];
14         dp1[i] = lower_bound(f,f+n,INF)-f;
15     }
16     for(int i = 0; i < n/2; i ++ )swap(a[i],a[n-i-1]);
17     // for(int i = 0; i < n; i ++) printf("%d ", a[i]);printf("\n");
18     memset(f,INF,sizeof(f));
19     for(int i = 0; i < n; i ++) {
20         *upper_bound(f,f+n,a[i]) = a[i];
21         dp2[i] = lower_bound(f,f+n,INF)-f;
22     }
23     for(int i = 0; i < n; i ++) {
24         // printf("%d %d\n",dp1[i],dp2[i]);
25         MAX = max(MAX,dp1[i]+dp2[n-i-1]);
26     }
27     printf("%d\n",MAX-1);
28     return 0;
29 }

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325209790&siteId=291194637