动态规划练习 14
题目:Bridging signals (POJ 1631)
链接:http://acm.pku.edu.cn/JudgeOnline/problem?id=1631
#include <iostream>
#include <vector>
#include <limits.h>
using namespace std;
// O(nlogn)
int LIS(const vector<int> &data)
{
// if min_value[i] is j, means j is the min end value of all i length
// increasing sequences.
vector<int> min_value(data.size(), INT_MAX);
int len = 1;
for (size_t i = 0; i < data.size(); ++i)
{
int left = 1, right = len;
while (left <= right)
{
int mid = (left + right) / 2;
if (data[i] > min_value[mid])
{
left = mid + 1;
}
else
{
right = mid - 1;
}
}
// right = left - 1
// min_value[right] < data[i], min_value[left] >= data[i]
if (data[i] < min_value[left])
{
min_value[left] = data[i];
}
if (len < left)
{
len = left;
}
}
return len;
}
int main(int argc, char **argv)
{
int cases, n;
vector<int> data;
cin >> cases;
while (cases--)
{
cin >> n;
while (n--)
{
int tmp;
cin >> tmp;
data.push_back(tmp);
}
cout << LIS(data) << endl;
data.clear();
}
return 0;
}