Submission #1352759

#TimeUsernameProblemLanguageResultExecution timeMemory
1352759adscodingSplit the sequence (APIO14_sequence)C++17
100 / 100
785 ms81836 KiB
#include <bits/stdc++.h>
#define SZ(x) ((int)x.size())
#define fi first
#define se second
#define pb push_back
#define FOR(i, a, b) for (int i = a, _b = b; i <= _b; ++i)
#define FORD(i, a, b) for (int i = a, _b = b; i >= _b; --i)
#define FORLL(i, a, b) for (ll i = a, _b = b; i <= _b; ++i)
#define FORDLL(i, a, b) for (ll i = a, _b = b; i >= _b; --i)
#define all(x) x.begin(), x.end()
#define uni(x) sort(all(x)), x.erase(unique(all(x)), x.end())
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

#define dbg(...) debug(#__VA_ARGS__, __VA_ARGS__)

template<typename T>
void __print_one(const char *&s, const T &x)
{
    while (*s == ' ') ++s;
    const char *p = s;
    int bal = 0;
    while (*s)
    {
        if (*s == '(') ++bal;
        else if (*s == ')') --bal;
        else if (*s == ',' && bal == 0) break;
        ++s;
    }
    cerr.write(p, s - p) << " = " << x;
    if (*s == ',')
    {
        ++s;
        cerr << "  ,  ";
    }
}

template<typename... Args>
void debug(const char *s, Args... args)
{
    cerr << "[  ";
    int dummy[] = { 0 , ( __print_one(s, args) , 0 )... };
    (void)dummy;
    cerr << "  ]\n\n";
}

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

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

// --------------------------------------------------------------------------------------------

constexpr int maxn = 1e5 + 3;
int n, K, OPT[201][maxn], ik;
ll a[maxn], dp_before[maxn], dp_cur[maxn], pre[maxn];

// --------------------------------------------------------------------------------------------

ll cost(int l, int r)
{
    return (pre[r] - pre[l - 1]) * (pre[n] - pre[r]);
}

void DNC(int l, int r, int optl, int optr)
{
    if (l > r) return;
    int mid = l + r >> 1;
    pll opt = {ll(-1e18), ll(-1e9)};
    FOR(i, optl, min(optr, mid))
        maximize(opt, pll(dp_before[i - 1] + cost(i, mid), i));
//    dbg(l, r, optl, optr, opt.se);
    OPT[ik][mid] = opt.se - 1;
    maximize(dp_cur[mid], opt.fi);
    DNC(l, mid - 1, optl, opt.se);
    DNC(mid + 1, r, opt.se, optr);
}

void solve()
{
    cin >> n >> K;
    FOR(i, 1, n)
    {
        cin >> a[i];
        pre[i] = pre[i - 1] + a[i];
    }

    for (ik = 1; ik <= K; ++ik)
    {
        FOR(j, 1, n)
        {
            dp_before[j] = dp_cur[j];
            dp_cur[j] = -1e18;
        }
        DNC(ik, n - 1, ik, n - 1);
//        dbg(ik);
//        FOR(i, 1, n)
//            cerr << dp_cur[i] << ' ';
//        cerr << endl;
    }

    pll res = {ll(-1e18), ll(-1e18)};
    FOR(i, 1, n - 1)
        maximize(res, pll(dp_cur[i], i));

    cout << res.fi << '\n';
    int cur = K, pos = res.se;
    vector<int> vecpos;
//    dbg(OPT[1][3]);
    while (cur)
    {
        vecpos.push_back(pos);
        pos = OPT[cur][pos];
        --cur;
    }
    reverse(all(vecpos));
    for (int x : vecpos)
        cout << x << ' ';
}

signed main()
{
    ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
    #define TASK "TEST"
    if (fopen(TASK".INP", "r"))
    {
        freopen(TASK".INP", "r", stdin);
        freopen(TASK".OUT", "w", stdout);
    }
    solve();
    return 0;
}

Compilation message (stderr)

sequence.cpp: In function 'int main()':
sequence.cpp:147:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  147 |         freopen(TASK".INP", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
sequence.cpp:148:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  148 |         freopen(TASK".OUT", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...