#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define N 500005
int n;
int backup[N];
int a[N];
int top;
void LIS() {
memset(backup, 0, sizeof(backup));
top = 0;
backup[top] = 0xbfbfbfbf;
for(int i = 1; i <= n; i++) {
//如果a[i]>栈顶部元素,则压栈
if(a[i] > backup[top]) {
backup[++top] = a[i];
} else { //如果a[i]不大于栈顶部元素,则二分查找第一个比a[i]大的元素
int l = 1, r = top;
while(l <= r) {//二分找下标,下标从1开始
int mid = (l+r) >> 1;
if(a[i] > backup[mid])
l = mid + 1;
else
r = mid - 1;
}
//替换a[i]
backup[l] = a[i];
}
}
}
int main() {
freopen("data.in", "r", stdin);
int t;
scanf("%d", &t);
while(t--) {
scanf("%d", &n);
memset(a, 0, sizeof(a));
for(int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
}
LIS();
printf("%d\n", top);
}
return 0;
}
HDU 1905 Bridging signals(LIS)
最新推荐文章于 2019-05-16 12:18:02 发布