Submission #713683

#TimeUsernameProblemLanguageResultExecution timeMemory
713683mtxasPotatoes and fertilizers (LMIO19_bulves)C++14
24 / 100
79 ms8148 KiB
#include <bits/stdc++.h> #pragma GCC optimize("O3") #define int long long #define DISABLE_PRINTF #ifdef DISABLE_PRINTF #define printf(...) #endif // DISABLE_PRINTF using namespace std; void solve() { int n; cin >> n; vector<int> a(n + 1), b(n + 1); for(int i = 1; i <= n; ++i) { cin >> a[i] >> b[i]; } int A = 0; int B = 0; for(int i = 1; i <= n; ++i) { A += a[i], B += b[i]; } if(A == B) { /// solve it int ans = 0; int aid = 1, bid = 1; for(; bid <= n; ++bid) { for(; b[bid] > 0; ++aid) { int take = min(a[aid], b[bid]); b[bid] -= take; a[aid] -= take; ans += take * abs(bid - aid); if(b[bid] == 0) { break; } } } cout << ans; } else if(A == B + 1) { vector<int> pref(n + 1), suff(n + 2); vector<int> acopy = a, bcopy = b; /// calc pref int bid = 1; for(int i = 1; i <= n; ++i) { pref[i] = pref[i - 1]; for(; a[i] > 0; bid += (b[bid] == 0) ) { int take = min(a[i], b[bid]); a[i] -= take; b[bid] -= take; pref[i] += abs(i - bid) * take; } } a = acopy; b = bcopy; /// calc suff for(int i = n; i >= 1; --i) { suff[i] = suff[i + 1]; for(; a[i] > 0; bid -= (b[bid] == 0) ) { int take = min(a[i], b[bid]); a[i] -= take; b[bid] -= take; suff[i] += abs(i - bid) * take; } } int ans = suff[1]; for(int i = 1; i <= n; ++i) { ans = min(ans, pref[i] + suff[i + 1]); } cout << ans << '\n'; } } signed main() { #ifdef EVAL cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(false); #else freopen("input_00.txt", "r", stdin); #endif // EVAL solve(); }
#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...