답안 #1062419

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1062419 2024-08-17T06:36:43 Z CodeLakVN Feast (NOI19_feast) C++17
30 / 100
30 ms 33368 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();
    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);
      |         ~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 20 ms 3408 KB Output is correct
2 Correct 20 ms 5456 KB Output is correct
3 Correct 21 ms 5720 KB Output is correct
4 Correct 27 ms 5460 KB Output is correct
5 Correct 21 ms 5456 KB Output is correct
6 Correct 21 ms 5352 KB Output is correct
7 Correct 21 ms 5468 KB Output is correct
8 Correct 21 ms 5468 KB Output is correct
9 Correct 30 ms 5572 KB Output is correct
10 Correct 20 ms 5468 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 2904 KB Output is correct
2 Correct 13 ms 3932 KB Output is correct
3 Correct 13 ms 3904 KB Output is correct
4 Correct 13 ms 3776 KB Output is correct
5 Correct 20 ms 5460 KB Output is correct
6 Correct 12 ms 3780 KB Output is correct
7 Correct 13 ms 3932 KB Output is correct
8 Correct 20 ms 5572 KB Output is correct
9 Correct 20 ms 5464 KB Output is correct
10 Correct 13 ms 3932 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 23 ms 3412 KB Output is correct
2 Correct 23 ms 5640 KB Output is correct
3 Correct 23 ms 5716 KB Output is correct
4 Correct 27 ms 5712 KB Output is correct
5 Correct 23 ms 5712 KB Output is correct
6 Correct 27 ms 5716 KB Output is correct
7 Correct 23 ms 5712 KB Output is correct
8 Correct 28 ms 5696 KB Output is correct
9 Correct 23 ms 5724 KB Output is correct
10 Correct 26 ms 5828 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 4 ms 33368 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 4 ms 33368 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 4 ms 33368 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 20 ms 3408 KB Output is correct
2 Correct 20 ms 5456 KB Output is correct
3 Correct 21 ms 5720 KB Output is correct
4 Correct 27 ms 5460 KB Output is correct
5 Correct 21 ms 5456 KB Output is correct
6 Correct 21 ms 5352 KB Output is correct
7 Correct 21 ms 5468 KB Output is correct
8 Correct 21 ms 5468 KB Output is correct
9 Correct 30 ms 5572 KB Output is correct
10 Correct 20 ms 5468 KB Output is correct
11 Correct 13 ms 2904 KB Output is correct
12 Correct 13 ms 3932 KB Output is correct
13 Correct 13 ms 3904 KB Output is correct
14 Correct 13 ms 3776 KB Output is correct
15 Correct 20 ms 5460 KB Output is correct
16 Correct 12 ms 3780 KB Output is correct
17 Correct 13 ms 3932 KB Output is correct
18 Correct 20 ms 5572 KB Output is correct
19 Correct 20 ms 5464 KB Output is correct
20 Correct 13 ms 3932 KB Output is correct
21 Correct 23 ms 3412 KB Output is correct
22 Correct 23 ms 5640 KB Output is correct
23 Correct 23 ms 5716 KB Output is correct
24 Correct 27 ms 5712 KB Output is correct
25 Correct 23 ms 5712 KB Output is correct
26 Correct 27 ms 5716 KB Output is correct
27 Correct 23 ms 5712 KB Output is correct
28 Correct 28 ms 5696 KB Output is correct
29 Correct 23 ms 5724 KB Output is correct
30 Correct 26 ms 5828 KB Output is correct
31 Incorrect 4 ms 33368 KB Output isn't correct
32 Halted 0 ms 0 KB -