Link: https://www.nowcoder.com/acm/contest/116/C
Source: Niuke.com
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
Question meaning: Chinese question
analysis: I didn't think of it during the competition. After listening to the explanations of the big guys, I understood it a little bit. Find the longest increasing subsequence from left to right, and find the longest increasing subsequence from right to left.
Code:
#include<bits/stdc++.h> using namespace std; #define ll long long int a[500000+50]; int dp[500000+50]; int years[500000+50]; intmain() { int n; cin>>n; for(int i=0;i<n;i++){ cin>>a[i]; } int num=0; for(int i=0;i<n;i++){ int *p=upper_bound(dp,dp+num,a[i]); if(p==dp+num){ num++; } *p=a[i]; ans[i]=num; } num=0; for(int i=i=n-1;i>=0;i--){ int *p=upper_bound(dp,dp+num,a[i]); if(p==dp+num){ num++; } *p=a[i]; ans[i]+=num; } int maxans=ans[0]; for(int i=1;i<n;i++){ maxans=max(maxans,ans[i]); } cout<<maxans-1<<endl; return 0; }