最长上升子序列的贪心算法
06月 18, 2008
1 comment
我用的二分实现的所以时间复杂度是O(nlogn)
谁看得出来为什么是贪心吗?话说这是pku1631
#include <iostream>
using namespace std;
int const maxn = 40000 + 5;
int a[maxn], b[maxn];
void solve(){
int n;
scanf("%d",&n);
for (int i = 0 ; i < n; i++)
scanf("%d",&a[i]);
int t = 0;
b[0] = a[n - 1];
for (int i = n-2; i>=0; i--)
if (a[i] < b[t]) b[++t] = a[i];
else {
int l = 0, r = t;
while (r > l){
int m = (l + r) / 2;
if (a[i] >= b[m]) r = m; else l = m + 1;
}
b[l] = a[i];
}
printf("%d\n",t+1);
}
int main(){
int t;
for (scanf("%d",&t); t > 0; t--)
solve();
return 0;
}
友情提示:群论
这算法是对的吗?....