Submission #70984926


Source Code Expand

#include <bits/extc++.h>

using namespace std;

#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

const ll INF = numeric_limits<ll>::max() / 4;

struct MCMF {
	struct edge {
		int from, to, rev;
		ll cap, cost, flow;
	};
	int N;
	vector<vector<edge>> ed;
	vi seen;
	vector<ll> dist, pi;
	vector<edge*> par;

	MCMF(int N) : N(N), ed(N), seen(N), dist(N), pi(N), par(N) {}

	void addEdge(int from, int to, ll cap, ll cost) {
		if (from == to) return;
		ed[from].push_back(edge{ from,to,sz(ed[to]),cap,cost,0 });
		ed[to].push_back(edge{ to,from,sz(ed[from])-1,0,-cost,0 });
	}

	void path(int s) {
		fill(all(seen), 0);
		fill(all(dist), INF);
		dist[s] = 0; ll di;

		__gnu_pbds::priority_queue<pair<ll, int>> q;
		vector<decltype(q)::point_iterator> its(N);
		q.push({ 0, s });

		while (!q.empty()) {
			s = q.top().second; q.pop();
			seen[s] = 1; di = dist[s] + pi[s];
			for (edge& e : ed[s]) if (!seen[e.to]) {
				ll val = di - pi[e.to] + e.cost;
				if (e.cap - e.flow > 0 && val < dist[e.to]) {
					dist[e.to] = val;
					par[e.to] = &e;
					if (its[e.to] == q.end())
						its[e.to] = q.push({ -dist[e.to], e.to });
					else
						q.modify(its[e.to], { -dist[e.to], e.to });
				}
			}
		}
		rep(i,0,N) pi[i] = min(pi[i] + dist[i], INF);
	}

	pair<ll, ll> maxflow(int s, int t) {
		ll totflow = 0, totcost = 0;
		while (path(s), seen[t]) {
			ll fl = INF;
			for (edge* x = par[t]; x; x = par[x->from])
				fl = min(fl, x->cap - x->flow);

			totflow += fl;
			for (edge* x = par[t]; x; x = par[x->from]) {
				x->flow += fl;
				ed[x->to][x->rev].flow -= fl;
			}
		}
		rep(i,0,N) for(edge& e : ed[i]) totcost += e.cost * e.flow;
		return {totflow, totcost/2};
	}

	// If some costs can be negative, call this before maxflow:
	void setpi(int s) { // (otherwise, leave this out)
		fill(all(pi), INF); pi[s] = 0;
		int it = N, ch = 1; ll v;
		while (ch-- && it--)
			rep(i,0,N) if (pi[i] != INF)
			  for (edge& e : ed[i]) if (e.cap)
				  if ((v = pi[i] + e.cost) < pi[e.to])
					  pi[e.to] = v, ch = 1;
		assert(it >= 0); // negative cost cycle
	}
};

#define int long long

const int N = 21;

int dp[(1 << N)+5];
int pr[(1 << N)+5];
int a[N];

signed main()
{
    cin.tie(0);
    ios_base::sync_with_stdio(false);
    ///
    int n;
    cin >> n;
    int sum = 0;
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
        sum += a[i];
    }
    if (sum%n != 0) {
        cout << "-1\n";
        return 0;
    }
    int rp = sum/n;
    MCMF fw(n+2);
    for (int i = 0; i < n; ++i) {
        a[i] -= rp;
        if (a[i] > 0) {
            fw.addEdge(n, i, a[i], 0);
        }
        if (a[i] < 0) {
            fw.addEdge(i, n+1, -a[i], 0);
        }
    }
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) if (a[i] > a[j]) {
            fw.addEdge(i, j, 1e10, 1);
        }
    }
    fw.maxflow(n, n+1);
    vector<array<int, 3>> res;
    for (int i = 0; i < n; ++i) {
        for (auto ed : fw.ed[i]) if (ed.to < n) {
            if (ed.flow > 0) {
                res.push_back({ed.from, ed.to, ed.flow});
            }
        }
    }
    cout << res.size() << '\n';
    int op = 0;
    while (op < res.size()) {
        for (auto& [aa, bb, cc] : res) {
            if (aa == -1) continue;
            if (a[aa]+rp < cc) continue;
            ++op;
            a[aa] -= cc;
            a[bb] += cc;
            cout << aa+1 << ' ' << bb+1 << ' ' << cc << '\n';
            aa = -1;
        }
    }
    
}

Submission Info

Submission Time
Task F - Candy Redistribution
User PabloNo
Language C++23 (GCC 15.2.0)
Score 0
Code Size 3740 Byte
Status WA
Exec Time 1 ms
Memory 3820 KiB

Compile Error

./Main.cpp: In function 'int main()':
./Main.cpp:141:15: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::array<long long int, 3> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  141 |     while (op < res.size()) {
      |            ~~~^~~~~~~~~~~~

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 525
Status
AC × 2
AC × 55
WA × 39
Set Name Test Cases
Sample 00-sample-01.txt, 00-sample-02.txt
All 00-sample-01.txt, 00-sample-02.txt, 01-01.txt, 01-02.txt, 01-03.txt, 01-04.txt, 01-05.txt, 01-06.txt, 01-07.txt, 01-08.txt, 01-09.txt, 01-10.txt, 01-11.txt, 01-12.txt, 01-13.txt, 01-14.txt, 01-15.txt, 01-16.txt, 01-17.txt, 01-18.txt, 01-19.txt, 01-20.txt, 01-21.txt, 01-22.txt, 01-23.txt, 01-24.txt, 01-25.txt, 01-26.txt, 01-27.txt, 01-28.txt, 01-29.txt, 01-30.txt, 01-31.txt, 01-32.txt, 01-33.txt, 01-34.txt, 01-35.txt, 01-36.txt, 01-37.txt, 01-38.txt, 01-39.txt, 01-40.txt, 01-41.txt, 01-42.txt, 01-43.txt, 01-44.txt, 01-45.txt, 01-46.txt, 01-47.txt, 01-48.txt, 01-49.txt, 01-50.txt, 01-51.txt, 01-52.txt, 01-53.txt, 01-54.txt, 01-55.txt, 01-56.txt, 01-57.txt, 01-58.txt, 01-59.txt, 01-60.txt, 01-61.txt, 01-62.txt, 01-63.txt, 01-64.txt, 01-65.txt, 01-66.txt, 01-67.txt, 01-68.txt, 01-69.txt, 01-70.txt, 01-71.txt, 01-72.txt, 01-73.txt, 01-74.txt, 01-75.txt, 01-76.txt, 01-77.txt, 01-78.txt, 01-79.txt, 01-80.txt, 01-81.txt, 01-82.txt, 01-83.txt, 01-84.txt, 01-85.txt, 01-86.txt, 01-87.txt, 01-88.txt, 01-89.txt, 01-90.txt, 01-91.txt, 01-92.txt
Case Name Status Exec Time Memory
00-sample-01.txt AC 1 ms 3548 KiB
00-sample-02.txt AC 1 ms 3580 KiB
01-01.txt AC 1 ms 3596 KiB
01-02.txt AC 1 ms 3604 KiB
01-03.txt AC 1 ms 3596 KiB
01-04.txt AC 1 ms 3596 KiB
01-05.txt AC 1 ms 3604 KiB
01-06.txt AC 1 ms 3532 KiB
01-07.txt AC 1 ms 3580 KiB
01-08.txt AC 1 ms 3456 KiB
01-09.txt AC 1 ms 3532 KiB
01-10.txt AC 1 ms 3548 KiB
01-11.txt AC 1 ms 3588 KiB
01-12.txt AC 1 ms 3668 KiB
01-13.txt AC 1 ms 3676 KiB
01-14.txt AC 1 ms 3704 KiB
01-15.txt AC 1 ms 3656 KiB
01-16.txt AC 1 ms 3644 KiB
01-17.txt AC 1 ms 3808 KiB
01-18.txt AC 1 ms 3704 KiB
01-19.txt AC 1 ms 3620 KiB
01-20.txt AC 1 ms 3620 KiB
01-21.txt WA 1 ms 3704 KiB
01-22.txt WA 1 ms 3784 KiB
01-23.txt AC 1 ms 3592 KiB
01-24.txt WA 1 ms 3724 KiB
01-25.txt WA 1 ms 3772 KiB
01-26.txt WA 1 ms 3732 KiB
01-27.txt WA 1 ms 3660 KiB
01-28.txt WA 1 ms 3648 KiB
01-29.txt WA 1 ms 3704 KiB
01-30.txt WA 1 ms 3644 KiB
01-31.txt WA 1 ms 3676 KiB
01-32.txt WA 1 ms 3704 KiB
01-33.txt AC 1 ms 3732 KiB
01-34.txt WA 1 ms 3768 KiB
01-35.txt WA 1 ms 3672 KiB
01-36.txt WA 1 ms 3772 KiB
01-37.txt WA 1 ms 3696 KiB
01-38.txt WA 1 ms 3808 KiB
01-39.txt AC 1 ms 3724 KiB
01-40.txt WA 1 ms 3724 KiB
01-41.txt WA 1 ms 3624 KiB
01-42.txt AC 1 ms 3676 KiB
01-43.txt AC 1 ms 3676 KiB
01-44.txt WA 1 ms 3772 KiB
01-45.txt WA 1 ms 3656 KiB
01-46.txt WA 1 ms 3704 KiB
01-47.txt WA 1 ms 3768 KiB
01-48.txt WA 1 ms 3808 KiB
01-49.txt WA 1 ms 3772 KiB
01-50.txt AC 1 ms 3456 KiB
01-51.txt AC 1 ms 3596 KiB
01-52.txt WA 1 ms 3704 KiB
01-53.txt WA 1 ms 3724 KiB
01-54.txt WA 1 ms 3772 KiB
01-55.txt WA 1 ms 3732 KiB
01-56.txt WA 1 ms 3724 KiB
01-57.txt WA 1 ms 3732 KiB
01-58.txt WA 1 ms 3748 KiB
01-59.txt WA 1 ms 3596 KiB
01-60.txt WA 1 ms 3704 KiB
01-61.txt WA 1 ms 3748 KiB
01-62.txt WA 1 ms 3584 KiB
01-63.txt WA 1 ms 3792 KiB
01-64.txt WA 1 ms 3768 KiB
01-65.txt WA 1 ms 3796 KiB
01-66.txt WA 1 ms 3592 KiB
01-67.txt AC 1 ms 3768 KiB
01-68.txt AC 1 ms 3588 KiB
01-69.txt AC 1 ms 3768 KiB
01-70.txt AC 1 ms 3584 KiB
01-71.txt AC 1 ms 3676 KiB
01-72.txt AC 1 ms 3732 KiB
01-73.txt AC 1 ms 3536 KiB
01-74.txt AC 1 ms 3604 KiB
01-75.txt AC 1 ms 3708 KiB
01-76.txt AC 1 ms 3624 KiB
01-77.txt AC 1 ms 3660 KiB
01-78.txt AC 1 ms 3716 KiB
01-79.txt AC 1 ms 3784 KiB
01-80.txt AC 1 ms 3808 KiB
01-81.txt AC 1 ms 3708 KiB
01-82.txt AC 1 ms 3772 KiB
01-83.txt AC 1 ms 3808 KiB
01-84.txt AC 1 ms 3820 KiB
01-85.txt AC 1 ms 3584 KiB
01-86.txt AC 1 ms 3452 KiB
01-87.txt AC 1 ms 3456 KiB
01-88.txt AC 1 ms 3784 KiB
01-89.txt AC 1 ms 3596 KiB
01-90.txt AC 1 ms 3580 KiB
01-91.txt AC 1 ms 3748 KiB
01-92.txt AC 1 ms 3580 KiB