QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#576451#9315. Rainbow Bracket SequenceRillloWA 2ms8324kbC++233.4kb2024-09-19 20:24:322024-09-19 20:24:35

Judging History

This is the latest submission verdict.

  • [2024-09-19 20:24:35]
  • Judged
  • Verdict: WA
  • Time: 2ms
  • Memory: 8324kb
  • [2024-09-19 20:24:32]
  • Submitted

answer

#include<bits/stdc++.h>

using i64 = long long;
const int V = 1000;
const int E = 100000;
template<class T>
struct MCF {
	int s, t, n, m;
	T flow, cost, dis[V];
	int head[V], cur[V], pre[V];
	bool vis[V];
	struct edge {
		int y, nxt;
		T f, c;
	} e[E * 2];
	
	void init(int _s, int _t, int _n) {
		m = 0;
		n = _n;
		s = _s;
		t = _t;
		for (int i = 0; i < n; i++) {
			head[i] = -1;
		}
	}
	void addedge(int x, int y, T f1, T c, T f2 = 0) {
		e[m] = {y, head[x], f1, c}; head[x] = m++;
		e[m] = {x, head[y], f2, -c}; head[y] = m++;
	}

	bool spfa() {
		T inf = std::numeric_limits<T>::max() / 2;
		for (int i = 0; i < n; i++) {
			dis[i] = inf;
			vis[i] = false;
			pre[i] = -1;
		}
		dis[s] = 0;
		vis[s] = true;
		std::queue<int> q;
		q.push(s);
		while (!q.empty()) {
			int x = q.front();
			for (int i = head[x]; ~i; i = e[i].nxt) {
				int y = e[i].y;
				if (e[i].f && dis[y] > dis[x] + e[i].c) {
					dis[y] = dis[x] + e[i].c;
					pre[y] = i;
					if (!vis[y]) {
						vis[y] = 1;
						q.push(y);
					}
				}
			}
			q.pop();
			vis[x] = false;
		}
        // for (int i = 0; i < n; i++) {
        //     std::cout << dis[i] << " \n"[i == n - 1];
        // }
		return dis[t] != inf;
		
	}
	void augment() {
		int x = t;
		T f = std::numeric_limits<T>::max();
        
		while (~pre[x]) {
            // std::cout << x << " ";
            // print(x);
			f = std::min(f, e[pre[x]].f);
			x = e[pre[x] ^ 1].y;
		}
        // std::cout << x<< "\n";
        // print(x);
        // std::cout << "\n";
        
		flow += f;
		cost += f * dis[t];
        // std::cout << "flow : " << flow << " " << "cost : " << cost << "\n";
		x = t;
		while (~pre[x]) {
			e[pre[x]].f -= f;
			e[pre[x] ^ 1].f += f;
			x = e[pre[x] ^ 1].y;
		}
	}
	std::pair<T, T> work() {
		flow = 0;
		cost = 0;
		while (spfa()) augment();
        
        // for (int i)
		return {flow, cost};
	}
};


void solve() {
    int n, m;
    std::cin >> n >> m;
    std::vector<int> l(m), c(2 * n), v(2 * n);
    for (int i = 0; i < m; i++) {
        std::cin >> l[i];
    }
    for (int i = 0; i < 2 * n; i++) {
        std::cin >> c[i];
        c[i]--;
    }
    for (int i = 0; i < 2 * n; i++) {
        std::cin >> v[i];
    }
    
    i64 sum = 0;
    int s = m + 2 * n;
    int ss = s + 1, st = s + 2;
    MCF<i64> flow;
    flow.init(ss, st, st + 1);

    for (int i = 0; i < m; i++) {
        flow.addedge(s, st, l[i], 0);
        flow.addedge(ss, i, l[i], 0);
        sum += l[i];
    }
    for (int i = 0; i < 2 * n; i++) {
        flow.addedge(c[i], i + m, 1, -v[i]);
        if (i + 1 != 2 * n) {
            int lim = i / 2 + 1;
            flow.addedge(ss, i + 1 + m, lim, 0);
            sum += lim;
            flow.addedge(i + m, st, lim, 0);
            flow.addedge(i + m, i + 1 + m, i + 1 - lim, 0);
        }
    }

    sum += n;
    flow.addedge(ss, s, n, 0);
    flow.addedge(s - 1, st, n, 0);

    auto [f, cost] = flow.work();
    // std::cout << f << " " << -cost << "\n";
    if (f != sum) {
        std::cout << -1 << "\n";
    } else {
        std::cout << -cost << "\n";
    }  

    
}
int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    std::cout << std::fixed << std::setprecision(10);

    int t;
    std::cin >> t;
    while (t--) {
        solve();
    }

    return 0;
}

详细

Test #1:

score: 100
Accepted
time: 2ms
memory: 8296kb

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

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
-1
-1
-1
-1
-1
2539318868
1013080942
-1
-1
-1
2231197660
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
5268471900
-1
-1
-1
-1
-1
883992368
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
1309966981
-1
-1
-1
1373323696

result:

wrong answer 3rd numbers differ - expected: '2351080746', found: '-1'