Submission #1113195

# Submission time Handle Problem Language Result Execution time Memory
1113195 2024-11-16T03:22:37 Z kzhi Feast (NOI19_feast) C++14
Compilation error
0 ms 0 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] = {-inf, 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 'bool FULL::check(long long int)':
feast.cpp:121:41: error: 'inf' was not declared in this scope; did you mean 'ynf'?
  121 |         dp[0][0] = {0, 0}, dp[0][1] = {-inf, 0};
      |                                         ^~~
      |                                         ynf
feast.cpp:121:47: error: no match for 'operator=' (operand types are 'std::pair<long long int, long long int>' and '<brace-enclosed initializer list>')
  121 |         dp[0][0] = {0, 0}, dp[0][1] = {-inf, 0};
      |                                               ^
In file included from /usr/include/c++/10/bits/stl_algobase.h:64,
                 from /usr/include/c++/10/bits/char_traits.h:39,
                 from /usr/include/c++/10/ios:40,
                 from /usr/include/c++/10/istream:38,
                 from /usr/include/c++/10/sstream:38,
                 from /usr/include/c++/10/complex:45,
                 from /usr/include/c++/10/ccomplex:39,
                 from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:54,
                 from feast.cpp:5:
/usr/include/c++/10/bits/stl_pair.h:390:7: note: candidate: 'std::pair<_T1, _T2>& std::pair<_T1, _T2>::operator=(typename std::conditional<std::__and_<std::is_copy_assignable<_T1>, std::is_copy_assignable<_T2> >::value, const std::pair<_T1, _T2>&, const std::__nonesuch&>::type) [with _T1 = long long int; _T2 = long long int; typename std::conditional<std::__and_<std::is_copy_assignable<_T1>, std::is_copy_assignable<_T2> >::value, const std::pair<_T1, _T2>&, const std::__nonesuch&>::type = const std::pair<long long int, long long int>&]'
  390 |       operator=(typename conditional<
      |       ^~~~~~~~
/usr/include/c++/10/bits/stl_pair.h:393:41: note:   no known conversion for argument 1 from '<brace-enclosed initializer list>' to 'std::conditional<true, const std::pair<long long int, long long int>&, const std::__nonesuch&>::type' {aka 'const std::pair<long long int, long long int>&'}
  390 |       operator=(typename conditional<
      |                 ~~~~~~~~~~~~~~~~~~~~~    
  391 |   __and_<is_copy_assignable<_T1>,
      |   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~        
  392 |          is_copy_assignable<_T2>>::value,
      |          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  393 |   const pair&, const __nonesuch&>::type __p)
      |   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~
/usr/include/c++/10/bits/stl_pair.h:401:7: note: candidate: 'std::pair<_T1, _T2>& std::pair<_T1, _T2>::operator=(typename std::conditional<std::__and_<std::is_move_assignable<_Tp>, std::is_move_assignable<_T2> >::value, std::pair<_T1, _T2>&&, std::__nonesuch&&>::type) [with _T1 = long long int; _T2 = long long int; typename std::conditional<std::__and_<std::is_move_assignable<_Tp>, std::is_move_assignable<_T2> >::value, std::pair<_T1, _T2>&&, std::__nonesuch&&>::type = std::pair<long long int, long long int>&&]'
  401 |       operator=(typename conditional<
      |       ^~~~~~~~
/usr/include/c++/10/bits/stl_pair.h:404:31: note:   no known conversion for argument 1 from '<brace-enclosed initializer list>' to 'std::conditional<true, std::pair<long long int, long long int>&&, std::__nonesuch&&>::type' {aka 'std::pair<long long int, long long int>&&'}
  401 |       operator=(typename conditional<
      |                 ~~~~~~~~~~~~~~~~~~~~~
  402 |   __and_<is_move_assignable<_T1>,
      |   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  403 |          is_move_assignable<_T2>>::value,
      |          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  404 |   pair&&, __nonesuch&&>::type __p)
      |   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~
/usr/include/c++/10/bits/stl_pair.h:418:2: note: candidate: 'template<class _U1, class _U2> typename std::enable_if<std::__and_<std::is_assignable<_T1&, const _U1&>, std::is_assignable<_T2&, const _U2&> >::value, std::pair<_T1, _T2>&>::type std::pair<_T1, _T2>::operator=(const std::pair<_U1, _U2>&) [with _U1 = _U1; _U2 = _U2; _T1 = long long int; _T2 = long long int]'
  418 |  operator=(const pair<_U1, _U2>& __p)
      |  ^~~~~~~~
/usr/include/c++/10/bits/stl_pair.h:418:2: note:   template argument deduction/substitution failed:
feast.cpp:121:47: note:   couldn't deduce template parameter '_U1'
  121 |         dp[0][0] = {0, 0}, dp[0][1] = {-inf, 0};
      |                                               ^
In file included from /usr/include/c++/10/bits/stl_algobase.h:64,
                 from /usr/include/c++/10/bits/char_traits.h:39,
                 from /usr/include/c++/10/ios:40,
                 from /usr/include/c++/10/istream:38,
                 from /usr/include/c++/10/sstream:38,
                 from /usr/include/c++/10/complex:45,
                 from /usr/include/c++/10/ccomplex:39,
                 from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:54,
                 from feast.cpp:5:
/usr/include/c++/10/bits/stl_pair.h:430:2: note: candidate: 'template<class _U1, class _U2> typename std::enable_if<std::__and_<std::is_assignable<_T1&, _U1&&>, std::is_assignable<_T2&, _U2&&> >::value, std::pair<_T1, _T2>&>::type std::pair<_T1, _T2>::operator=(std::pair<_U1, _U2>&&) [with _U1 = _U1; _U2 = _U2; _T1 = long long int; _T2 = long long int]'
  430 |  operator=(pair<_U1, _U2>&& __p)
      |  ^~~~~~~~
/usr/include/c++/10/bits/stl_pair.h:430:2: note:   template argument deduction/substitution failed:
feast.cpp:121:47: note:   couldn't deduce template parameter '_U1'
  121 |         dp[0][0] = {0, 0}, dp[0][1] = {-inf, 0};
      |                                               ^
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")
      |     ^~~~~~~~