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

题目链接:https://loj.ac/problem/6005

题意:略。

思路:首先动态规划求出F[i],表示以第i位为开头的最长上升序列的长度,求出最长上升序列长度K。1、把序列每位i拆成两个点<i.a>和<i.b>,从<i.a>到<i.b>连接一条容量为1的有向边。2、建立附加源S和汇T,如果序列第i位有F[i]=K,从S到<i.a>连接一条容量为1的有向边。3、如果F[i]=1,从<i.b>到T连接一条容量为1的有向边。4、如果j>i且A[i] <= A[j]且F[j]+1=F[i],从<i.b>到<j.a>连接一条容量为1的有向边。

求网络最大流,就是第二问的结果。把边(<1.a>,<1.b>)(<N.a>,<N.b>)(S,<1.a>)(<N.b>,T)这四条边的容量修改为无穷大,再求一次网络最大流,就是第三问结果。

P.S.:在maxlen==1的时候第三问直接输出跟第二问一样的结果就可以了,虽然答案应该是无穷大。

P.P.S:点限制的题目拆点连边要熟练掌握。

#pragma comment(linker, "/STACK:102400000,102400000")
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <string>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <sstream>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#define MOD 1e9+7
#define lson l,mid,root<<1
#define rson mid+1,r,root<<1|1
#define DBN1(a)           cerr<<#a<<"="<<(a)<<"\n"
#define DBN2(a,b)         cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<"\n"
#define DBN3(a,b,c)       cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<"\n"
#define DBN4(a,b,c,d)     cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<", "<<#d<<"="<<(d)<<"\n"
#define DBN5(a,b,c,d,e)   cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<", "<<#d<<"="<<(d)<<", "<<#e<<"="<<(e)<<"\n"
#define DBN6(a,b,c,d,e,f) cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<", "<<#d<<"="<<(d)<<", "<<#e<<"="<<(e)<<", "<<#f<<"="<<(f)<<"\n"
using namespace std;
typedef long long ll;
const int maxn=10000+5;
const int maxm=500+5;
const int INF=0x3f3f3f3f;
const double PI=acos(-1.0);
template<typename T>
inline T read(T&x){
    x=0;int _f=0;char ch=getchar();
    while(ch<'0'||ch>'9')_f|=(ch=='-'),ch=getchar();
    while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
    return x=_f?-x:x;
}
struct Edge{
    int from,to,cap,flow;
    Edge(int u,int v,int c,int f):from(u),to(v),cap(c),flow(f){}
};
struct Dinic{
    int n,m,s,t;
    vector<Edge>edges;
    vector<int>G[maxn];
    bool vis[maxn];
    int d[maxn];
    int cur[maxn];

    void init(int n){
        for (int i=0;i<n;i++) G[i].clear();
        edges.clear();
    }

    void AddEdge(int from,int to,int cap){
        edges.push_back(Edge(from,to,cap,0));
        edges.push_back(Edge(to,from,0,0));
        m=edges.size();
        G[from].push_back(m-2);
        G[to].push_back(m-1);
    }

    bool BFS(){
        memset(vis,0,sizeof(vis));
        queue<int>Q;
        Q.push(s);
        d[s]=0;
        vis[s]=1;
        while (!Q.empty()){
            int x=Q.front();Q.pop();
            for (int i=0;i<(int)G[x].size();i++){
                Edge& e=edges[G[x][i]];
                if (!vis[e.to] && e.cap>e.flow){
                    vis[e.to]=1;
                    d[e.to]=d[x]+1;
                    Q.push(e.to);
                }
            }
        }
        return vis[t];
    }

    int DFS(int x,int a){
        if (x==t || a==0) return a;
        int flow=0,f;
        for (int& i=cur[x];i<(int)G[x].size();i++){
            Edge& e=edges[G[x][i]];
            if (d[x]+1==d[e.to] && (f=DFS(e.to,min(a,e.cap-e.flow)))>0){
                e.flow+=f;
                edges[G[x][i]^1].flow-=f;
                flow+=f;
                a-=f;
                if (a==0) break;
            }
        }
        return flow;
    }

    int Maxflow(int s,int t){
        this->s=s;this->t=t;
        int flow=0;
        while (BFS()){
            memset(cur,0,sizeof(cur));
            flow+=DFS(s,INF);
        }
        return flow;
    }
}A;
int dp[maxm],a[maxm];
int n,maxlen;
void solve1(){
    A.init(n*2+2);
    for (int i=1;i<=n;i++){
        A.AddEdge(i,i+n,1);
        if (dp[i]==maxlen) A.AddEdge(0,i,1);
        if (dp[i]==1) A.AddEdge(i+n,n*2+1,1);
    }
    for (int i=1;i<=n;i++){
        for (int j=i+1;j<=n;j++){
            if (a[j]>=a[i] && dp[j]==dp[i]+1) A.AddEdge(j+n,i,1);
        }
    }
    int tmp=A.Maxflow(0,n*2+1);
    if (maxlen==1) printf("%d\n%d\n",tmp,tmp);
    else printf("%d\n",tmp);
}
void solve2(){
    A.init(n*2+2);
    for (int i=1;i<=n;i++){
        int v=1;
        if (i==1 || i==n) v=INF;
        if (dp[i]==maxlen) A.AddEdge(0,i,v);
        if (dp[i]==1) A.AddEdge(i+n,n*2+1,v);
        A.AddEdge(i,i+n,v);
    }
    for (int i=1;i<=n;i++){
        for (int j=i+1;j<=n;j++){
            if (a[j]>=a[i] && dp[j]==dp[i]+1) A.AddEdge(j+n,i,1);
        }
    }
    printf("%d\n",A.Maxflow(0,n*2+1));
}
int main(){
    read(n);
    for (int i=1;i<=n;i++) read(a[i]);
    maxlen=0;
    for (int i=1;i<=n;i++){
        dp[i]=0;
        for (int j=i-1;j>=1;j--){
            if (a[j]<=a[i] && dp[j]>dp[i]) dp[i]=dp[j];
        }
        dp[i]++;
        maxlen=max(maxlen,dp[i]);
    }
    printf("%d\n",maxlen);
    solve1();
    if (maxlen!=1) solve2();
    return 0;
}

 

 

发表评论

电子邮件地址不会被公开。 必填项已用*标注