拦截导弹(DP)

在这里插入图片描述

思路:我们可以发现题目要求的是最长不上升子序列,那么我们倒着求一遍最长上升子序列就可以了这就是ans1,然后问最少配多少套我们再正向求一遍最长上升子序列这就是ans2

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <set>
#include<iostream>
//#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define space putchar(' ')
#define enter putchar('\n')
typedef pair<int,int> PII;
const int mod=1e4+7;
const int N=2e6+10;
const int inf=0x7f7f7f7f;


ll gcd(ll a,ll b)
{
    return b==0?a:gcd(b,a%b);
}

ll lcm(ll a,ll b)
{
    return a*(b/gcd(a,b));
}

template <class T>
void read(T &x)
{
    char c;
    bool op = 0;
    while(c = getchar(), c < '0' || c > '9')
        if(c == '-')
            op = 1;
    x = c - '0';
    while(c = getchar(), c >= '0' && c <= '9')
        x = x * 10 + c - '0';
    if(op)
        x = -x;
}
template <class T>
void write(T x)
{
    if(x < 0)
        x = -x, putchar('-');
    if(x >= 10)
        write(x / 10);
    putchar('0' + x % 10);
}

int fa[N];
int n,m;
int a[N];
int dp[N];
int main()
{
   int n;
   int cnt=0;
    while(~scanf("%d",&a[cnt]))cnt++;
    //cout<<cnt<<endl;
    fill(dp,dp+cnt,inf);
    for(int i=cnt-1;i>=0;i--)
    {
        *lower_bound(dp,dp+cnt,a[i])=a[i];
    }
    int ans1=lower_bound(dp,dp+cnt,inf)-dp;
    int ans2;
    fill(dp,dp+cnt,inf);
    for(int i=0;i<cnt;i++)
    {
        *lower_bound(dp,dp+cnt,a[i])=a[i];
    }
    ans2=lower_bound(dp,dp+cnt,inf)-dp;


    printf("%d\n%d\n",ans1,ans2);


    return 0;
}








  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值