QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#567420 | #9315. Rainbow Bracket Sequence | wym1111 | WA | 1ms | 3748kb | C++17 | 2.9kb | 2024-09-16 11:52:21 | 2024-09-16 11:52:21 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define endl '\n'
const int N = 1e3 + 10;
const ll INF = 0x3f3f3f3f3f3f3f3f;
class Dinic_ssp {
private:
struct Edge {
int from, to;
ll cap, flow, val;
};
vector<Edge> edges;
vector<int> g[N];
ll dis[N], cur[N], mincost;
bool vis[N];
public:
void init (int n) {
for (int i = 1; i <= n; i ++) {
g[i].clear();
}
edges.clear();
mincost = 0;
}
void addEdge (int u, int v, int c, int w) {
edges.push_back({u, v, c, 0, w});
edges.push_back({v, u, 0, 0, -w});
int m = edges.size();
g[u].push_back(m - 2);
g[v].push_back(m - 1);
}
bool spfa (int s, int t) {
memset(dis, 0x3f, sizeof dis);
memset(cur, 0, sizeof cur);
queue<int> q;
dis[s] = 0;
q.push(s);
vis[s] = 1;
while (!q.empty()) {
int x = q.front();
q.pop();
vis[x] = 0;
// cout << x << ' ' << dis[x] << endl;
for (auto &i: g[x]) {
Edge &e = edges[i];
if (e.cap > e.flow && dis[e.to] > dis[x] + e.val) {
dis[e.to] = dis[x] + e.val;
if (!vis[e.to]) {
q.push(e.to);
vis[e.to] = 1;
}
}
}
}
return dis[t] != INF;
}
int dfs (int x, int t, ll flow) {
if (x == t || flow == 0) return flow;
ll res = 0;
vis[x] = 1;
for (int i = cur[x]; i < (int)g[x].size(); i ++) {
cur[x] = i;
Edge &e = edges[g[x][i]];
if (dis[e.to] == dis[x] + e.val && !vis[e.to]) {
ll d = dfs(e.to, t, min(flow - res, e.cap - e.flow));
res += d;
mincost += d * e.val;
edges[g[x][i]].flow += d;
edges[g[x][i] ^ 1].flow -= d;
if (res == flow) {
vis[x] = 0;
return flow;
}
}
}
vis[x] = 0;
return res;
}
pair<ll, ll> mcmf (int s, int t) {
ll flow = 0;
while (spfa(s, t)) {
while (int d = dfs(s, t, INF)) {
flow += d;
}
}
return {flow, mincost};
}
};
Dinic_ssp dinic;
int n, m;
int cnt[N], lim[N], col[N];
ll val[N], sum;
void init () {
cin >> n >> m;
n *= 2;
for (int i = 1; i <= m; i ++) {
cin >> lim[i];
cnt[i] = 0;
}
for (int i = 1; i <= n; i ++) {
cin >> col[i];
cnt[col[i]] ++;
}
for (int i = 1; i <= n; i ++) {
cin >> val[i];
sum += val[i];
}
}
void solve() {
init();
int s = 0, t = n + m + 1;
dinic.init(t);
dinic.addEdge(s, n, n, 0);
for (int i = 1; i <= n; i ++) {
if (i < n) dinic.addEdge(i + 1, i, i / 2, 0);
dinic.addEdge(i, n + col[i], 1, val[i]);
}
for (int i = 1; i <= m; i ++) {
dinic.addEdge(n + i, t, max(0, cnt[i] - lim[i]), 0);
}
auto [flow, cost] = dinic.mcmf(s, t);
// cout << n << ' ' << flow << ' ' << cost << endl;
if (flow != n / 2) {
cout << "-1\n";
return;
}
cout << sum - cost << endl;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
int _ = 1;
cin >> _;
while (_--){
solve();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3584kb
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: 3748kb
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 8207917891 10933459792 15346239366 -1 22700667072 24237090171 27844557462 29652423117 34655728549 -1 -1 50452805386 53964479056 59047186149 66091268253 72195791827 75053547493 82337616026 90441323053 96495683277 101795894941 -1 112579234590 118815485324 128534074920 138535088976 -1 157472327774 1...
result:
wrong answer 2nd numbers differ - expected: '343766215', found: '8207917891'