Submission #1106639

#TimeUsernameProblemLanguageResultExecution timeMemory
1106639Canuc80kPotatoes and fertilizers (LMIO19_bulves)C++17
100 / 100
104 ms30032 KiB
#include "bits/stdc++.h" using namespace std; using ll = long long; using ld = long double; using ull = unsigned long long; const ll N = 1e6 + 1; ll n; ll a[N], b[N]; namespace sub1 { const ll N = 1e6 + 1; ll f[N]; void solve() { ll res = 0; for (int i = 1; i <= n; i ++) f[i] = f[i - 1] + (a[i] - b[i]); for (int i = 1; i <= n; i ++) res += abs(f[i]); cout << res; } } namespace sub3 { const ll N = 3e3 + 1; ll d[N]; ll f[(ll)3e4 + 1], of[(ll)3e4 + 1]; void solve() { for (int i = 1; i <= n; i ++) d[i] = d[i - 1] + a[i] - b[i]; memset(f, 0x3f, sizeof f); of[0] = 0; for (int i = 1; i <= n; i ++) { for (int j = 0; j <= d[n]; j ++) { if (j != 0) f[j] = f[j - 1]; f[j] = min(f[j], of[j] + abs(d[i] - j)); } for (int j = 0; j <= d[n]; j ++) { of[j] = f[j]; f[j] = 1e18; } } cout << of[d[n]]; } } namespace sub5 { const ll N = 1e6 + 1; ll d[N]; void solve() { for (int i = 1; i <= n; i ++) { d[i] = d[i - 1] + a[i] - b[i]; } priority_queue<ll> pq; ll res = 0; for (int i = 1; i <= n; i ++) { if (d[i] < 0) res += -d[i], d[i] = 0; if (d[i] >= d[n]) res += d[i] - d[n], d[i] = d[n]; pq.push(d[i]); ll top = pq.top(); if (pq.top() > d[i]) { res += top - d[i]; pq.pop(); pq.push(d[i]); } // cout << "Debug: " << i << ' ' << d[i] << ' ' << res << endl; } cout << res; } } void doTest(ll testID) { cin >> n; ll sa = 0, sb = 0; for (int i = 1; i <= n; i ++) { cin >> a[i] >> b[i]; sa += a[i], sb += b[i]; } // if (sa == sb) {sub1::solve(); return;} // if (n <= 3e3 && max(sa, sb) <= 3e4) {sub3::solve(); return;} sub5::solve(); } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); int test = 1; // cin >> test; for (int _ = 1; _ <= test; _ ++) doTest(_); }
#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...