QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#590366#9315. Rainbow Bracket SequenceMastey_WA 1ms3868kbC++142.6kb2024-09-25 23:35:592024-09-25 23:36:00

Judging History

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

  • [2024-09-25 23:36:00]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:3868kb
  • [2024-09-25 23:35:59]
  • 提交

answer

#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<cstdlib>
#include<iomanip>
#include<queue>
#include<vector>
#include<bitset>
#include<set>
#include<stack>
using namespace std;
typedef long long ll;
typedef pair<int,ll> pil;
const int maxn=505;
const int maxm=2e3+5;
const int inf=2e9;
inline int read()
{
	int x=0,y=1; char c=getchar();
	while(c<'0'||c>'9') {if(c=='-') y=-1; c=getchar();}
	while(c>='0'&&c<='9') x=x*10+c-'0',c=getchar();
	return x*y;
}
int n,m;
struct edge
{
	int to,next,c,u;
	ll w;
}g[maxm<<1];
int head[maxn],cnt(0);
inline void add(int a,int b,int c,ll w)
{
	g[cnt].to=b; g[cnt].u=a;
	g[cnt].c=c; g[cnt].w=w;
	g[cnt].next=head[a];
	head[a]=cnt++;
	return ;
}
void Add(int a,int b,int c,ll w)
{add(a,b,c,w); add(b,a,0,-w);}
ll dis[maxn];
int vis[maxn],pre[maxn];
bool spfa(int s,int t)
{
	memset(dis,-1,sizeof(dis));
	memset(pre,-1,sizeof(pre));
	memset(vis,0,sizeof(vis));
	queue<int> q;
	q.push(s); dis[s]=0; vis[s]=1;
	while(!q.empty())
	{
		int u=q.front(); q.pop(); vis[u]=0;
		for(int i=head[u];i!=-1;i=g[i].next)
		{
			int v=g[i].to;
			if(!g[i].c) continue;
			if(dis[v]<dis[u]+g[i].w)
			{
				dis[v]=dis[u]+g[i].w;
				pre[v]=i;
				if(!vis[v]) q.push(v),vis[v]=1;
			}
		}
	}
	return (pre[t]!=-1);
}
pil EK(int s,int t)
{
	ll ans(0); int res=0;
	while(spfa(s,t))
	{
		int mn=inf;
		for(int i=t;i!=s;i=g[pre[i]].u)
			mn=min(mn,g[pre[i]].c);
		for(int i=t;i!=s;i=g[pre[i]].u)
		{
			g[pre[i]].c-=mn;
			g[pre[i]^1].c+=mn;
			ans+=mn*g[pre[i]].w;
		}
		res+=mn;
	}
	return make_pair(res,ans);
}
int lim[maxn],tot(0);
int S,T,col[maxn];
ll val[maxn];
int deg[maxn];
int Tm=0;
void solve()
{
	Tm++;
	int sum=0;
	memset(head,-1,sizeof(head)); cnt=0;
	memset(deg,0,sizeof(deg));
	n=read(); m=read();
	S=2*n+m+2; T=2*n+m+1;
	for(int i=1;i<=m;i++)
	{
		lim[i]=read(); Add(S,i,inf,0);
		deg[S]+=lim[i]; deg[i]-=lim[i];
	}
	for(int i=1;i<=n*2;i++) col[i]=read();
	for(int i=1;i<=n*2;i++)
	{
		val[i]=read();
		Add(col[i],m+i,1,val[i]);
	}
	for(int i=1;i<=n*2;i++)
	{
		int tmp=ceil(i*1.0/2);
		Add(m+i,m+i+1,min(i,n)-tmp,0);
		deg[m+i]+=tmp; deg[m+i+1]-=tmp;
	}
	int ss=2*n+m+3,tt=2*n+m+4;
	for(int i=1;i<=n*2+m+2;i++)
	{
		if(deg[i]<0) Add(ss,i,-deg[i],0);
		else if(deg[i]>0) Add(i,tt,deg[i],0),sum+=deg[i];
	}
	Add(T,S,inf,0);
	pil tmp=EK(ss,tt);
	if(tmp.first!=sum) {printf("-1\n"); return ;}
	printf("%lld\n",EK(S,T).second+tmp.second);
	return ;
}
int main()
{
	//freopen("test.in","w",stdout);
	int T=read();
	while(T--) solve();
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 1ms
memory: 3728kb

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
343766215
2351080746
3426965219
-1
-1
1351561190
2539318868
1013080942
-1
-1
-1
2231197660
2719131728
3983627641
-1
-1
1046749330
6115214757
-1
-1
3902088946
-1
2566553992
-1
5977120748
7505501534
-1
5054275471
1467678317
883992368
-1
-1
4024634503
-1
1447294256
6757607722
-1
-1
-1
-1
-1
43914006...

result:

wrong answer 10th numbers differ - expected: '4656646546', found: '-1'