Submission #770941

# Submission time Handle Problem Language Result Execution time Memory
770941 2023-07-02T07:25:34 Z dxz05 Potatoes and fertilizers (LMIO19_bulves) C++17
0 / 100
1 ms 596 KB
#pragma GCC optimize("Ofast,O3,unroll-loops")
#pragma GCC target("avx,avx2")

#include <bits/stdc++.h>

using namespace std;

#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define bpc(x) __builtin_popcount(x)
#define bpcll(x) __builtin_popcountll(x)
#define MP make_pair
//#define endl '\n'

mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());

typedef long long ll;
const int MOD = 1e9 + 7;
const int N = 5e5 + 30;

int a[N], b[N];
void solve(){
    int n;
    cin >> n;

    for (int i = 1; i <= n; i++) cin >> a[i] >> b[i];

    deque<int> before, after;
    ll sum = 0;
    for (int i = 2; i <= n; i++){
        if (a[i] > b[i]) after.push_back(i);
        sum += a[i] - b[i];
    }

    ll ans = 0;
    auto transfer = [&](int to, int from) -> void{
        int mn = min(a[from] - b[from], b[to] - a[to]);

//        cout << from << " -> " << to << ": " << mn << " units\n";

        ans += (ll) mn * abs(from - to);
        a[from] -= mn;
        a[to] += mn;

        if (from > to) sum -= mn;
    };

    for (int i = 1; i <= n; i++){
        if (!after.empty() && after.front() == i) after.pop_front();

        if (a[i] >= b[i]){
            if (a[i] > b[i]) before.push_back(i);
            continue;
        }

        while (a[i] < b[i]){
            int x = -1, y = -1;
            if (!before.empty()) x = before.back();
            if (sum > 0) y = after.front();

            if (y != -1 && (x == -1 || y - i < i - x)){
                transfer(i, y);
                if (a[y] == b[y]) after.pop_front();
            } else if (x != -1){
                transfer(i, x);
                if (a[x] == b[x]) before.pop_back();
            } else {
                break;
            }

        }

        assert(a[i] == b[i]);
    }

    cout << ans << endl;

}

int main(){
    clock_t startTime = clock();
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif

    int test_cases = 1;
//    cin >> test_cases;

    for (int test = 1; test <= test_cases; test++){
        //cout << (solve() ? "YES" : "NO") << endl;
        solve();
    }

#ifdef LOCAL
    cerr << "Time: " << int((double) (clock() - startTime) / CLOCKS_PER_SEC * 1000) << " ms" << endl;
#endif

    return 0;
}

Compilation message

bulves.cpp: In function 'int main()':
bulves.cpp:81:13: warning: unused variable 'startTime' [-Wunused-variable]
   81 |     clock_t startTime = clock();
      |             ^~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Runtime error 1 ms 596 KB Execution killed with signal 6
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Runtime error 1 ms 596 KB Execution killed with signal 6
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Incorrect 1 ms 340 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Runtime error 1 ms 596 KB Execution killed with signal 6
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Runtime error 1 ms 596 KB Execution killed with signal 6
4 Halted 0 ms 0 KB -