제출 #855477

#제출 시각아이디문제언어결과실행 시간메모리
855477PragmatismSplit the sequence (APIO14_sequence)C++17
50 / 100
811 ms131072 KiB
//Bismillahir-Rahmanir-Rahim

#include <bits/stdc++.h>
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")

#define pb push_back
#define pii pair <int, int>
#define pll pair <long long, long long>
#define pld pair <long double, long double>
#define ll long long
#define ld long double
#define x first
#define y second
#define all(v) v.begin(),v.end()
#define sz(s) (int)s.size()
#define skip continue
#define bpop(x) (ll)__builtin_popcountll(x)

using namespace std;

const int N = 1e5 + 7;
const int M = 1e2 + 7;
const int MAXA = 5e4 + 7;
const int inf = 1e9 + 7;
const ll INF = 1e18 + 7;
const ll MOD = 1e9 + 7;

pii dir[] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};

#define int long long

//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

int n, k, a[N], pre[N], pr[N][207], dp[N][207];
struct chtDynamic {
    struct line {
        int m, b, val, pos;
        double x;
        bool query;
        line(int _m = 0, int _b = 0, int i = 0): m(_m), b(_b), val(0), pos(i), x(0), query(false) {}
        int eval(int x) {
            return m * x + b;
        }
        bool parallel(const line &other) const {
            return m == other.m;
        }
        ld intersect(const line &other) const {
          return (parallel(other) ? inf : 1.0 * (other.b - b) / (m - other.m));
        }
        bool operator < (const line &other) const {
          if(other.query) return x < other.val;
          else return m < other.m;
        }
    };
    set<line> hull;
    bool cPrev(auto it) { return it != hull.begin(); }
    bool cNext(auto it) { return it != hull.end() && next(it) != hull.end(); }
    bool bad(const line &l1, const line &l2, const line &l3) {
        return l1.intersect(l3) <= l1.intersect(l2);
    }
    bool bad(auto it) {
        return cPrev(it) && cNext(it) && bad(*prev(it), *it, *next(it));
    }
    auto update(auto it) {
        if(!cPrev(it)) return it;
        ld x = it -> intersect(*prev(it));
        line tmp(*it); tmp.x = x;
        it = hull.erase(it);
        return hull.insert(it, tmp);
    }
    void addLine(int m, int b, int pos) {
        line l(m, b, pos);
        auto it = hull.lower_bound(l);
        if(it != hull.end() && l.parallel(*it)) {
            if(it -> b < b) it = hull.erase(it);
            else return;
        }
        it = hull.insert(it, l);
        if(bad(it)) return (void) hull.erase(it);
        while(cPrev(it) && bad(prev(it))) hull.erase(prev(it));
        while(cNext(it) && bad(next(it))) hull.erase(next(it));
        it = update(it);
        if(cPrev(it)) update(prev(it));
        if(cNext(it)) update(next(it));
    }
    pii get(int x) const {
        if(hull.empty())return {0, 1};
        line q;
        q.val = x, q.query = 1;
        auto it = --hull.lower_bound(q);
        line opt = *it;
        return {opt.eval(x), opt.pos};
    }
}convex[207];
void solve() {
    cin >> n >> k;
    for (int i = 1;i <= n;i++)cin >> a[i], pre[i] = pre[i - 1] + a[i];
    convex[1].addLine(pre[1], -pre[1] * pre[1], 1);
    for (int i = 2;i <= n;i++) {
        for (int j = min(i, k + 1);j >= 2;j--) {
            pii p = convex[j - 1].get(pre[i]);
            if (p.x >= dp[i][j])dp[i][j] = p.x, pr[i][j] = p.y;
            convex[j].addLine(pre[i], dp[i][j] - pre[i] * pre[i], i);
        }
        convex[1].addLine(pre[i], -pre[i] * pre[i], i);
    }
    int ans = dp[n][k + 1];
    cout << ans << '\n';
    int pos = n, cur = k + 1;
    vector <int> path;
    while (cur > 0) {
        pos = pr[pos][cur], cur--;
        if (pos)path.pb(pos);
    }
    reverse(all(path));
    for (auto to : path)cout << to << ' ';
}
signed main() {
    //srand(time(NULL));
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    //freopen("milkorder.in", "r", stdin);
    //freopen("milkorder.out", "w", stdout);
    int test = 1;
    //cin >> test;
    for (int i = 1;i <= test;i++) {
        //cout << "Case " << i << ": ";
        solve();
    }
    return 0;
}

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

sequence.cpp:4: warning: ignoring '#pragma comment ' [-Wunknown-pragmas]
    4 | #pragma comment(linker, "/stack:200000000")
      | 
sequence.cpp:58:16: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
   58 |     bool cPrev(auto it) { return it != hull.begin(); }
      |                ^~~~
sequence.cpp:59:16: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
   59 |     bool cNext(auto it) { return it != hull.end() && next(it) != hull.end(); }
      |                ^~~~
sequence.cpp:63:14: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
   63 |     bool bad(auto it) {
      |              ^~~~
sequence.cpp:66:17: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
   66 |     auto update(auto it) {
      |                 ^~~~
#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...