计算从给定的序列中最多可取出多少个长度为 s 的递增子序列。(给定序列中的每个元素最多只能被取出使用一次)
如果允许在取出的序列中多次使用 x1 和 xn,则从给定序列中最多可取出多少个长度为 s 的递增子序列。
注意:递增指非严格递增。
输入格式
第 1 行有 1 个正整数 n,表示给定序列的长度。
接下来的 1 行有 n 个正整数 x1,⋯,xn。
输出格式
第 1 行输出最长递增子序列的长度 s。
第 2 行输出可取出的长度为 s 的递增子序列个数。
第 3 行输出允许在取出的序列中多次使用 x1 和 xn 时可取出的长度为 s 的递增子序列个数。
数据范围
1≤n≤500
输入样例:
43 625
输出样例:
2
2
3
解题思路
最大流,拆点
先 dp 解出第一问,g[i] 表示前 i 个数以 a[i] 结尾的最长上升子序列的个数,对于 j<i,a[j]≤a[i],g[i]==g[j]+1,即 i 可以通过 j 这个点转移过来,将 j 向 i 建边,且容量为 1,假设 LIS 为 s,建立源点 S,S 向所有 g[i]=1 的 i 连边,且容量为 1,建立汇点 T,所有 g[i]=s 的 i 向 T 连边,且容量为 1,另外由于一个点可能会用到多次,但实际上只能用一次,即对点有限制,故需要拆点,即拆分为入点和出点,中间连一条容量为 1 的边,求解 S 到 T 的最大流即为第二问的答案,为什么?由于拆点,流网络中每个点只会用一次,对应原序列中每个 i 只会用一次,且 S 到 T 的路径长度为 s,可以保证原序列中所有求得的长度都为 s,当 1 和 n 不受限制时,将流网络中对应的正边容量设为足够大在榨干残余网络即可
时间复杂度:O(n2m)
代码
// Problem: 最长递增子序列问题// Contest: AcWing// URL: https://www.acwing.com/problem/content/2182/// Memory Limit: 256 MB// Time Limit: 1000 ms// // Powered by CP Editor (https://cpeditor.org)// %%%Skyqwq#include<bits/stdc++.h>//#define int long long#define help {cin.tie(NULL); cout.tie(NULL);}#define pb push_back#define fi first#define se second#define mkp make_pairusingnamespace std;
typedeflonglong LL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
template <typename T> boolchkMax(T &x, T y){ return (y > x) ? x = y, 1 : 0; }
template <typename T> boolchkMin(T &x, T y){ return (y < x) ? x = y, 1 : 0; }
template <typename T> voidinlineread(T &x){
int f = 1; x = 0; char s = getchar();
while (s < '0' || s > '9') { if (s == '-') f = -1; s = getchar(); }
while (s <= '9' && s >= '0') x = x * 10 + (s ^ 48), s = getchar();
x *= f;
}
constint N=1005,M=N+250005,inf=1e9;
int n,a[N],g[N],S,T;
int h[N],ne[M],f[M],e[M],idx;
int d[N],cur[N],hh,tt,q[N];
voidadd(int a,int b,int c){
e[idx]=b,f[idx]=c,ne[idx]=h[a],h[a]=idx++;
e[idx]=a,f[idx]=0,ne[idx]=h[b],h[b]=idx++;
}
boolbfs(){
memset(d,-1,sizeof d);
q[0]=S;
hh=tt=d[S]=0;
cur[S]=h[S];
while(hh<=tt)
{
int x=q[hh++];
for(int i=h[x];~i;i=ne[i])
{
int y=e[i];
if(d[y]==-1&&f[i])
{
d[y]=d[x]+1;
cur[y]=h[y];
if(y==T)returntrue;
q[++tt]=y;
}
}
}
returnfalse;
}
intdfs(int x,int limit){
if(x==T)return limit;
int flow=0;
for(int i=cur[x];~i&&flow<limit;i=ne[i])
{
cur[x]=i;
int y=e[i];
if(d[y]==d[x]+1&&f[i])
{
int t=dfs(y,min(f[i],limit-flow));
if(!t)d[y]=-1;
f[i]-=t,f[i^1]+=t,flow+=t;
}
}
return flow;
}
intdinic(){
int res=0,flow;
while(bfs())while(flow=dfs(S,inf))res+=flow;
return res;
}
intmain(){
memset(h,-1,sizeof h);
scanf("%d",&n);
S=0,T=2*n+1;
for(int i=1;i<=n;i++)scanf("%d",&a[i]);
int s=1;
for(int i=1;i<=n;i++)
{
g[i]=1;
for(int j=1;j<i;j++)
if(a[j]<=a[i])g[i]=max(g[i],g[j]+1),s=max(s,g[i]);
}
for(int i=1;i<=n;i++)
{
add(i,i+n,1);
for(int j=1;j<i;j++)
if(a[j]<=a[i]&&g[i]==g[j]+1)
add(j+n,i,1);
if(g[i]==1)add(S,i,1);
if(g[i]==s)add(i+n,T,1);
}
int res=0;
printf("%d\n%d\n",s,res=dinic());
if(s==1)printf("%d",n);
else {
for(int i=0;i<idx;i+=2)
{
int a=e[i^1],b=e[i];
if(a==S&&b==1)f[i]=inf;
if(a==1&&b==n+1)f[i]=inf;
if(a==n+n&&b==T)f[i]=inf;
if(a==n&&b==n+n)f[i]=inf;
}
printf("%d",res+dinic());
}
return0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!