题目描述
某国为了防御敌国的导弹袭击,发展出一种导弹拦截系统。但是这种导弹拦截系统有一个缺陷:虽然它的第一发炮弹能够到达任意的高度,但是以后每一发炮弹都不能高于前一发的高度。某天,雷达捕捉到敌国的导弹来袭。由于该系统还在试用阶段,所以只有一套系统,因此有可能不能拦截所有的导弹。
输入导弹依次飞来的高度(雷达给出的高度数据是\le 50000≤50000的正整数),计算这套系统最多能拦截多少导弹,如果要拦截所有导弹最少要配备多少套这种导弹拦截系统。
输入输出格式
输入格式:
11行,若干个整数(个数\le 100000≤100000)
输出格式:
22行,每行一个整数,第一个数字表示这套系统最多能拦截多少导弹,第二个数字表示如果要拦截所有导弹最少要配备多少套这种导弹拦截系统。
输入输出样例
输入样例#1:
389 207 155 300 299 170 158 65
输出样例#1:
6 2
解法:优化的最长上升子序列问题
最近经常遇到这一类的dp。今天选拔赛也遇到一道用n^2算法但始终超时。
一直没有 学会这个NlogN算法的姿势。
现在学习一波。
#include <iostream>
#include <algorithm>
#include <climits>
#include <cstring>
#include <vector>
#include <map>
#define pii pair<int, int>
#define vi vector<int>
#define ll long long
#define eps 1e-5
using namespace std;
const int maxn = 2e5 + 10;
int a[maxn];
int dp[maxn];
int asc[maxn];
map<int, int> vis;
int main()
{
// freopen("/Users/vector/Desktop/testdata.in", "r", stdin);
ios::sync_with_stdio(false);
cin.tie(0);
int n = 0;
while(cin >> a[n++]);
int len = 0;
memset(dp, 0, sizeof(dp));
for(int i = 0; i < n; i++)
{
*upper_bound(dp, dp + n, a[i], greater<int>()) = a[i];
}
len = lower_bound(dp, dp + n, 0,greater<int>()) - dp;
cout << len << endl;
memset(asc, 1, sizeof(asc));
for(int i = 0; i < n; i++)
{
*lower_bound(asc, asc + n, a[i]) = a[i];
}
len = lower_bound(asc, asc + n, 0x1010101) - asc;
cout << len << endl;
return 0;
}