Submission #418200

#TimeUsernameProblemLanguageResultExecution timeMemory
418200Kevin_Zhang_TWArranging Tickets (JOI17_arranging_tickets)C++17
65 / 100
6034 ms18100 KiB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define pb emplace_back
#define AI(i) begin(i), end(i)
template<class T> bool chmin(T &a, T b) { return b < a && (a = b, true); }
template<class T> bool chmax(T &a, T b) { return a < b && (a = b, true); }
#ifdef KEV
#define DE(args...) kout("[ " + string(#args) + " ] = ", args)
void kout() { cerr << endl; }
template<class T, class ...U> void kout(T a, U ...b) { cerr << a << ' ', kout(b...); }
template<class T> void debug(T l, T r) { while (l != r) cerr << *l << " \n"[next(l)==r], ++l; }
#else
#define DE(...) 0
#define debug(...) 0
#endif

const int MAX_N = 300010;
const ll inf = 1ll << 55;

int n, m;

vector<pair<int,ll>> rc[MAX_N];

int32_t main() {
	ios_base::sync_with_stdio(0), cin.tie(0);
	cin >> n >> m;
	vector<tuple<int,int,int>> req(m);
	for (auto &[l, r, c] : req) {
		cin >> l >> r >> c;
		if (l > r) swap(l, r);
		assert(c == 1);
	}

	vector<ll> sum(n + 10);

	for (auto [l, r, c] : req)
		sum[l] += c, sum[r] -= c;

	for (auto [l, r, c] : req) rc[l].pb(r, c);

	auto cal = [&](ll mx) {
		// first is right end, second is count
		priority_queue<pair<int,ll>> pq;
		vector<ll> pfsum(sum);

		ll use = 0;
		for (int i = 1;i <= n;++i) {
			for (auto [r, c] : rc[i]) 
				pq.emplace(r, c);

			pfsum[i] += pfsum[i-1];

			while (pfsum[i] > mx) {
				assert(pq.size());
				auto [r, c] = pq.top(); pq.pop();
				ll req = min(c, (pfsum[i] - mx + 1) / 2);
				use += req;
				pfsum[i] -= req * 2;
				pfsum[r] += req * 2;
				if (req < c) pq.emplace(r, c - req);
			}
		}

		return mx + use;
	};

	ll res = inf;

	for (int i = 0;i <= m;++i) {
		chmin(res, cal(i));
	}

	cout << res << '\n';
	
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...