ac code:
#include <iostream> #include <algorithm> #include <cstdio> #include <cstring> using namespace std; typedef long long ll; #define INF 1e9 #define maxn 100005 int a[maxn]; //记录原序列 int d[maxn]; //d[k] 表示长度为k的上升子序列结尾的最小值 int f[maxn]; //f[t] 表示以a[t]结尾的上升子序列的最大长度 int main() { int n,m; int cas; cin >> cas; while(cas --){ cin >> n; for(int i = 1;i <= n;i ++){ cin >> a[i]; } //nlogn 的dp 求最长公共子串 for(int i = 1;i <= n;i ++){ d[i] = INF; f[i] = 1; } int nowK = 1; for(int i = 1;i <= n;i ++){ int l,r; l = 1; r = i+1; while(l <= r){ int m = (l+r)/2; if(a[i] > d[m]) l = m + 1; else r = m - 1; } f[i] = l; d[l] = a[i]; } for(int i = 1;i <= n-1;i ++) printf("%d ",f[i]); printf("%d\n",f[n]); } return 0; }
时间: 2024-12-13 09:10:01