QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#571949#9315. Rainbow Bracket SequenceAsritWA 2ms8168kbC++142.8kb2024-09-18 10:34:072024-09-18 10:34:08

Judging History

你现在查看的是最新测评结果

  • [2024-09-18 10:34:08]
  • 评测
  • 测评结果:WA
  • 用时:2ms
  • 内存:8168kb
  • [2024-09-18 10:34:07]
  • 提交

answer

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define rop(i,a,b) for(int i=(a);i<(b);i++)
#define per(i,a,b) for(int i=(a);i>=(b);i--)
#define por(i,a,b) for(int i=(a);i>(b);i--)
#define inn(x) (x<<1)
#define ouu(x) (x<<1|1)
using namespace std;
const int N=5e4+10,inf=1e9;
int TT,n,m,S,T,suml;
int l[N],c[N];
long long v[N];
struct node{
    int nx,to,c;
    long long w;
}e[N<<1];
int h[N],idx;

void add(int x,int y,int z,int w){
    e[idx].nx=h[x],e[idx].to=y,e[idx].c=z,e[idx].w=w,h[x]=idx++;
    e[idx].nx=h[y],e[idx].to=x,e[idx].c=0,e[idx].w=-w,h[y]=idx++;
}

int getid(int p,int x){
    return p*2*n+x;
}

void init(){
    memset(h,-1,sizeof(h));
    suml=idx=0;
}

long long dis[N];
int incf[N],vis[N],pre[N];
queue<int> q;

bool SPFA(){
	memset(incf,0,sizeof(incf));
	memset(dis,0x3f,sizeof(dis));
	q.push(S),incf[S]=inf,dis[S]=0;
	while(!q.empty()){
		int x=q.front();q.pop();
		vis[x]=0;
		for(int i=h[x];~i;i=e[i].nx){
			int y=e[i].to;
			if(e[i].c&&dis[y]>dis[x]+e[i].w){
				dis[y]=dis[x]+e[i].w;
				incf[y]=min(e[i].c,incf[x]);
				pre[y]=i;
				if(!vis[y]){
					vis[y]=1;
					q.push(y);
				}
			}
		}
	}
	return incf[T]>0;
}

long long EK(){
	long long cost=0,flow=0;
	while(SPFA()){
		long long f=incf[T];
        flow+=f;
		cost+=f*dis[T];
		for(int i=T;i!=S;i=e[pre[i]^1].to)
			e[pre[i]].c-=f,e[pre[i]^1].c+=f;
	}
    if(flow!=n) return -1;
	return -cost;
}

int main(){
    scanf("%d",&TT);
    while(TT--){
        S=0,T=N-3;
        scanf("%d%d",&n,&m);
        init();
        rep(i,1,m) scanf("%d",&l[i]),suml+=l[i];
        rep(i,1,2*n) scanf("%d",&c[i]);
        rep(i,1,2*n) scanf("%lld",&v[i]);
        if(suml>n){
            puts("-1");
            continue;
        }
        rep(x,1,2*n){
            add((n+2)*4*n+c[x],inn(getid(n+1,x)),1,0);
            add((n+2)*4*n+m+1,inn(getid(n+1,x)),1,0);
            add(inn(getid(n+1,x)),ouu(getid(n+1,x)),1,0);
            rep(p,0,n){
                if(p>x) break;
                if(p>(2*n-x)) break;
                if((p&1)^(x&1)) continue;
                add(inn(getid(p,x)),ouu(getid(p,x)),1,0);
                if(!p) add(ouu(getid(p,x)),T,1,0);
                else{
                    add(ouu(getid(p,x)),ouu(getid(p-1,x+1)),1,0);
                    add(ouu(getid(n+1,x)),inn(getid(p,x)),1,-v[x]);
                }
            }
        }
        rep(i,1,m) add(S,(n+2)*4*n+i,l[i],0);
        add(S,(n+2)*4*n+m+1,n-suml,0);
        printf("%lld\n",EK());
    }
    return 0;
}
/*
1
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
*/

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 2ms
memory: 5928kb

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:

4906452153
343766215
2351080746
3426965219
2593135825
-1
1351561190
2539318868
1013080942
4656646546
1158637046
-1
2231197660
2719131728
3983627641
5287385592
-1
1046749330
6115214757
3920988203
-1
3902088946
4035887375
2566553992
5466071623
5977120748
7505501534
-1
5054275471
1467678317
883992368
1...

result:

wrong answer 1st numbers differ - expected: '-1', found: '4906452153'