답안 #1113188

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1113188 2024-11-16T03:14:44 Z kzhi Feast (NOI19_feast) C++14
63 / 100
69 ms 63568 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 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], 0));
        }

        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 {
    vector < ll > v;

    ll s[N];
    int par[N];

    void build(){
        FOR (i, 1, n){
            par[i] = -1;
            s[i] = a[i];
        }
    }

    int F(int x){
        if (par[x] < 0)
            return x;
        return par[x] = F(par[x]);
    }

    void Merge(int u, int v){
        u = F(u);
        v = F(v);
        if (u == v)
            return;

        if (par[u] > par[v])
            swap(u, v);

        par[u] += par[v];
        par[v] = u;
        s[u] += s[v];
    }

    void SOLVE(){
        build();

        FOR (i, 1, n - 1)
            if (a[i] >= 0 && a[i + 1] >= 0)
                Merge(i, i + 1);

        FOR (i, 1, n)
            if (F(i) == i && a[i] >= 0)
                v.pb(s[i]);

        sort(all(v));

        if (k >= v.size()){
            ll res = 0;

            for (ll i : v)
                res += i;

            cout << res;
        }
        else{
            ll res = 0;
            int sz = v.size();

            FOR (i, 1, k)
                res += v[sz - i];

            cout << res;
        }
    }
}

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), 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 'void FULL::SOLVE()':
feast.cpp:162:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  162 |         if (k >= v.size()){
      |             ~~^~~~~~~~~~~
feast.cpp: In function 'int main()':
feast.cpp:31:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   31 |         {freopen(TASK".inp","r",stdin);freopen(TASK".out","w",stdout);}
      |          ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
feast.cpp:223:5: note: in expansion of macro 'openfile'
  223 |     openfile("feast")
      |     ^~~~~~~~
feast.cpp:31:47: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   31 |         {freopen(TASK".inp","r",stdin);freopen(TASK".out","w",stdout);}
      |                                        ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
feast.cpp:223:5: note: in expansion of macro 'openfile'
  223 |     openfile("feast")
      |     ^~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 30 ms 5456 KB Output is correct
2 Correct 25 ms 6736 KB Output is correct
3 Correct 27 ms 6740 KB Output is correct
4 Correct 27 ms 6744 KB Output is correct
5 Correct 27 ms 6684 KB Output is correct
6 Correct 27 ms 6480 KB Output is correct
7 Correct 26 ms 6320 KB Output is correct
8 Correct 25 ms 6652 KB Output is correct
9 Correct 30 ms 6632 KB Output is correct
10 Correct 27 ms 6472 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 4176 KB Output is correct
2 Correct 17 ms 5000 KB Output is correct
3 Incorrect 17 ms 4572 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 30 ms 7796 KB Output is correct
2 Correct 32 ms 7752 KB Output is correct
3 Correct 32 ms 9232 KB Output is correct
4 Correct 34 ms 8152 KB Output is correct
5 Correct 32 ms 8080 KB Output is correct
6 Correct 28 ms 9204 KB Output is correct
7 Correct 33 ms 9032 KB Output is correct
8 Correct 30 ms 9112 KB Output is correct
9 Correct 30 ms 9312 KB Output is correct
10 Correct 31 ms 8016 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 46 ms 63220 KB Output is correct
3 Correct 43 ms 63304 KB Output is correct
4 Correct 50 ms 63304 KB Output is correct
5 Correct 42 ms 63324 KB Output is correct
6 Correct 46 ms 63304 KB Output is correct
7 Correct 43 ms 63352 KB Output is correct
8 Correct 43 ms 63312 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
10 Correct 53 ms 63212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 46 ms 63220 KB Output is correct
3 Correct 43 ms 63304 KB Output is correct
4 Correct 50 ms 63304 KB Output is correct
5 Correct 42 ms 63324 KB Output is correct
6 Correct 46 ms 63304 KB Output is correct
7 Correct 43 ms 63352 KB Output is correct
8 Correct 43 ms 63312 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
10 Correct 53 ms 63212 KB Output is correct
11 Correct 44 ms 63304 KB Output is correct
12 Correct 43 ms 63408 KB Output is correct
13 Correct 43 ms 63304 KB Output is correct
14 Correct 43 ms 63304 KB Output is correct
15 Correct 43 ms 63304 KB Output is correct
16 Correct 42 ms 63304 KB Output is correct
17 Correct 42 ms 63304 KB Output is correct
18 Correct 41 ms 63304 KB Output is correct
19 Correct 44 ms 63324 KB Output is correct
20 Correct 42 ms 63312 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 46 ms 63220 KB Output is correct
3 Correct 43 ms 63304 KB Output is correct
4 Correct 50 ms 63304 KB Output is correct
5 Correct 42 ms 63324 KB Output is correct
6 Correct 46 ms 63304 KB Output is correct
7 Correct 43 ms 63352 KB Output is correct
8 Correct 43 ms 63312 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
10 Correct 53 ms 63212 KB Output is correct
11 Correct 44 ms 63304 KB Output is correct
12 Correct 43 ms 63408 KB Output is correct
13 Correct 43 ms 63304 KB Output is correct
14 Correct 43 ms 63304 KB Output is correct
15 Correct 43 ms 63304 KB Output is correct
16 Correct 42 ms 63304 KB Output is correct
17 Correct 42 ms 63304 KB Output is correct
18 Correct 41 ms 63304 KB Output is correct
19 Correct 44 ms 63324 KB Output is correct
20 Correct 42 ms 63312 KB Output is correct
21 Correct 55 ms 63556 KB Output is correct
22 Correct 69 ms 63560 KB Output is correct
23 Correct 56 ms 63560 KB Output is correct
24 Correct 52 ms 63568 KB Output is correct
25 Correct 57 ms 63500 KB Output is correct
26 Correct 53 ms 63568 KB Output is correct
27 Correct 55 ms 63560 KB Output is correct
28 Correct 45 ms 63540 KB Output is correct
29 Correct 45 ms 63564 KB Output is correct
30 Correct 45 ms 63560 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 30 ms 5456 KB Output is correct
2 Correct 25 ms 6736 KB Output is correct
3 Correct 27 ms 6740 KB Output is correct
4 Correct 27 ms 6744 KB Output is correct
5 Correct 27 ms 6684 KB Output is correct
6 Correct 27 ms 6480 KB Output is correct
7 Correct 26 ms 6320 KB Output is correct
8 Correct 25 ms 6652 KB Output is correct
9 Correct 30 ms 6632 KB Output is correct
10 Correct 27 ms 6472 KB Output is correct
11 Correct 16 ms 4176 KB Output is correct
12 Correct 17 ms 5000 KB Output is correct
13 Incorrect 17 ms 4572 KB Output isn't correct
14 Halted 0 ms 0 KB -