QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#569682 | #9315. Rainbow Bracket Sequence | cdd | WA | 0ms | 4044kb | C++20 | 3.5kb | 2024-09-17 04:41:56 | 2024-09-17 04:41:56 |
Judging History
answer
#include<bits/stdc++.h>
#define LL long long
using namespace std;
int read() {
int x = 0, w = 1;
char ch = 0;
while (ch < '0' || ch > '9') {
if (ch == '-') w = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = x * 10 + (ch ^ 48);
ch = getchar();
}
return x * w;
}
struct dinic {
const LL inf = 1e18;
struct edge {
LL v, nxt, cap, flow, cost;
};
vector<edge> e;
int n, S, T;
LL maxflow, mncost;
vector<int> head, cur, vis;
vector<LL> dis;
dinic(int x) {init(x);}
void init(int x) {
n = x;
S = 0, T = n + 1, maxflow = 0;
e.clear();
head.assign(n + 5, -1);
cur.assign(n + 5, -1);
vis.assign(n + 5, 0);
dis.assign(n + 5, inf);
}
void add_edge(int u, int v, LL w, LL cost) {
e.push_back({v, head[u], w, 0, cost});
head[u] = e.size() - 1;
e.push_back({u, head[v], 0, 0, -cost});
head[v] = e.size() - 1;
}
int spfa() {
dis.assign(n + 5, inf);
vis.assign(n + 5, 0);
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; i = e[i].nxt) {
int v = e[i].v;
if (e[i].cap > e[i].flow && dis[v] > dis[u] + e[i].cost) {
dis[v] = dis[u] + e[i].cost;
if (!vis[v]) q.push(v), vis[v] = 1;
}
}
}
return dis[T] != inf;
}
LL dfs(int u, LL flow) {
if (u == T || !flow) return flow;
vis[u] = 1;
LL ret = 0;
for (int i = cur[u]; ~i; i = e[i].nxt) {
cur[u] = i;
int v = e[i].v;
LL d = 0;
if (!vis[v] && dis[v] == dis[u] + e[i].cost && (d = dfs(v, min(flow - ret, e[i].cap - e[i].flow)))) {
ret += d;
e[i].flow += d;
e[i ^ 1].flow -= d;
if (ret == flow) return ret;
}
}
vis[u] = 0;
return ret;
}
void gets_maxflow() {
maxflow = mncost = 0;
int x;
while (spfa()) {
cur = head;
vis.assign(n + 5, 0);
while(x = dfs(S, inf)) {
maxflow += x;
mncost += x * dis[T];
}
}
}
};
const int maxn = 1e2 + 5;
const int inf = 1e9;
int T;
int n, m;
int main()
{
T = read();
while (T--) {
n = read(); m = read();
int N = 2 * n;
LL tot = 0;
vector<int> l(m + 5);
vector<int> c(N + 5), v(N + 5);
vector<int> cnt(m + 5, 0);
for (int i = 1; i <= m; i++) l[i] = read();
for (int i = 1; i <= N; i++) c[i] = read(), cnt[c[i]]++;
for (int i = 1; i <= N; i++) v[i] = read(), tot += v[i];
// 1 - 2n 括号, 2n + 1 ~ 2n + m 颜色
dinic G(N + m);
for (int i = 1; i <= m; i++) G.add_edge(N + i, G.T, cnt[i] - l[i], 0);
for (int i = 1; i <= N; i++) G.add_edge(i, N + c[i], 1, v[i]);
for (int i = 1; i < N; i++) G.add_edge(i + 1, i, i / 2, 0);
G.add_edge(G.S, N, n, 0);
G.gets_maxflow();
if (G.maxflow < n) {
printf("-1\n");
continue;
}
printf("%lld\n", tot - G.mncost);
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3756kb
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: 0ms
memory: 4044kb
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 1497027962 1351561190 2539318868 1013080942 4656646546 -1 -1 2231197660 2719131728 3983627641 4712174168 3121174891 1046749330 6115214757 3920988203 3914858167 3902088946 -1 2566553992 5268471900 5977120748 7505501534 -1 5054275471 1467678317 883992368 104456298...
result:
wrong answer 6th numbers differ - expected: '-1', found: '1497027962'