제출 #1369087

#제출 시각아이디문제언어결과실행 시간메모리
1369087nikolanPotatoes and fertilizers (LMIO19_bulves)C++20
24 / 100
149 ms15920 KiB
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

typedef long long ll;

int main() {
    int n;
    cin >> n;
    vector<ll> a(n), b(n);
    for (int i = 0; i < n; i++) cin >> a[i] >> b[i];

    // L[i] and R[i] represent the feasible corridor for the net flow f_i
    vector<ll> L(n + 1, 0), R(n + 1, 0);

    // Backward pass: Determine the global constraints needed to reach f_n = 0
    // Based on: f_{i-1} must be in [f_i + b_i - a_i, f_i + b_i]
    for (int i = n - 1; i >= 0; i--) {
        L[i] = L[i + 1] + b[i] - a[i];
        R[i] = R[i + 1] + b[i];
    }

    ll current_f = 0;
    ll total_cost = 0;

    // Forward pass: Pick the f_i closest to 0 within the feasible corridor
    for (int i = 0; i < n; i++) {
        // Local range forced by the previous flow:
        ll local_L = current_f - b[i];
        ll local_R = current_f + a[i] - b[i];

        // Intersect with the global corridor [L[i+1], R[i+1]]
        ll final_L = max(local_L, L[i + 1]);
        ll final_R = min(local_R, R[i + 1]);

        // Pick the value in [final_L, final_R] closest to 0
        if (0 < final_L) current_f = final_L;
        else if (0 > final_R) current_f = final_R;
        else current_f = 0;

        total_cost += abs(current_f);
    }

    cout << total_cost << endl;
}
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…