P1020 导弹拦截

题目链接
第一问求的是最长不下降子序列,第二问求的是最长上升子序列,o(nlogn)可以求出,其实第二问为什么是最长上升我自己也有点懵逼,只是之前写过一个类似的题才知道的。

#include<iostream>
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<string>
#include<vector>
#include<queue>
#include<algorithm>
#include<deque>
#include<map>
#include<stdlib.h>
#include<set>
#include<iomanip>
#include<stack>
#define ll long long
#define ms(a,b) memset(a,b,sizeof(a))
#define lowbit(x) x & -x
#define fi first
#define ull unsigned long long
#define se second
#define endl "\n"
#define bug cout<<"----acac----"<<endl
#define IOS ios::sync_with_stdio(false), cin.tie(0),cout.tie(0)
using namespace std;
const int maxn =1e5 + 5;
const int maxm = 1.5e5+50;
const double eps = 1e-7;
const double inf = 0x3f3f3f3f;
const double  lnf  = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9+7;
const int p=1e7+233;
const  double pi=3.141592653589;
int a[maxn],q1[maxn],q2[maxn];
int main()
{
    int n=1;
    while(cin>>a[n])n++;
    n--;
    q1[1]=q2[1]=a[1];
    int cnt1=1,cnt2=1;
    for(int i=2;i<=n;i++)
    {
        if(q1[cnt1]>=a[i])
        {
            q1[++cnt1]=a[i];
        }
        else
        {
            int id=upper_bound(q1+1,q1+1+cnt1,a[i],greater<int>())-q1;//最长不下降子序列
            q1[id]=a[i];
        }
        if(q2[cnt2]<a[i])
        {
            q2[++cnt2]=a[i];
        }
        else
        {
             int id=lower_bound(q2+1,q2+1+cnt2,a[i])-q2;//最长上升子序列(严格上升)
             q2[id]=a[i];
        }
    }
    cout<<cnt1<<endl<<cnt2<<endl;
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值