제출 #636828

#제출 시각아이디문제언어결과실행 시간메모리
636828elkernos수열 (APIO14_sequence)C++17
71 / 100
98 ms131072 KiB
// while (clock()<=69*CLOCKS_PER_SEC)
// #pragma comment(linker, "/stack:200000000")
// #pragma GCC optimize("O3")
// #pragma GCC target ("avx2")
// #pragma GCC optimize("Ofast")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
// #pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;
using namespace std;

template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define sim template <class c
#define ris return *this
#define dor > debug &operator<<
#define eni(x)                                                                    \
    sim > typename enable_if<sizeof dud<c>(0) x 1, debug &>::type operator<<(c i) \
    {
sim > struct rge {
    c b, e;
};
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c *x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef XOX
    ~debug()
    {
        cerr << endl;
    }
    eni(!=) cerr << boolalpha << i;
    ris;
} eni(==) ris << range(begin(i), end(i));
}
sim, class b dor(pair<b, c> d)
{
    ris << "" << d.first << " --> " << d.second << "";
}
sim dor(rge<c> d)
{
    *this << "[";
    for(auto it = d.b; it != d.e; ++it)
        *this << ", " + 2 * (it == d.b) << *it;
    ris << "]";
}
#else
    sim dor(const c &)
    {
        ris;
    }
#endif
}
;
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "

#ifdef XOX
#warning Times may differ!!!
#endif

#define endl '\n'
#define pb emplace_back
#define vt vector
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

const int nax = 1e6, mod = 1e9 + 7;

struct L {
    ll a, b, from;
    ll operator()(ll x)
    {
        return a * x + b;
    }
    pair<ll, ll> inter(L &he) const
    {
        ll up = he.b - b;
        ll down = a - he.a;
        return {up, down};
    }
};

struct CHT {
    vector<L> hull;
    int start = 0;
    pair<ll, ll> query(ll x)
    {
        while(start <= (int)hull.size() - 2) {
            L A = hull[start], B = hull[start + 1];
            if(A(x) <= B(x))
                start++;
            else
                break;
        }
        return {hull[start](x), hull[start].from};
    }
    void add(L x)
    {
        while(start <= (int)hull.size() - 2) {
            L A = hull.end()[-2], B = hull.end()[-1];
            auto one = A.inter(B), two = B.inter(x);
            if(two.second == 0) {
                if(B.b <= x.b)
                    hull.pop_back();
                else
                    break;
                continue;
            }
            if(one.first * two.second <= two.first * one.second)
                hull.pop_back();
            else
                break;
        }
        hull.push_back(x);
    }
};

ll sq(ll x)
{
    return x * x;
}

int main()
{
    cin.tie(nullptr)->sync_with_stdio(0);
    int n, k;
    cin >> n >> k;
    vector<int> p(n + 1);
    for(int i = 1; i <= n; i++) {
        cin >> p[i];
        p[i] += p[i - 1];
    }
    // p[i]*(p[j]-p[i])+dp[j]
    // p[i]p[j]-p[i]^2+dp[j]
    vector<CHT> hull(k + 1);
    hull[0].add({p[n], 0, n});
    vt<vt<int>> kto(n + 1, vt<int>(k + 1));
    for(int i = n - 1; i >= 1; i--) {
        for(int use = k; use >= 1; use--) {
            if(hull[use - 1].hull.empty()) continue;
            pair<ll, ll> dp = hull[use - 1].query(p[i]);
            L in = {p[i], -sq(p[i]) + dp.first, i};
            kto[i][use] = dp.second;
            hull[use].add(in);
        }
    }
    pair<ll, ll> dp = hull[k].query(0);
    cout << dp.first << endl;
    vector<int> print;
    while(k) {
        cout << dp.second << " ";
        dp.second = kto[dp.second][k--];
    }
    cout << endl;
}
#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...