UVa 481 - What Goes Up

时间:2023-03-09 16:20:51
UVa 481 - What Goes Up

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

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

 #include <cstdio>
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
typedef vector<int> vi; vi v, temp, pos, ans; int main()
{
#ifdef LOCAL
freopen("in", "r", stdin);
#endif
int x;
while (scanf("%d", &x) != EOF)
v.push_back(x);
temp.push_back(v[]);
pos.push_back();
for (int i = ; i < v.size(); i++)
{
x = v[i];
if (x > temp.back())
{
temp.push_back(x);
pos.push_back(temp.size());
}
else
{
vi::iterator it = lower_bound(temp.begin(), temp.end(), x);
*it = x;
pos.push_back(it-temp.begin()+);
}
}
for (int p = pos.size()-, len = temp.size(); p >= && len > ; p--)
if (pos[p] == len)
{
ans.push_back(v[p]);
len--;
}
cout << ans.size() << endl << '-' << endl;
for (int i = ans.size()-; i >= ; i--)
cout << ans[i] << endl;
return ;
}