随笔 - 0  文章 - 5  trackbacks - 0
<2025年6月>
25262728293031
1234567
891011121314
15161718192021
22232425262728
293012345

常用链接

留言簿(2)

文章分类

文章档案

教育

信息学奥赛

有用网站

在线OJ

专题测试

租房信息

搜索

  •  

最新评论

最优算法:时间复杂度最坏为nlogn
#include<iostream>
#include
<algorithm>
using namespace std;
int f[100000],top;
int main()
{
    
int n,*p,x;
    cin
>>n;
    top
=0;
    
while (n--)
    {        
        cin
>>x;
        p
=lower_bound(f,f+top,x,greater<int>()); 
        
//删掉greater<int>()或换成less<int>()则为最长上升子序列 
        *p=x;
        
if (p-f>=top) top++;         
    }
    cout
<<top<<endl;
    system(
"pause");
    
return 0;
}

使用向量动态开辟空间:
#include<iostream>
#include
<vector>
#include
<algorithm>
using namespace std;
int main()
{
    vector
<int> v;
    vector
<int>::iterator it;
    
int n,x,top;
    cin
>>n;
    
while (n--)
    {
        cin
>>x;
        it
=lower_bound(v.begin(),v.end(),x);        
        
if (it==v.end()) v.push_back(x);
        
else *it=x;
    }
    cout
<<v.size()<<endl;
    system(
"pause");
    
return 0;


posted on 2011-10-30 17:21 龙在江湖 阅读(274) 评论(0)  编辑 收藏 引用 所属分类: 算法