Submission #1227402

#TimeUsernameProblemLanguageResultExecution timeMemory
1227402RiverFlowRainy Markets (CCO22_day1problem2)C++20
5 / 25
60 ms6820 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(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;
}

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];

    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();

}


/*     Let the river flows naturally     */

Compilation message (stderr)

Main.cpp: In function 'int main()':
Main.cpp:36:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   36 |         freopen(task".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
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".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...