QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#565098#9315. Rainbow Bracket Sequenceforget-starTL 0ms22168kbC++202.0kb2024-09-15 20:14:182024-09-15 20:14:19

Judging History

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

  • [2024-09-15 20:14:19]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:22168kb
  • [2024-09-15 20:14:18]
  • 提交

answer

#include<bits/stdc++.h>
#define int long long
#define fr(i,a,b) for(int i(a),end##i(b);i<=end##i;i++)
#define fd(i,a,b) for(int i(a),end##i(b);i>=end##i;i--)
using namespace std;
const int maxn=1e6+5,inf=1e17;
int nex[maxn],head[maxn],to[maxn],w[maxn],cost[maxn];

int n,s,t;
int e=1;
void add(int x,int y,int z,int c){
	nex[++e]=head[x];
	head[x]=e;
	to[e]=y;w[e]=z;cost[e]=c;
}
void insert(int x,int y,int z,int c){
	add(x,y,z,c);
	add(y,x,0,-c);
}


int L[maxn];
vector<int> C[2000+5];
int V[maxn];
int m;
bool pd[maxn];
int d[maxn],q[maxn],flow[maxn],pre[maxn];
bool spfa(){
	int f=1,l=0;
	fr(i,1,n)d[i]=inf,pd[i]=0;
	d[s]=0;pd[s]=1;flow[s]=inf;
	q[++l]=s;
	while(f<=l){
		int x=q[f++];
		for(int i=head[x];i;i=nex[i])if(w[i]>0&&d[to[i]]>d[x]+cost[i]){
			d[to[i]]=d[x]+cost[i];
			pre[to[i]]=i;
			flow[to[i]]=min(flow[x],w[i]);
			if(!pd[to[i]])q[++l]=to[i],pd[to[i]]=1;
		}
		pd[x]=0;
	}
	if(d[t]>inf/2)return 0;
	return 1;
}
int sf=0,sc=0;
void up(){
	int x=t;
	while(x){
		w[pre[x]]-=flow[t];
		w[pre[x]^1]+=flow[t];
		x=to[pre[x]^1];
	}
	sf+=flow[t];
	sc+=flow[t]*d[t];
}
void solve(){
	int n;
	e=1;sf=sc=0;
	scanf("%lld%lld",&n,&m);
	s=2*n+m+1;t=2*n+m+3;int T=t-1;
	::n=t;
	fr(i,1,t)head[i]=0;
	fr(i,1,m)C[i].clear();

	fr(i,1,m)scanf("%lld",&L[i]);

	fr(i,1,2*n){
		int x;scanf("%lld",&x);
		C[x].push_back(2*n-i+1);
	}

	int sum=0;
	fr(i,1,2*n){
		scanf("%lld",&V[i]);
		sum+=V[i];
	}
	reverse(V+1,V+2*n+1);

	fr(i,1,m){  
		int u=((int)C[i].size());
		if(u-L[i]<0){
			cout<<-1<<'\n';
			return ;
		}
		insert(s,i+2*n,u-L[i],0);
		for(auto j:C[i]){
			insert(i+2*n,j,1,V[j]);
		}
	}

	fr(i,1,2*n-1)insert(i,i+1,t,0);
	fr(i,1,2*n)if((i&1))insert(i,T,1,0);
	insert(T,t,n,0);

	while(spfa()){
		up();
	}
	//cout<<sf<<'\n';
	if(sf!=n)cout<<-1<<'\n';
	else cout<<sum-sc<<'\n';
}
signed main(){
#ifdef pig
	freopen("pig.in","r",stdin);
	freopen("pig.out","w",stdout);
#endif
	int t;cin>>t;
	while(t--)solve();

	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 22168kb

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
Time Limit Exceeded

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:


result: