求助:最长下降子序列那题,(最小的那个数据)本地AC,提交RE

P2766 最长不下降子序列问题

Algha_Porthos @ 2019-10-02 20:14:39

#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,m,s,t;
int cnt,maxflow,flow;
int head[105005],d[105005];
int a[105005],len,dp[105005];
queue <int> q;
struct A{
    int to,nxt,val;
}ed[525005];
void double_add(int x,int y,int w){
    ed[++cnt].to=y;
    ed[cnt].val=w;
    ed[cnt].nxt=head[x];
    head[x]=cnt;

    ed[++cnt].to=x;
    ed[cnt].val=0;
    ed[cnt].nxt=head[y];
    head[y]=cnt;
}
bool bfs(){
    memset(d,0,sizeof(d));
    while(!q.empty())
        q.pop();
    q.push(s);
    d[s]=1;
    while(!q.empty()){
        int x=q.front();
        q.pop();
        for(int i=head[x];i;i=ed[i].nxt){
            if(ed[i].val&&!d[ed[i].to]){
                q.push(ed[i].to);
                d[ed[i].to]=d[x]+1;
                if(ed[i].to==t)
                    return 1;
            }
        }
    }
    return 0;
}
int dinic(int x,int flow){
    if(x==t)
        return flow;
    int rest=flow,k;
    for(int i=head[x];i&&rest;i=ed[i].nxt){
        int y=ed[i].to;
        if(ed[i].val&&d[y]==d[x]+1){
            k=dinic(y,min(rest,ed[i].val));
            ed[i].val-=k;
            ed[i^1].val+=k;
            rest-=k;
        }
    }
    return flow-rest;
}//
signed main(){
    cin>>n;
    for(int i=1;i<=n;++i)
        scanf("%lld",&a[i]),dp[i]=1;
    for(int i=1;i<=n;++i)
        for(int j=1;j<=i-1;++j)
            if(a[j]<=a[i])
                dp[i]=max(dp[i],dp[j]+1);
    for(int i=1;i<=n;++i)
        len=max(len,dp[i]); 
    printf("%lld\n",len);

    s=0;t=5000;
    memset(head,-1,sizeof(head));
    for(int i=1;i<=n;++i)
        if(dp[i]==1)
            double_add(s,i,1);
    for(int i=1;i<=n;++i)
        if(dp[i]==len)
            double_add(i+n,t,1);
    for(int i=1;i<=n;++i)
        double_add(i,i+n,1);
    for(int i=1;i<=n;++i){
        for(int j=1;j<i;++j)
            if(a[j]<=a[i]&&dp[j]+1==dp[i])
                double_add(j+n,i,1);
    }
    while(bfs()){ 
        flow=1;
        while(flow){
            flow=dinic(s,0x3f3f3f3f);
            maxflow+=flow;
        }
    }// 
    cout<<maxflow<<endl;

//  printf("%d\n",ans);
    double_add(1,1+n,0x3f3f3f3f),double_add(s,1,0x3f3f3f3f);
    if(dp[n]==len){
        double_add(n,n*2,0x3f3f3f3f);
        double_add(n*2,t,0x3f3f3f3f);
    }
    while(bfs()){
        flow=1;
        while(flow){
            flow=dinic(s,0x3f3f3f3f);
            maxflow+=flow;
        }
    }// 
    cout<<maxflow<<endl;

    while(bfs()){
        flow=1;
        while(flow){
            flow=dinic(s,0x3f3f3f3f);
            maxflow+=flow;
        }
    }// 
    cout<<maxflow<<endl;

}// 

by Provicy @ 2019-10-02 20:16:55

%%%巨佬您会写这题


by Algha_Porthos @ 2019-10-02 20:17:59

@DXL ???我re了


by Provicy @ 2019-10-02 20:19:00

估计是内存访问有问题(瞎猜)


by Ynoi @ 2019-10-02 20:24:20

@自动AK机 main没有return 0

这样有些时候会出问题


by Algha_Porthos @ 2019-10-02 20:24:51

OKOK


by Algha_Porthos @ 2019-10-02 20:25:37

@Ynoi 还是有锅


by ylxmf2005 @ 2019-10-02 21:01:36

一开始我还以为你在玩梗qwq


by Algha_Porthos @ 2019-10-03 08:06:34

@_ikun emmmmm


|