标签:des style blog http io ar color os sp
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 10951 | Accepted: 6000 |
Description

Input
Output
Sample Input
4 6 4 2 6 3 1 5 10 2 3 4 5 6 7 8 9 10 1 8 8 7 6 5 4 3 2 1 9 5 8 9 2 3 1 7 4 6
Sample Output
3 9 1 4题目翻译:题目说的啥翻译起来不重要了,第一个数据t代表t组数据,然后每组数据有一个n,紧接着有n个数字,分别是1-n的错乱排序,然后求这个序列的最长上升子序列。
解题思路:用暴力解决超时,于是在查找插入的时候采用2分法,轻松解决问题。
#include<stdio.h>
int a[40000],t,n,num,res,i,w;
int Q(int left,int right,int path){
int mid;
while(left<=right){
mid=(left+right)/2;
if(a[mid]<path) left=mid+1;
else right=mid-1;
}return right;
}
int main(){
scanf("%d",&t);
while(t--){
scanf("%d",&n);
scanf("%d",&num);
a[0]=num;
res=1;
for(i=1;i<n;i++){
scanf("%d",&num);
if(num>a[res-1]) a[res++]=num;
else if(num<a[res-1]){
w=Q(0,res-1,num);
a[w+1]=num;
}
}
printf("%d\n",res);
}return 0;
}
神一般的STL
#include<stdio.h>
#include<algorithm>
using namespace std;
int a[40000],t,n,num,res,i,w;
int main(){
scanf("%d",&t);
while(t--){
scanf("%d",&n);
scanf("%d",&num);
a[0]=num;
res=1;
for(i=1;i<n;i++){
scanf("%d",&num);
if(num>a[res-1]) a[res++]=num;
else if(num<a[res-1]){
w=lower_bound(a,a+res,num)-a;
a[w]=num;
}
}
printf("%d\n",res);
}return 0;
}
STL最简版
#include<stdio.h>
#include<algorithm>
using namespace std;
int a[40000],t,n,num,i,k,size;
int main(){
scanf("%d",&t);
while(t--){
scanf("%d",&n);
for(i=0,size=0;i<n;i++){
scanf("%d",&num);
k=lower_bound(a,a+size,num)-a;
a[k]=num;
size=max(size,k+1);
}
printf("%d\n",size);
}return 0;
}
标签:des style blog http io ar color os sp
原文地址:http://blog.csdn.net/hpuhjl/article/details/41649967