제출 #1368232

#제출 시각아이디문제언어결과실행 시간메모리
1368232africBikeparking (EGOI24_bikeparking)C++20
9 / 100
25 ms10388 KiB
#include <bits/stdc++.h>
using namespace std;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int n;
    cin >> n;
    vector<long long> s;
    vector<long long> u;
    vector<int>pos;
    for (int i = 0; i < n; i++) {
        long long x; cin >> x; s.push_back(x);
        if (x > 0) {pos.push_back(i);};
    }
    for (int i = 0; i < n; i++) {
        long long x; cin >> x; u.push_back(x);
    }
    sort(pos.begin(),pos.end(),greater<int>());
    // first pass - go from large to small.
    long long opinion = 0;
    int ptr = 0;
    int rptr = 0;
    int counter = 0;
    
    for (int i = (n-1); i >= 0; i--) {
        if (ptr == pos.size()) {
            while (u[i] > 0) {
                if (s[i] > 0) {
                    if (u[i] > s[i]) {
                        u[i] -= s[i];
                        s[i] = 0;
                    }
                    else {
                        s[i] -= u[i];
                        u[i] = 0;
                    }
                }
                else {
                    while (s[pos[rptr]] == 0) {
                        rptr++;
                    }
                    if (u[i] > s[pos[rptr]]) {
                        u[i] -= s[pos[rptr]];
                        counter -= s[pos[rptr]];
                        s[pos[rptr]] = 0;
                        rptr++;
                    }
                    else {
                        s[pos[rptr]] -= u[i];
                        counter -= u[i];
                        u[i] = 0;
                    }
                }
            }
        }
        else {
            while (u[i] > 0) {
            while (ptr < pos.size() && pos[ptr] >= i) {
                ptr++;
            }
            if (ptr == pos.size()) {
                break;
            }
            if (s[pos[ptr]] > u[i]) {
                counter += u[i];
                u[i] = 0;
                s[pos[ptr]] -= u[i];
            }
            else {
                counter += s[pos[ptr]];
                u[i] -= s[pos[ptr]];
                s[pos[ptr]] = 0;
                ptr++;
            }
        }
        if (ptr == pos.size()) {
            while (u[i] > 0) {
                if (s[i] > 0) {
                    if (u[i] > s[i]) {
                        u[i] -= s[i];
                        s[i] = 0;
                    }
                    else {
                        s[i] -= u[i];
                        u[i] = 0;
                    }
                }
                else {
                    while (s[pos[rptr]] == 0) {
                        rptr--;
                    }
                    if (u[i] > s[pos[rptr]]) {
                        u[i] -= s[pos[rptr]];
                        counter -= s[pos[rptr]];
                        s[pos[rptr]] = 0;
                        rptr++;
                    }
                    else {
                        s[pos[rptr]] -= u[i];
                        counter -= u[i];
                        u[i] = 0;
                    }
                }
            }
        }
        }
    }
    cout << counter << endl;
    return 0;
}
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…