UVa 481

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

  由于数据量较大,使用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