제출 #1227736

#제출 시각아이디문제언어결과실행 시간메모리
1227736RiverFlowRainy Markets (CCO22_day1problem2)C++20
25 / 25
350 ms118328 KiB
#include <bits/stdc++.h>

#define nl "\n"
#define no "NO"
#define yes "YES"
#define fi first
#define se second
#define vec vector
#define task "main"
#define _mp make_pair
#define ii pair<int, int>
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define evoid(val) return void(std::cout << val)
#define FOR(i, a, b) for(int i = (a); i <= (b); ++i)
#define FOD(i, b, a) for(int i = (b); i >= (a); --i)
#define unq(x) sort(all(x)); x.resize(unique(all(x)) - x.begin())

using namespace std;

template<typename U, typename V> bool maxi(U &a, V b) {
    if (a < b) { a = b; return 1; } return 0;
}
template<typename U, typename V> bool mini(U &a, V b) {
    if (a > b or a == -1) { a = b; return 1; } return 0;
}

const int N = (int)1e6 + 9;
const int mod = (int)1e9 + 7;

void prepare(); void main_code();

int main() {
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    if (fopen(task".inp", "r")) {
//        freopen("C:\\Users\\ASUS\\Downloads\\2022CCOTestData\\day1data\\d1p2\\d1p2.4-112.in", "r", stdin);
        freopen(task".inp", "r", stdin);
        freopen(task".out", "w", stdout);
    }
    const bool MULTITEST = 0; prepare();
    int num_test = 1; if (MULTITEST) cin >> num_test;
    while (num_test--) { main_code(); }
}

void prepare() {};

int b[N], p[N], u[N];

int n;

namespace SUB2 {
int dp[2007][401];
int trace[2007][401];
int _at[2007];

int res[N][3];

void sol() {
    memset(dp, -1, sizeof dp);
    dp[1][0] = 0;
    for(int i = 1; i < n; ++i)
    for(int j = 0; j <= b[i]; ++j) if (dp[i][j] != -1) {
        for(int t = 0; t <= p[i]; ++t) {
            if (t > b[i + 1]) break ;
            if (p[i] - t > b[i] - j + u[i]) continue ;

            if (mini(dp[i + 1][t],
                dp[i][j] + max(0, p[i] - t - (b[i] - j)))) {
                    trace[i+1][t] = j;
                }
        }
    }
//    FOR(i, 2, n) {
//        FOR(j, 1, b[i])
//            cout << dp[i][j] << ' '; cout << nl;
//    }

    int ans = -1;
    int px = -1;
    for(int i = 0; i <= b[n]; ++i)
        if (dp[n][i] != -1 and mini(ans, dp[n][i]))
            px=i;
    assert(ans != -1);
    cout << yes << nl;
    cout << ans << nl;
    for(int i=n;i>1;--i){
        res[i][2]=px;
        int j = trace[i][px];
        // px la so nguoi da di qua phai
        // j la so luong nguoi truoc do roi
        int mid = max(0, p[i-1]-px - (b[i-1]-j));
        res[i][1] = mid;
        res[i][0] = p[i-1]-px-mid;
        px = j;
    }
    FOR(i, 2, n) cout << res[i][0] << ' ' << res[i][1] << ' ' << res[i][2] << nl;
}
};

bool check_false() {
    int c = 0;
    FOR(i, 1, n - 1) {
        // c la so nguoi tai i
        int d = max(0, p[i]-u[i]);
        d -= b[i] - c;
        c = max(0, d);
        if (c > b[i + 1])
            return 1;
    }
    return 0;
}

int B[N], P[N], U[N];

namespace SUB {

int buy[N];

void sol() {
    long long ans = 0;
    stack<int> st;
    FOR(i, 1, n - 1) {
        int j = min(b[i], p[i]);
        p[i] -= j, b[i] -= j;
        j = min(p[i], b[i+1]);
        p[i] -= j, b[i+1]-=j;

        st.push(i);
        ans += p[i];
        while (st.size() and p[i]){
            int j=st.top();
            int z = min(p[i], u[j]);
            u[j]-=z, p[i]-=z;
            buy[j]+=z;
            if (u[j]==0)
                st.pop();
        }
    }
    cout << yes << nl;
    cout << ans << nl;
    FOR(i, 1, n) b[i]=B[i], p[i]=P[i];
    FOR(i, 1, n - 1) {

        int d = min(b[i], p[i]);
        p[i] -= d;

        cout << d << ' ' << buy[i] << ' ';
        p[i] -= buy[i];

//        assert(p[i]>=0);
        b[i+1]-=p[i];
        cout << p[i] << nl;
    }
}
};


void main_code() {
    cin >> n;
    FOR(i, 1, n) cin >> b[i];
    FOR(i, 1, n - 1) cin >> p[i];
    FOR(i, 1, n - 1) cin >> u[i], u[i]=min(u[i], p[i]);

    FOR(i, 1, n) B[i]=b[i];
    FOR(i, 1, n - 1) P[i]=p[i];
    FOR(i, 1, n - 1) U[i]=u[i];

    int B = *max_element(b + 1, b + n + 1);
    int P = *max_element(p + 1, p + n + 1);
    int U = *max_element(u + 1, u + n + 1);

    if (check_false())
        evoid(no);
//    if (n <= 2000 and B <= 400 and P <= 200 and U <= 200)
//        return SUB2::sol(), void();

    SUB::sol();
}


/*     Let the river flows naturally     */

컴파일 시 표준 에러 (stderr) 메시지

Main.cpp: In function 'int main()':
Main.cpp:37:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   37 |         freopen(task".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:38:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   38 |         freopen(task".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...