Submission #1062422

# Submission time Handle Problem Language Result Execution time Memory
1062422 2024-08-17T06:37:49 Z CodeLakVN Feast (NOI19_feast) C++17
51 / 100
39 ms 2808 KB
#include <bits/stdc++.h>

#define no "NO"
#define yes "YES"
#define F first
#define S second
#define vec vector
#define task "FEAST"
#define ll long long
#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 maximize(U &a, V b) {
    if (a < b) { a = b; return 1; } return 0;
}
template<typename U, typename V> bool minimize(U &a, V b) {
    if (a > b) { a = b; return 1; } return 0;
}

const int N = (int)3e5 + 9;
const int INF = (int)1e9;
const int MOD = (int)1e9 + 7;

int n, k;
int a[N];

namespace sub3 {
    bool valid() {
        return k == 1;
    }

    void solve() {
        ll sum = 0, minPre = 0, ans = 0;
        FOR(i, 1, n) {
            sum += a[i];
            maximize(ans, sum - minPre);
            minimize(minPre, sum);
        }
        cout << ans << "\n";
    }
}

namespace sub1 {
    bool valid() {
        return *min_element(a + 1, a + n + 1) >= 0;
    }

    void solve() {
        ll ans = 0;
        FOR(i, 1, n) ans += a[i];
        cout << ans << "\n";
    }
}

namespace sub2 {
    bool valid() {
        bool appeared = 0;
        FOR(i, 1, n) {
            if (a[i] < 0 && appeared) return false;
            if (a[i] < 0) appeared = 1;
        }
        return true;
    }

    void solve() {
        ll ans = 0;
        FOR(i, 1, n) ans += a[i];
        ans -= *min_element(a + 1, a + n + 1);
        cout << ans << "\n";
    }
}

namespace sub5 {
    const int N = 303;
    ll dp[N][N];
    ll sum[N];

    bool valid() {
        return n <= 300;
    }

    void solve() {
        FOR(i, 1, n) sum[i] = sum[i - 1] + a[i];

        memset(dp, -0x3f, sizeof(dp));
        dp[0][0] = 0;
        FOR(i, 1, n) FOR(j, 1, min(k, i)) {
            ll maxDP = dp[j - 1][j - 1];
            FOR(x, j, i) {
                maximize(dp[i][j], maxDP + (sum[i] - sum[x - 1]));
                maximize(maxDP, dp[x][j - 1]);
            }
            //cout << i << " " << j << "  " << dp[i][j] << "\n";
        }

        ll ans = 0;
        FOR(i, 1, n) FOR(j, 1, min(k, i)) maximize(ans, dp[i][j]);

        cout << ans << "\n";
    }
}

namespace sub6 {
    const int N = (int)2e3 + 3;
    ll dp[N][N], maxDP[N];
    ll sum[N];

    bool valid() {
        return n <= 2000;
    }

    void calc(int id, int l, int r, int optL, int optR) {
        if (l > r) return;
        int mid = (l + r) >> 1;
        int bestPos;
        FOR(i, optL, min(optR, r)) {
            ll newCost = maxDP[i - 1] + sum[mid] - sum[i - 1];
            if (maximize(dp[id][mid], newCost)) bestPos = i;
        }
        calc(id, l, mid - 1, optL, bestPos);
        calc(id, mid + 1, r, bestPos, optR);
    }

    void solve() {
        FOR(i, 1, n) sum[i] = sum[i - 1] + 1LL * a[i];

        memset(dp, -0x3f, sizeof(dp));
        dp[0][0] = 0;

        ll minPre = 0;
        FOR(i, 1, n) dp[1][i] = sum[i] - minPre, minimize(minPre, sum[i]);

        FOR(id, 2, k) {
            FOR(i, 1, n) maxDP[i] = max(maxDP[i - 1], dp[id - 1][i]), cout << i << " " << id - 1 << "  " << dp[id - 1][i] << "\n";
            calc(id, id, n, id, n);
        }
        ll ans = 0;
        FOR(j, 1, k) FOR(i, j, n) maximize(ans, dp[j][i]);
        cout << ans << "\n";
    }
}

void main_code() {
    cin >> n >> k;
    FOR(i, 1, n) cin >> a[i];

    if (sub3::valid()) sub3::solve();
    else if (sub1::valid()) sub1::solve();
    else if (sub2::valid()) sub2::solve();
    else if (sub5::valid()) sub5::solve();
    //if (sub6::valid()) sub6::solve();
}

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;
    int num_test = 1; if (MULTITEST) cin >> num_test;
    while (num_test--) { main_code(); cout << "\n"; }
}

/* Lak lu theo dieu nhac */

Compilation message

feast.cpp: In function 'int main()':
feast.cpp:165:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  165 |         freopen(task".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
feast.cpp:166:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  166 |         freopen(task".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
feast.cpp: In function 'void sub6::calc(int, int, int, int, int)':
feast.cpp:128:13: warning: 'bestPos' may be used uninitialized in this function [-Wmaybe-uninitialized]
  128 |         calc(id, l, mid - 1, optL, bestPos);
      |         ~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 19 ms 2648 KB Output is correct
2 Correct 19 ms 2632 KB Output is correct
3 Correct 19 ms 2808 KB Output is correct
4 Correct 24 ms 2768 KB Output is correct
5 Correct 28 ms 2764 KB Output is correct
6 Correct 19 ms 2652 KB Output is correct
7 Correct 18 ms 2768 KB Output is correct
8 Correct 19 ms 2768 KB Output is correct
9 Correct 19 ms 2652 KB Output is correct
10 Correct 19 ms 2776 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 2648 KB Output is correct
2 Correct 17 ms 2652 KB Output is correct
3 Correct 12 ms 2648 KB Output is correct
4 Correct 13 ms 2648 KB Output is correct
5 Correct 33 ms 2768 KB Output is correct
6 Correct 12 ms 2776 KB Output is correct
7 Correct 13 ms 2768 KB Output is correct
8 Correct 19 ms 2648 KB Output is correct
9 Correct 18 ms 2804 KB Output is correct
10 Correct 21 ms 2652 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 21 ms 2648 KB Output is correct
2 Correct 21 ms 2768 KB Output is correct
3 Correct 21 ms 2648 KB Output is correct
4 Correct 39 ms 2768 KB Output is correct
5 Correct 21 ms 2648 KB Output is correct
6 Correct 21 ms 2728 KB Output is correct
7 Correct 21 ms 2652 KB Output is correct
8 Correct 22 ms 2648 KB Output is correct
9 Correct 22 ms 2652 KB Output is correct
10 Correct 30 ms 2652 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 0 ms 2396 KB Output is correct
4 Correct 0 ms 2396 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 1 ms 2392 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 0 ms 2396 KB Output is correct
10 Correct 1 ms 2508 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 0 ms 2396 KB Output is correct
4 Correct 0 ms 2396 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 1 ms 2392 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 0 ms 2396 KB Output is correct
10 Correct 1 ms 2508 KB Output is correct
11 Correct 3 ms 2396 KB Output is correct
12 Correct 2 ms 2396 KB Output is correct
13 Correct 1 ms 2396 KB Output is correct
14 Correct 2 ms 2396 KB Output is correct
15 Correct 3 ms 2396 KB Output is correct
16 Correct 2 ms 2392 KB Output is correct
17 Correct 5 ms 2392 KB Output is correct
18 Correct 1 ms 2396 KB Output is correct
19 Correct 1 ms 2396 KB Output is correct
20 Correct 1 ms 2396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 0 ms 2396 KB Output is correct
4 Correct 0 ms 2396 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 1 ms 2392 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 0 ms 2396 KB Output is correct
10 Correct 1 ms 2508 KB Output is correct
11 Correct 3 ms 2396 KB Output is correct
12 Correct 2 ms 2396 KB Output is correct
13 Correct 1 ms 2396 KB Output is correct
14 Correct 2 ms 2396 KB Output is correct
15 Correct 3 ms 2396 KB Output is correct
16 Correct 2 ms 2392 KB Output is correct
17 Correct 5 ms 2392 KB Output is correct
18 Correct 1 ms 2396 KB Output is correct
19 Correct 1 ms 2396 KB Output is correct
20 Correct 1 ms 2396 KB Output is correct
21 Incorrect 1 ms 2396 KB Output isn't correct
22 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 19 ms 2648 KB Output is correct
2 Correct 19 ms 2632 KB Output is correct
3 Correct 19 ms 2808 KB Output is correct
4 Correct 24 ms 2768 KB Output is correct
5 Correct 28 ms 2764 KB Output is correct
6 Correct 19 ms 2652 KB Output is correct
7 Correct 18 ms 2768 KB Output is correct
8 Correct 19 ms 2768 KB Output is correct
9 Correct 19 ms 2652 KB Output is correct
10 Correct 19 ms 2776 KB Output is correct
11 Correct 12 ms 2648 KB Output is correct
12 Correct 17 ms 2652 KB Output is correct
13 Correct 12 ms 2648 KB Output is correct
14 Correct 13 ms 2648 KB Output is correct
15 Correct 33 ms 2768 KB Output is correct
16 Correct 12 ms 2776 KB Output is correct
17 Correct 13 ms 2768 KB Output is correct
18 Correct 19 ms 2648 KB Output is correct
19 Correct 18 ms 2804 KB Output is correct
20 Correct 21 ms 2652 KB Output is correct
21 Correct 21 ms 2648 KB Output is correct
22 Correct 21 ms 2768 KB Output is correct
23 Correct 21 ms 2648 KB Output is correct
24 Correct 39 ms 2768 KB Output is correct
25 Correct 21 ms 2648 KB Output is correct
26 Correct 21 ms 2728 KB Output is correct
27 Correct 21 ms 2652 KB Output is correct
28 Correct 22 ms 2648 KB Output is correct
29 Correct 22 ms 2652 KB Output is correct
30 Correct 30 ms 2652 KB Output is correct
31 Correct 1 ms 2396 KB Output is correct
32 Correct 1 ms 2396 KB Output is correct
33 Correct 0 ms 2396 KB Output is correct
34 Correct 0 ms 2396 KB Output is correct
35 Correct 1 ms 2396 KB Output is correct
36 Correct 1 ms 2396 KB Output is correct
37 Correct 1 ms 2392 KB Output is correct
38 Correct 1 ms 2396 KB Output is correct
39 Correct 0 ms 2396 KB Output is correct
40 Correct 1 ms 2508 KB Output is correct
41 Correct 3 ms 2396 KB Output is correct
42 Correct 2 ms 2396 KB Output is correct
43 Correct 1 ms 2396 KB Output is correct
44 Correct 2 ms 2396 KB Output is correct
45 Correct 3 ms 2396 KB Output is correct
46 Correct 2 ms 2392 KB Output is correct
47 Correct 5 ms 2392 KB Output is correct
48 Correct 1 ms 2396 KB Output is correct
49 Correct 1 ms 2396 KB Output is correct
50 Correct 1 ms 2396 KB Output is correct
51 Incorrect 1 ms 2396 KB Output isn't correct
52 Halted 0 ms 0 KB -