QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#572281#9315. Rainbow Bracket Sequenceyzj123WA 1ms3896kbC++142.6kb2024-09-18 13:27:132024-09-18 13:27:16

Judging History

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

  • [2024-09-18 13:27:16]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:3896kb
  • [2024-09-18 13:27:13]
  • 提交

answer

#include <bits/stdc++.h>
#define int long long
using namespace std;
int read()
{
	int res = 0, bj = 1;
	char ch = getchar();
	while (ch < '0' || ch > '9')
	{
		if (ch == '-') bj = -1;
		ch = getchar();
	}
	while (ch >= '0' && ch <= '9')
	{
		res = res * 10 + ch - '0';
		ch = getchar();
	}
	return res * bj;
} 
const int MAXN = 105, inf = 1e10;
int s, t, tot;
int n, m;
int lim[MAXN], col[MAXN];
int p1[MAXN << 1], p2[MAXN << 1]; 
int a[MAXN], val[MAXN], chk[MAXN];
int maxf, maxc;
struct Edge {
	int nxt, to, w, c;
}e[1000005];
int cnt = 1, h[MAXN * 10];
void addedge(int x, int y, int w, int c)
{
	e[++cnt] = (Edge){h[x], y, w, c};
	h[x] = cnt;
	e[++cnt] = (Edge){h[y], x, 0, -c};
	h[y] = cnt;
}
int dis[MAXN << 3], vis[MAXN << 3];
int pre[MAXN << 3];
void clear()
{
	for (int i = 1; i <= tot; i++) h[i] = 0;
	cnt = 1;
}
int spfa()
{
	for (int i = 1; i <= tot; i++) dis[i] = -inf, vis[i] = 0;
	queue<int> q;
	q.push(s);
	vis[s] = 1;
	dis[s] = 0;
	while (q.size())
	{
		int u = q.front();
//		cout << u << "\n";
		q.pop();
		vis[u] = 0;
		for (int i = h[u]; i; i = e[i].nxt)
		{
			int v = e[i].to;
			if (e[i].w != 0 && dis[v] < dis[u] + e[i].c)
			{
				dis[v] = dis[u] + e[i].c;
				pre[v] = i;
				if (!vis[v]) vis[v] = 1, q.push(v);
			}
		}
	}
	if (dis[t] != -inf) return 1;
	else return 0;
}
void EK()
{
	while (spfa())
	{
		int minx = inf;
		for (int i = t; i != s; i = e[pre[i] ^ 1].to)
		{
//			cout << "i:" << i << "\n";
//			system("pause");
			minx = min(minx, e[pre[i]].w);
		}
//		cout << minx << "\n";
//		system("pause");
		for (int i = t; i != s; i = e[pre[i] ^ 1].to)
		{
			e[pre[i]].w -= minx;
			e[pre[i] ^ 1].w += minx;
		}
		maxf += minx;
		maxc += minx * dis[t];
	}
}
void solve()
{
	int suml = 0;
	s = 1, t = 2, tot = 2;
	n = read(), m = read();
	for (int i = 1; i <= m; i++) 
	{
		lim[i] = read(), col[i] = ++tot;
		suml += lim[i];
		addedge(s, col[i], lim[i], 0);
	}
	chk[0] = 0; 
	for (int i = 1; i <= 2 * n; i++) 
	{
		p1[i] = ++tot;
		p2[i] = ++tot;
		a[i] = read();
		addedge(col[a[i]], p1[i], 1, 0);
		addedge(s, p1[i], 1, -inf);
		if (i > 1) addedge(p2[i - 1], p2[i], inf, 0);
		if (i & 1) 
		{
			chk[++chk[0]] = ++tot;
			addedge(p2[i], chk[chk[0]], 1, 0);
			addedge(chk[chk[0]], t, 1, 0);
		}
	}
	for (int i = 1; i <= 2 * n; i++) 
	{
		val[i] = read();
		addedge(p1[i], p2[i], 1, val[i]);
	}
	maxf = maxc = 0;
	EK(); 
	int ans = maxc + (n - suml) * inf;
	if (ans < 0) cout << -1 << "\n";
	else cout << ans << "\n";
	clear();
}
signed main()
{
	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: 3892kb

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: 3896kb

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:

4458752148
343766215
11649648670
31812906175
2145509671
1230630334
10831452391
2539318868
1013080942
41097019708
174677382
13507349949
2231197660
40758225925
60000000000
32812012416
1773576034
10841935645
15689642167
41345740508
22539009666
31988904333
32075963702
30999388067
5268471900
90000000000
...

result:

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