이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define ff first
#define ss second
int N, M, L;
ll A[200005], X[200005], mx, S[200005];
vector<pii> V[200005];
bool g(ll m, ll n, int p) {
fill(S, S + N + 1, 0);
priority_queue<pii> pq;
ll s = 0;
for (int i = 1; i <= p; ++i) {
for (auto j : V[i]) if (j.ff > p) pq.push(j);
while (s < X[i]) {
if (pq.empty()) return 0;
auto t = pq.top(); pq.pop();
if (s + t.ss <= X[i]) {
S[t.ff] += t.ss;
s += t.ss;
}
else {
S[t.ff] += X[i] - s;
t.ss -= X[i] - s;
s = X[i];
pq.push(t);
}
}
}
for (int i = p + 1; i <= N; ++i) {
s -= S[i];
if (A[i] - s + n - s > m) return 0;
}
return 1;
}
bool f(ll m) {
for (ll n = mx - m; n <= mx - m + 1; ++n) {
for (int i = 1; i <= L; ++i) X[i] = (A[i] + n - m + 1) / 2;
if (g(m, n, L)) return 1;
}
return 0;
}
int main() {
ios_base::sync_with_stdio(0), cin.tie(0);
cin >> N >> M;
for (int i = 1; i <= M; ++i) {
int a, b, c; cin >> a >> b >> c;
if (a > b) swap(a, b);
A[a] += c;
A[b] -= c;
V[a].emplace_back(b, c);
}
for (int i = 1; i <= N; ++i) A[i] += A[i - 1];
mx = *max_element(A + 1, A + N + 1);
for (int i = 1; i <= N; ++i) if (mx == A[i]) {
L = i;
break;
}
ll l = 1, r = mx;
while (l < r) {
ll m = l + r >> 1;
if (f(m)) r = m;
else l = m + 1;
}
cout << l;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |