Submission #1113196

# Submission time Handle Problem Language Result Execution time Memory
1113196 2024-11-16T03:22:52 Z kzhi Feast (NOI19_feast) C++14
100 / 100
132 ms 63572 KB
/*
    Author : kmv a.k.a kzhi
    K41 IT CHV
*/
#include <bits/stdc++.h>
using namespace std;


#define int long long
#define ll long long
#define FOR(i,a,b) for (int i = a ; i <= b; ++ i)
#define FOD(i,a,b) for (int i = a; i >= b; -- i)


#define BIT(mask,i)       ((mask >> i) & 1)
#define MASK(i)                (1ll << (i))
#define OFFBIT(mask,i)  (mask &~(1ll<<(i)))
#define ONBIT(mask,i) (mask | (1ll << (i)))
#define lg2(x)    (63 - __builtin_clzll(x))
#define c_bit          __builtin_popcountll

#define vi vector < int >
#define all(a) a.begin(), a.end()
#define pb push_back

#define ii pair<int,int>
#define pll pair < ll, ll >
#define fi first
#define se second

#define openfile(TASK) if (fopen(TASK".inp","r"))\
        {freopen(TASK".inp","r",stdin);freopen(TASK".out","w",stdout);}
#define endl '\n'
#define Faster ios_base::sync_with_stdio(false); \
                        cin.tie(0);  cout.tie(0);

#define mid(l,r) ((l + r) >> 1)
#define left(id) (id << 1)
#define right(id) ((id << 1) | 1)
#define ci const int

template <class X, class Y> bool maximize(X &a, const Y &b){if(a < b) return a = b, true;return false;}
template <class X, class Y> bool minimize(X &a, const Y &b){if(a > b) return a = b, true;return false;}

const int N = 3e5 + 5;
const ll oo = 1e17 + 7;
const int LOG = lg2(N);

int n, k;
int a[N];
ll t[N];

ll sum(int l, int r){
    if (l > r)
        return 0;
    return t[r] - t[l - 1];
}

bool ok1 = 1;
int cnt2 = 0, pos2 = -1;

namespace SUB1{
    ll pt[N];

    void SOLVE(){
        pt[0] = oo;
        ll res = - oo;

        FOR (i, 1, n){
            pt[i] = min(t[i], pt[i - 1]);
            maximize(res, t[i] - pt[i - 1]);
            maximize(res, t[i]);
        }

        maximize(res, 0ll);
        cout << res;
    }
}

namespace SUB6{
    static const int maxN = 2e3 + 5;

    ll dp[maxN][maxN][2];

    ll sol(int i, int cnt, bool ok){
        if (i == n + 1 || cnt == k + 1)
            return 0;
        if (dp[i][cnt][ok] != - oo)
            return dp[i][cnt][ok];
        ll cur = - oo;

        if (!ok){
            maximize(cur, sol(i + 1, cnt, 1) + a[i]);

            maximize(cur, sol(i + 1, cnt, 0));
        }
        else{
            maximize(cur, sol(i + 1, cnt, 1) + a[i]);

            maximize(cur, sol(i + 1, cnt + 1, 0) + max(a[i], 0ll));
        }

        return dp[i][cnt][ok] = cur;
    }

    void SOLVE(){
        FOR (i, 0, maxN - 1)
            FOR (j, 0, maxN - 1)
                FOR (ok, 0, 1)
                    dp[i][j][ok] = - oo;


        cout << sol(1, 1, 0);
    }
}

namespace FULL {
    pll dp[N][2];

    bool check(int res) {
        dp[0][0] = {0, 0}, dp[0][1] = {-oo, 0};
        for (int i = 1; i <= n; ++i) {
            dp[i][0] = max(dp[i - 1][0], dp[i - 1][1]);
            dp[i][1] =
                max(make_pair(dp[i - 1][1].fi + a[i], dp[i - 1][1].se),
                    make_pair(dp[i - 1][0].fi + a[i] - res, dp[i - 1][0].se + 1));
        }
        return max(dp[n][0], dp[n][1]).se >= k;
    };

    void SOLVE(){
        ll l = 0, r = oo;

        while (l + 1 < r){
            ll mid = mid(l, r);

            if(check(mid))
                l = mid;
            else
                r = mid;
        }

        check(l);

        cout << l * k + max(dp[n][0].fi, dp[n][1].fi);
    }
}

void SOLVE(){
    cin >> n >> k;

    FOR (i, 1, n){
        cin >> a[i];
        t[i] = t[i - 1] + a[i];
        if (a[i] < 0){
            ok1 = 0;
            cnt2 ++;
            pos2 = i;
        }
    }

    if (ok1){
        cout << t[n];
        return;
    }

    if (cnt2 == 1){
        if (k >= 2)
            cout << t[n] - a[pos2];
        else
            cout << max({t[n], sum(1, pos2 - 1), sum(pos2 + 1, n)});
        return;
    }

    if (k == 1){
        SUB1 :: SOLVE();
        return;
    }

    if (n <= 2000 && k <= 2000){
        SUB6 :: SOLVE();
        return;
    }

    FULL :: SOLVE();
}

signed main(){
    Faster
    openfile("feast")

    int q = 1;

//    cin >> q;

    while (q --){
        SOLVE();
    }

    return 0;
}

Compilation message

feast.cpp: In function 'int main()':
feast.cpp:32:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   32 |         {freopen(TASK".inp","r",stdin);freopen(TASK".out","w",stdout);}
      |          ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
feast.cpp:190:5: note: in expansion of macro 'openfile'
  190 |     openfile("feast")
      |     ^~~~~~~~
feast.cpp:32:47: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   32 |         {freopen(TASK".inp","r",stdin);freopen(TASK".out","w",stdout);}
      |                                        ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
feast.cpp:190:5: note: in expansion of macro 'openfile'
  190 |     openfile("feast")
      |     ^~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 22 ms 4944 KB Output is correct
2 Correct 22 ms 4944 KB Output is correct
3 Correct 22 ms 5132 KB Output is correct
4 Correct 22 ms 4944 KB Output is correct
5 Correct 22 ms 4884 KB Output is correct
6 Correct 25 ms 5088 KB Output is correct
7 Correct 24 ms 4940 KB Output is correct
8 Correct 26 ms 4944 KB Output is correct
9 Correct 26 ms 4944 KB Output is correct
10 Correct 23 ms 4916 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 4944 KB Output is correct
2 Correct 17 ms 4944 KB Output is correct
3 Correct 18 ms 4944 KB Output is correct
4 Correct 15 ms 4944 KB Output is correct
5 Correct 22 ms 5004 KB Output is correct
6 Correct 15 ms 4944 KB Output is correct
7 Correct 15 ms 4944 KB Output is correct
8 Correct 25 ms 4944 KB Output is correct
9 Correct 22 ms 4936 KB Output is correct
10 Correct 18 ms 4956 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 30 ms 7248 KB Output is correct
2 Correct 30 ms 7248 KB Output is correct
3 Correct 30 ms 7236 KB Output is correct
4 Correct 29 ms 7188 KB Output is correct
5 Correct 32 ms 7432 KB Output is correct
6 Correct 28 ms 7248 KB Output is correct
7 Correct 28 ms 7248 KB Output is correct
8 Correct 30 ms 7240 KB Output is correct
9 Correct 28 ms 7352 KB Output is correct
10 Correct 29 ms 7240 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 40 ms 63308 KB Output is correct
3 Correct 43 ms 63212 KB Output is correct
4 Correct 45 ms 63212 KB Output is correct
5 Correct 46 ms 63304 KB Output is correct
6 Correct 44 ms 63376 KB Output is correct
7 Correct 42 ms 63312 KB Output is correct
8 Correct 45 ms 63312 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
10 Correct 50 ms 63304 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 40 ms 63308 KB Output is correct
3 Correct 43 ms 63212 KB Output is correct
4 Correct 45 ms 63212 KB Output is correct
5 Correct 46 ms 63304 KB Output is correct
6 Correct 44 ms 63376 KB Output is correct
7 Correct 42 ms 63312 KB Output is correct
8 Correct 45 ms 63312 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
10 Correct 50 ms 63304 KB Output is correct
11 Correct 45 ms 63312 KB Output is correct
12 Correct 42 ms 63436 KB Output is correct
13 Correct 49 ms 63304 KB Output is correct
14 Correct 45 ms 63304 KB Output is correct
15 Correct 46 ms 63304 KB Output is correct
16 Correct 45 ms 63304 KB Output is correct
17 Correct 43 ms 63304 KB Output is correct
18 Correct 50 ms 63316 KB Output is correct
19 Correct 43 ms 63260 KB Output is correct
20 Correct 43 ms 63304 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 40 ms 63308 KB Output is correct
3 Correct 43 ms 63212 KB Output is correct
4 Correct 45 ms 63212 KB Output is correct
5 Correct 46 ms 63304 KB Output is correct
6 Correct 44 ms 63376 KB Output is correct
7 Correct 42 ms 63312 KB Output is correct
8 Correct 45 ms 63312 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
10 Correct 50 ms 63304 KB Output is correct
11 Correct 45 ms 63312 KB Output is correct
12 Correct 42 ms 63436 KB Output is correct
13 Correct 49 ms 63304 KB Output is correct
14 Correct 45 ms 63304 KB Output is correct
15 Correct 46 ms 63304 KB Output is correct
16 Correct 45 ms 63304 KB Output is correct
17 Correct 43 ms 63304 KB Output is correct
18 Correct 50 ms 63316 KB Output is correct
19 Correct 43 ms 63260 KB Output is correct
20 Correct 43 ms 63304 KB Output is correct
21 Correct 60 ms 63560 KB Output is correct
22 Correct 68 ms 63472 KB Output is correct
23 Correct 59 ms 63572 KB Output is correct
24 Correct 53 ms 63532 KB Output is correct
25 Correct 57 ms 63568 KB Output is correct
26 Correct 51 ms 63560 KB Output is correct
27 Correct 60 ms 63392 KB Output is correct
28 Correct 44 ms 63568 KB Output is correct
29 Correct 45 ms 63492 KB Output is correct
30 Correct 42 ms 63456 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 22 ms 4944 KB Output is correct
2 Correct 22 ms 4944 KB Output is correct
3 Correct 22 ms 5132 KB Output is correct
4 Correct 22 ms 4944 KB Output is correct
5 Correct 22 ms 4884 KB Output is correct
6 Correct 25 ms 5088 KB Output is correct
7 Correct 24 ms 4940 KB Output is correct
8 Correct 26 ms 4944 KB Output is correct
9 Correct 26 ms 4944 KB Output is correct
10 Correct 23 ms 4916 KB Output is correct
11 Correct 17 ms 4944 KB Output is correct
12 Correct 17 ms 4944 KB Output is correct
13 Correct 18 ms 4944 KB Output is correct
14 Correct 15 ms 4944 KB Output is correct
15 Correct 22 ms 5004 KB Output is correct
16 Correct 15 ms 4944 KB Output is correct
17 Correct 15 ms 4944 KB Output is correct
18 Correct 25 ms 4944 KB Output is correct
19 Correct 22 ms 4936 KB Output is correct
20 Correct 18 ms 4956 KB Output is correct
21 Correct 30 ms 7248 KB Output is correct
22 Correct 30 ms 7248 KB Output is correct
23 Correct 30 ms 7236 KB Output is correct
24 Correct 29 ms 7188 KB Output is correct
25 Correct 32 ms 7432 KB Output is correct
26 Correct 28 ms 7248 KB Output is correct
27 Correct 28 ms 7248 KB Output is correct
28 Correct 30 ms 7240 KB Output is correct
29 Correct 28 ms 7352 KB Output is correct
30 Correct 29 ms 7240 KB Output is correct
31 Correct 1 ms 336 KB Output is correct
32 Correct 40 ms 63308 KB Output is correct
33 Correct 43 ms 63212 KB Output is correct
34 Correct 45 ms 63212 KB Output is correct
35 Correct 46 ms 63304 KB Output is correct
36 Correct 44 ms 63376 KB Output is correct
37 Correct 42 ms 63312 KB Output is correct
38 Correct 45 ms 63312 KB Output is correct
39 Correct 1 ms 336 KB Output is correct
40 Correct 50 ms 63304 KB Output is correct
41 Correct 45 ms 63312 KB Output is correct
42 Correct 42 ms 63436 KB Output is correct
43 Correct 49 ms 63304 KB Output is correct
44 Correct 45 ms 63304 KB Output is correct
45 Correct 46 ms 63304 KB Output is correct
46 Correct 45 ms 63304 KB Output is correct
47 Correct 43 ms 63304 KB Output is correct
48 Correct 50 ms 63316 KB Output is correct
49 Correct 43 ms 63260 KB Output is correct
50 Correct 43 ms 63304 KB Output is correct
51 Correct 60 ms 63560 KB Output is correct
52 Correct 68 ms 63472 KB Output is correct
53 Correct 59 ms 63572 KB Output is correct
54 Correct 53 ms 63532 KB Output is correct
55 Correct 57 ms 63568 KB Output is correct
56 Correct 51 ms 63560 KB Output is correct
57 Correct 60 ms 63392 KB Output is correct
58 Correct 44 ms 63568 KB Output is correct
59 Correct 45 ms 63492 KB Output is correct
60 Correct 42 ms 63456 KB Output is correct
61 Correct 122 ms 15852 KB Output is correct
62 Correct 117 ms 14316 KB Output is correct
63 Correct 103 ms 14308 KB Output is correct
64 Correct 126 ms 14252 KB Output is correct
65 Correct 127 ms 14408 KB Output is correct
66 Correct 123 ms 14160 KB Output is correct
67 Correct 132 ms 15944 KB Output is correct
68 Correct 102 ms 16480 KB Output is correct
69 Correct 94 ms 15944 KB Output is correct
70 Correct 99 ms 13896 KB Output is correct