NOIP 拦截导弹

题目链接:https://www.luogu.org/problemnew/show/P1020
题意很简洁,一共两问。
第一问是求单个拦截系统最多能拦截多少个,根据题意即最长非上升子序列。
第二问有多少导弹拦截系统才能拦截全部导弹,即有多少最长非上升子序列才能包括全部数。
稍加思考即可得出最长上升子序列的长度即为答案。
本题的转化思考和两个求序列长度写法要记一下(就我目前看到的题解貌似没人这么写。。。)。

#include<map>
#include<stack>
#include<queue>
#include<cmath>
#include<string>
#include<cstdio>
#include<cstring>
#include<iomanip>
#include<iostream>
#include<algorithm>
using namespace std;
#define LL long long int
#define lson rt<<1,l,m
#define rson rt<<1|1,m+1,r
const int inf = 0x3f3f3f3f;
int s[100100];
int up[100100];
int down[100100];
int main()
{
    int cmp = 0;
    memset(up, inf, sizeof(up));
    memset(down, inf, sizeof(down));
    while (~scanf("%d", &s[cmp++])); cmp--;
    for (int i =cmp-1;i>=0;i--)
        *upper_bound(down, down + cmp, s[i]) = s[i];
    for (int i = 0; i < cmp; i++)
        *lower_bound(up, up + cmp, s[i]) = s[i];
    int res = lower_bound(down, down + cmp, inf) - down;
    int ans = lower_bound(up, up + cmp, inf) - up;
    cout << res << endl;
    cout << ans << endl;
    //system("pause");
    return 0;
}

猜你喜欢

转载自blog.csdn.net/b_r_e_a_d/article/details/81107416