QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#565123#9315. Rainbow Bracket Sequenceforget-starTL 0ms24344kbC++202.5kb2024-09-15 20:20:012024-09-15 20:20:03

Judging History

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

  • [2024-09-15 20:20:03]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:24344kb
  • [2024-09-15 20:20:01]
  • 提交

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--)
#define fi first
#define se second
#define pii pair<int,int>
#define mk make_pair
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];
int h[maxn];
priority_queue<pii> Q;
bool dij(){
	int f=1,l=0;
	fr(i,1,n)d[i]=inf,pd[i]=0;
	d[s]=0;flow[s]=inf;
	while(!Q.empty())Q.pop();
	Q.push(mk(0,s));
	while(!Q.empty()){
		int x=Q.top().se;Q.pop();
		if(pd[x])continue;
		pd[x]=1;
		#define u to[i]
		for(int i=head[x];i;i=nex[i])if(w[i]>0&&d[u]>d[x]+cost[i]-(h[u]-h[x])){
			d[u]=d[x]+cost[i]-(h[u]-h[x]);
			Q.push(mk(-d[u],u));	
			pre[u]=i;
			flow[u]=min(flow[x],w[i]);
		}
		#undef u
	}
	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]+h[t]-h[s]);
}
void ford(){
	fr(i,1,n)h[i]=inf;
	h[s]=0;
	fr(T,1,n){
		bool flag=0;
		fr(x,1,n)if(h[x]!=inf)for(int j=head[x];j;j=nex[j])if(w[j]>0){
			int y=to[j];
			if(h[y]>h[x]+cost[j]){
				h[y]=h[x]+cost[j];
				flag=1;
			}
		}
		if(!flag)break;
	}
}
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);
	ford();
	while(dij()){
		up();	
		fr(i,1,::n)if(h[i]<inf)h[i]+=d[i];	
	}

	//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: 24344kb

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: