1 条题解

  • 0
    @ 2023-6-21 19:57:31

    C++ :

    #include<bits/stdc++.h>
    using namespace std;
    
    #define gc (c=getchar())
    int read()
    {
    	char c;
    	while(gc<'0');
    	int x=c-'0';
    	while(gc>='0')x=x*10+c-'0';
    	return x;
    }
    #define rep(i,l,r) for(int i=l;i<=r;++i)
    inline void chmax(int &x,int y)
    {
    	if(x<y)x=y;
    }
    namespace flow
    {
    const int N=550*2,M=N*N,oo=2139062143;
    int n,S,T;
    int t[N],a[N],g[N];
    struct edge
    {
    	int to,next,f;
    }l[M];int e=1;
    void add_e(int x,int y,int f=oo)
    {
    	l[++e]=(edge){y,t[x],f};t[x]=e;
    	l[++e]=(edge){x,t[y],0};t[y]=e;
    }
    int dfs(int x,int f)
    {
    	if(x==T)return f;
    	int f0=f;
    	for(int &i=a[x],y;y=l[i].to;i=l[i].next)
    	if(l[i].f&&g[y]<g[x])
    	{
    		int del=dfs(y,min(f,l[i].f));
    		l[i].f-=del;l[i^1].f+=del;
    		f-=del;
    		if(!f) return f0;
    	}
    	return f0-f;
    }
    int q[N],head,tail;
    bool bfs()
    {
    	memset(g+1,(1<<7)-1,n*sizeof(int));
    	g[T]=0;
    	tail=1;q[1]=T;
    	for(int head=1;head<=tail;++head)
    	{
    		int x=q[head];
    		for(int i=t[x],y;y=l[i].to;i=l[i].next)
    		if(l[i^1].f&&g[y]==2139062143) 
    		{
    			g[y]=g[x]+1;
    			if(y==S)return 1;
    			q[++tail]=y;
    		}
    	}
    	return 0;
    }
    int max_flow()
    {
    	int ans=0;
    	while(bfs()) 
    	{
    	  memcpy(a+1,t+1,n*sizeof(int));
    	  ans+=dfs(S,oo);
    	}
    	return ans;
    }
    void write(int x)
    {
    	for(int i=t[x],y;y=l[i].to;i=l[i].next)
    	if(l[i].f&&y!=T) printf("%d ",y);
    	puts("");
    }
    };
    
    const int N=505;
    int a[N],f[N];
    
    int main()
    {
    	//freopen("1.in","r",stdin);
    	int n;
    	scanf("%d",&n);
    	
    	flow::n=n*2+2;flow::S=flow::n-1;flow::T=flow::n;
    	
    	int ans=0;
    	rep(i,1,n)
    	{
    		a[i]=read();
    	  rep(j,1,i-1) if(a[i]>=a[j])chmax(f[i],f[j]);
    	  ++f[i];
    	  chmax(ans,f[i]);
        }
        printf("%d\n",ans);
        if(ans==1)
        {
        	printf("%d\n%d\n",n,n);
        	return 0;
        }
        rep(i,1,n)
        {
        	flow::add_e(i*2-1,i*2,1);
          if(f[i]==1)flow::add_e(flow::S,i*2-1);
          if(f[i]==ans)flow::add_e(i*2,flow::T);
          rep(j,1,i-1) if(a[i]>=a[j]&&f[i]==f[j]+1) flow::add_e(j*2,i*2-1,1);
        }
    	ans=flow::max_flow();
    	printf("%d\n",ans);
    	flow::add_e(1,2);flow::add_e(n*2-1,n*2);
    	printf("%d\n",ans+flow::max_flow());
    }
    
    • 1

    #6005. 「网络流 24 题」最长递增子序列

    信息

    ID
    994
    时间
    1000ms
    内存
    256MiB
    难度
    (无)
    标签
    递交数
    0
    已通过
    0
    上传者