제출 #916377

#제출 시각아이디문제언어결과실행 시간메모리
916377wiiSplit the sequence (APIO14_sequence)C++17
71 / 100
307 ms131072 KiB
#include <bits/stdc++.h>
using namespace std;

typedef double db;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;

#define lx (id << 1)
#define rx (lx  1)
#define gcd __gcd
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define bit(i, mask) ((mask) >> (i) & 1)
#define reset(x, val) memset(x, val, sizeof(x))
#define foru(i,a,b) for(int i = (a); i <= (b); ++i)
#define ford(i,a,b) for(int i = (a); i >= (b); --i)
#define FastIO ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);

template<typename T> bool maximize(T &res, const T &val) { if (res < val) { res = val; return true; } return false; }
template<typename T> bool minimize(T &res, const T &val) { if (val < res) { res = val; return true; } return false; }

const ll Linf = 0x3f3f3f3f3f3f3f3f;
const int Inf = 0x3f3f3f3f;
const ll Mod = 1e9 + 7;
const ll Mod2 = ll(1e9) + 9;
const int Lim = 1e6 + 5;
const int inv6 = 166666668;

/// ====*====*====*====*====*====*====*====*====*====*====*====*====*====*====*====*====

const int base = 3;
const int N = 1e5 + 5;
const int K = 200 + 1;
const int dx[] = {+1, -1, 0, 0};
const int dy[] = {0, 0, +1, -1};
const int block_size = sqrt(2e9) + 2;

struct Line {
    mutable ll a, b, p;

    bool operator< (const Line &x) const { return a < x.a; }
    bool operator< (ll x) const { return p < x; }

    ll query(ll x) {
        return a * x + b;
    }
};

struct LineContainer : multiset<Line, less<>> {
    ll div(ll a, ll b) {
        return a / b - ((a ^ b) < 0 && a % b);
    }

    bool isect(iterator x, iterator y) {
        if (y == end()) return x -> p = Linf, false;

        if (x -> a == y -> a)
            x -> p = (x -> b > y -> b ? Linf : -Linf);
        else
            x -> p = div(y -> b - x -> b, x -> a - y -> a);
        return x -> p >= y -> p;
    }

    void add(ll k, ll m) {
        iterator z = insert({k, m, 0}), y = z++, x = y;
        while (isect(y, z)) z = erase(z);

        if (x != begin() && isect(--x, y)) isect(x, y = erase(y));
        while ((y = x) != begin() && (--x) -> p >= y -> p)
            isect(x, erase(y));
    }

    ll query(ll x) {
        Line l = *lower_bound(x);
        return l.query(x);
    }
};

int n, k;
int a[N], pref[N];
ll dp[N][K];

LineContainer cht;
ll f(int l, int r) {
    return pref[r] - pref[l - 1];
}

void solve() {
    cin >> n >> k;
    foru(i, 1, n) cin >> a[i];
    foru(i, 1, n) pref[i] = pref[i - 1] + a[i];

    foru(i, 0, n) foru(z, 0, k) dp[i][z] = -Linf;

    dp[0][0] = 0;

    foru(z, 1, k) {
        cht.clear();
        foru(i, 1, n - 1) {
            int j = i - 1;
            cht.add(-pref[j], dp[j][z - 1]);

            dp[i][z] = cht.query(f(i + 1, n)) + f(i + 1, n) * pref[i];

            /*foru(z, 1, k)
                cout << (dp[i][z] <= -Inf ? -1 : dp[i][z]) << " ";
            cout << "\n";*/
        }
    }


    ll ans = -Linf;
    foru(i, 1, n - 1) maximize(ans, dp[i][k]);

    cout << ans << "\n";
    for (int cur = n - 1, z = k; cur > 0 && z > 0; --cur) {
        // cout << "? " << cur << " " << z << "\n";
        if (dp[cur][z] == ans) {
            // cout << "! " << cur << " " << z << "\n";
            for (int j = 0; j < cur; ++j)
                if (dp[j][z - 1] + f(j + 1, cur) * f(cur + 1, n) == ans) {
                    // cout << cur << " " << j <<  "\n";

                    cout << cur << " ";

                    cur = j + 1;
                    --z;
                    ans = dp[j][z];
                    break;
                }
        }
    }
}

signed main() {
    FastIO;

    #define task ""
    if (fopen(task".inp", "r")) {
        freopen(task".inp", "r", stdin);
        freopen(task".out", "w", stdout);
    }

    #define task "test"
    if (fopen(task".inp", "r")) {
        freopen(task".inp", "r", stdin);
        freopen(task".out", "w", stdout);
    } else if (0) {
        ofstream fout(task".inp");
        fout.close();

        freopen(task".inp", "r", stdin);
        freopen(task".out", "w", stdout);
    }

    #ifdef Sieve
        Linear_sieve();
    #endif

    int ntest = 1;
    // cin >> ntest;
    while (ntest--) {
        //cout << "Case " << q << ": " << "\n";
        solve();
        cout << "\n";
    }

    return 0;
}

/**  /\_/\
 *  (= ._.)
 *  / >TL \>AC
**/

컴파일 시 표준 에러 (stderr) 메시지

sequence.cpp:145: warning: "task" redefined
  145 |     #define task "test"
      | 
sequence.cpp:139: note: this is the location of the previous definition
  139 |     #define task ""
      | 
sequence.cpp: In function 'int main()':
sequence.cpp:141:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  141 |         freopen(task".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
sequence.cpp:142:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  142 |         freopen(task".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
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);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
sequence.cpp:153:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  153 |         freopen(task".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
sequence.cpp:154:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  154 |         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...