QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#585748#9315. Rainbow Bracket SequenceeastcloudWA 1ms7904kbC++203.3kb2024-09-23 22:03:022024-09-23 22:03:03

Judging History

This is the latest submission verdict.

  • [2024-09-23 22:03:03]
  • Judged
  • Verdict: WA
  • Time: 1ms
  • Memory: 7904kb
  • [2024-09-23 22:03:02]
  • Submitted

answer


#include<bits/stdc++.h>

#define ll long long
#define pi pair<int,int>
#define vi vector<int>
#define cpy(x,y,s) memcpy(x,y,sizeof(x[0])*(s))
#define mset(x,v,s) memset(x,v,sizeof(x[0])*(s))
#define all(x) begin(x),end(x)
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define ary array

#define N 100005
#define inf 0x3f3f3f3f

using namespace std;
int read(){
    int x=0,f=1;char ch=getchar();
    while(ch<'0' || ch>'9')f=(ch=='-'?-1:f),ch=getchar();
    while(ch>='0' && ch<='9')x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
    return x*f;
}
void write(int x){
    if(x<0)x=-x,putchar('-');
    if(x/10)write(x/10);
    putchar(x%10+'0');
}

int lim[N],col[N],val[N];

struct flow{
    int e=1,to[N],nex[N],head[N],edg[N],c[N];
    int deg[N],s,t,S,T,tot,fincos;
    int dis[N],vis[N],cur[N],ch[N];
    queue<int> q;
    void add(int u,int v,int L,int R,int cost){
        cost=-cost;
        to[++e]=v;nex[e]=head[u];head[u]=e;edg[e]=R-L;c[e]=cost;
        to[++e]=u;nex[e]=head[v];head[v]=e;edg[e]=0;c[e]=-cost;
        deg[v]+=L;deg[u]-=L;fincos+=L*cost;
    }
    void Add(int u,int v,int w){
        to[++e]=v;nex[e]=head[u];head[u]=e;edg[e]=w;
        to[++e]=u;nex[e]=head[v];head[v]=e;edg[e]=0;
    }
    void clear(){
        for(int i=1;i<=e;i++)to[i]=nex[i]=edg[i]=c[i]=deg[i]=0;
        for(int i=0;i<=tot;i++)head[i]=0;
        e=1;fincos=0;
    }
    int dfs(int x,int ed,int res){
        if(x==ed)return res;int ans=0;ch[x]++;
        for(int i=cur[x];i && res;i=nex[i]){
            cur[x]=i;int v=to[i],w=edg[i];
            if(!w || dis[v]!=dis[x]+c[i] || ch[v])continue;
            int k=dfs(v,ed,min(res,w));
            edg[i]-=k;edg[i^1]+=k;res-=k;ans+=k;fincos+=c[i]*k;
        }
        ch[x]--;
        return ans;
    }
    int spfa(int st,int ed){
        for(int i=0;i<=tot;i++)dis[i]=inf,cur[i]=head[i];
        dis[st]=0;q.push(st);vis[st]=1;
        while(q.size()){
            int u=q.front();q.pop();vis[u]=0;
            for(int i=head[u];i;i=nex[i]){
                int v=to[i],w=edg[i];
                if(!w || dis[v]<=dis[u]+c[i])continue;
                dis[v]=dis[u]+c[i];
                if(!vis[v])q.push(v),vis[v]=1;
            }
        }
        return dis[ed]!=inf;
    }
    int solve(){
        S=t+1;T=t+2;tot+=2;add(t,s,0,inf,0);
        vi chk;int ned=0;
        for(int i=0;i<=t;i++){
            if(deg[i]<0)Add(i,T,-deg[i]),chk.pb(e-1),ned+=edg[e-1];
            else if(deg[i]>0)Add(S,i,deg[i]),chk.pb(e-1);
        }
        int maxflow=0;
        while(spfa(S,T))maxflow+=dfs(S,T,inf);
        for(auto v:chk)if(edg[v]!=0)return -1;
        while(spfa(s,t))dfs(s,t,inf);
        return -fincos;
    }
}F;

void solve(){
    int n=read(),m=read();F.clear();
    for(int i=1;i<=m;i++)lim[i]=read();
    for(int i=1;i<=2*n;i++)col[i]=read();
    for(int i=1;i<=2*n;i++)val[i]=read();
    int S=0,T=2*n+m+1;
    F.tot=T;F.s=S;F.t=T;
    for(int i=1;i<=m;i++)F.add(S,i,lim[i],n,0);
    for(int i=1;i<=2*n;i++)F.add(col[i],m+i,0,1,val[i]);
    for(int i=1;i<2*n;i++)F.add(i+m,i+1+m,(i+1)/2,n,0);
    F.add(2*n+m,T,n,n,0);
    write(F.solve());putchar('\n');
}

int main(){
    #ifdef EAST_CLOUD
    freopen("a.in","r",stdin);
    //freopen("a.out","w",stdout);
    #endif

    int T=read();while(T--)solve();
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 7904kb

input:

2
3 2
1 2
1 2 2 2 1 2
3 1 4 2 2 1
3 2
2 2
1 2 2 2 1 2
3 1 4 2 2 1

output:

9
-1

result:

ok 2 number(s): "9 -1"

Test #2:

score: -100
Wrong Answer
time: 0ms
memory: 7676kb

input:

50
8 6
0 2 2 0 3 1
3 5 1 1 5 3 5 2 3 2 2 1 2 2 4 6
998133227 879226371 59632864 356493387 62611196 827258251 296576565 204244054 812713672 780267148 614679390 447700005 102067050 544546349 116002772 761999375
1 1
1
1 1
343766215 374461155
3 1
2
1 1 1 1 1 1
796323508 303640854 701432076 853325162 610...

output:

-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1

result:

wrong answer 2nd numbers differ - expected: '343766215', found: '-1'