-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy path11456-Trainsorting.cpp
42 lines (41 loc) · 1.16 KB
/
11456-Trainsorting.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <bits/stdc++.h>
using namespace std;
int main()
{
int t,n,v;
scanf("%d",&t);
while(t--){
vector<int> nums;
scanf("%d",&n);
for(int i=0;i<n;i++){
scanf("%d",&v);
nums.push_back(v);
}
vector<int> LIS;
deque<int> LDS;
int res = 0;
// from backwards, since we want to compute
// and store the dp length at i
for(int i=n-1;i>=0;i--){
int lower = lower_bound(LIS.begin(),LIS.end(),nums[i]) - LIS.begin();
int upper = upper_bound(LDS.begin(),LDS.end(),nums[i]) - LDS.begin();
int dpInc, dpDec;
if(lower == LIS.size()){
LIS.push_back(nums[i]);
dpInc = LIS.size();
} else {
LIS[lower] = nums[i];
dpInc = lower+1;
}
if(upper == 0){
LDS.push_front(nums[i]);
dpDec = LDS.size();
} else {
LDS[upper-1] = nums[i];
dpDec = LDS.size()-(upper-1);
}
res = max(res, dpInc+dpDec-1);
}
printf("%d\n",res);
}
}