思路:倒着求一遍最长上升子序列得到ans1,正向求一遍最长上升子序列就是ans2。
证明:为什么正向求一次最长递增子串就是最少需要配置的系统数。
正向数出来的最长递增子序列中,不可能有两个在同一套系统里拦截,所以可得到该解>=answer
(可能[Dilworth定理](https://baike.baidu.com/item/%E7%8B%84%E5%B0%94%E6%B2%83%E6%96%AF%E5%AE%9A%E7%90%86/18900593?fromtitle=Dilworth%E5%AE%9A%E7%90%86&fromid=5489361&fr=aladdin)
证明的是这个)
还需证该解<=answer:
设b[1]~b[i]是最长递增子串上的前i个数,b[i]到b[i+1]之间的子串的最长递增子串最多长为i,
如果为i+1, 那就有一个比b[1]到b[i]更长的子串了,矛盾。
然后要把b[i]到b[i+1]之间的 i 个递增的数分配到b[1]~b[i]个系统上,
直接按顺序一一对应地分配,即最小的分给b[1]开头的系统,最大的分给b[i]开头的系统
(如果b[i]到b[i+1]之间最长递增子串长度为i的话)。
b[i-1]到b[i]之间又可能有 i-1 个数组成的递增子串,
这个子串中应该不存在比 b[i]~b[i+1]之间的长 i 的递增子串的最小值还小的,
如果存在的话就可以组成 i+1个数的比b[i+1]小的递增子串了,矛盾。
同理,前一个子串中比后一个子串中倒数第二小的数还小的数最多只有1个,
比倒数第k个数小的数最多有k-1个,k=1~i,
也就是说大于等于倒数第k个数的数最少有i-k个,k=i时取b[i]系统来放,
所以这样一一分配法保证每个都能放下。
#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;
}
注:除证明外都是引用自别人的博客。