Submission #700254

#TimeUsernameProblemLanguageResultExecution timeMemory
700254nguyentunglamSplit the sequence (APIO14_sequence)C++17
0 / 100
17 ms4172 KiB
#include<bits/stdc++.h>
#define fi first
#define se second
#define endl "\n"
#define ii pair<int, int>
using namespace std;
const int N = 1e5 + 10;
long long f[210][N];
long long s[N];
struct line {
    long long m, c;
    long long eval(long long x) { return m * x + c; }
    long long calcx (line l) { return (c - l.c) / (l.m - m); }
};
int main() {
    #define task ""
    cin.tie(0) -> sync_with_stdio(0);
    if (fopen ("task.inp", "r")) {
        freopen ("task.inp", "r", stdin);
        freopen ("task.out", "w", stdout);
    }
    if (fopen (task".inp", "r")) {
        freopen (task".inp", "r", stdin);
        freopen (task".out", "w", stdout);
    }
    int n, k; cin >> n >> k;
    ++k;
    for(int i = 1; i <= n; i++) {
        cin >> s[i];
        s[i] += s[i - 1];
    }
    for(int j = 1; j <= k; j++) {
        deque<line> dq;
        dq.push_back({0, 0});
        for(int i = 1; i <= n; i++) {
            while (dq.size() >= 2 && dq.back().eval(s[i]) <= dq[dq.size() - 2].eval(s[i])) dq.pop_back();
            f[j][i] = dq.back().eval(s[i]) + s[i] * s[n] - s[i] * s[i];
            line cur = {s[i], f[j - 1][i] - s[i] * s[n]};
            if (!dq.empty() && dq.front().m == cur.m) {
                if (cur.c > dq.front().c) dq.pop_front();
                else continue;
            }
            while (dq.size() >= 2 && cur.calcx(dq[0]) >= dq[0].calcx(dq[1])) dq.pop_front();
            dq.push_front(cur);
        }
    }
    cout << f[k][n] << endl;
    int cur = n;
    vector<int> ans;
    for(int j = k; j >= 2; j--) {
        for(int i = cur - 1; i >= 1; i--) if (f[j - 1][i] + (s[cur] - s[i]) * (s[n] - s[cur]) == f[j][cur]) {
            cur = i;
            ans.push_back(cur);
            break;
        }
    }
    reverse(ans.begin(), ans.end());
    for(int &j : ans) cout << j << " ";
}

Compilation message (stderr)

sequence.cpp: In function 'int main()':
sequence.cpp:19:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   19 |         freopen ("task.inp", "r", stdin);
      |         ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
sequence.cpp:20:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   20 |         freopen ("task.out", "w", stdout);
      |         ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
sequence.cpp:23:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   23 |         freopen (task".inp", "r", stdin);
      |         ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
sequence.cpp:24:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   24 |         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...