标签:str oar names clipboard can lse print ace ++
第1行:1个数N,N为序列的长度(2 <= N <= 50000) 第2 - N + 1行:每行1个数,对应序列的元素(-10^9 <= S[i] <= 10^9)
输出最长递增子序列的长度。
8
5
1
6
8
2
4
5
10
5
两重循环dp会超时,所以用二分求最长上升子序列。
超时:
#include <iostream> #include <cstdio> #define MAX 50000 using namespace std; int ar[MAX + 10]; int dp[MAX + 10]; int n,ans; int main() { scanf("%d",&n); for(int i = 1;i <= n;i ++) { scanf("%d",&ar[i]); } for(int i = 1;i <= n;i ++) { dp[i] = 1; for(int j = 1;j < i;j ++) { if(ar[i] > ar[j]) { dp[i] = max(dp[i],dp[j] + 1); } } ans = max(ans,dp[i]); } printf("%d",ans); }
代码:
#include <iostream> #include <cstdio> #define MAX 50000 using namespace std; int dp[MAX],n,d,c; int main() { scanf("%d",&n); for(int i = 0;i < n;i ++) { scanf("%d",&d); if(!c || d > dp[c - 1]) dp[c ++] = d; else { int l = 0,r = c - 1; while(l < r) { int mid = (l + r) / 2; if(dp[mid] < d) l = mid + 1; else r = mid; } dp[l] = d; } } printf("%d",c); }
标签:str oar names clipboard can lse print ace ++
原文地址:https://www.cnblogs.com/8023spz/p/10015229.html