UVa 481 - What Goes Up

  题目大意:给你一系列数,找出它的最长(严格)递增子序列。

  由于数据量较大,使用O(n2)的LIS算法会超时,要使用O(nlogn)的LIS算法,这里有详细的介绍。

 1 #include <cstdio>
 2 #include <iostream>
 3 #include <algorithm>
 4 #include <vector>
 5 using namespace std;
 6 typedef vector<int> vi;
 7 
 8 vi v, temp, pos, ans;
 9 
10 int main()
11 {
12 #ifdef LOCAL
13     freopen("in", "r", stdin);
14 #endif
15     int x;
16     while (scanf("%d", &x) != EOF)
17         v.push_back(x);
18     temp.push_back(v[0]);
19     pos.push_back(1);
20     for (int i = 1; i < v.size(); i++)
21     {
22         x = v[i];
23         if (x > temp.back())
24         {
25             temp.push_back(x);
26             pos.push_back(temp.size());
27         }
28         else
29         {
30             vi::iterator it = lower_bound(temp.begin(), temp.end(), x);
31             *it = x;
32             pos.push_back(it-temp.begin()+1);
33         }
34     }
35     for (int p = pos.size()-1, len = temp.size(); p >=0 && len > 0; p--)
36         if (pos[p] == len)
37         {
38             ans.push_back(v[p]);
39             len--;
40         }
41     cout << ans.size() << endl << '-' << endl;
42     for (int i = ans.size()-1; i >= 0; i--)
43         cout << ans[i] << endl;
44     return 0;
45 }
View Code

 

转载于:https://www.cnblogs.com/xiaobaibuhei/p/3304280.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值