Submission #700266

#TimeUsernameProblemLanguageResultExecution timeMemory
700266nguyentunglamSplit the sequence (APIO14_sequence)C++17
100 / 100
879 ms81472 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 + 2; long long f[N], g[N], s[N]; int used[210][N]; struct line { long long m, c; int trace; 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, 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[i] = dq.back().eval(s[i]) + s[i] * s[n] - s[i] * s[i]; used[j][i] = dq.back().trace; line cur = {s[i], g[i] - s[i] * s[n], i}; 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); } for(int i = 1; i <= n; i++) g[i] = f[i]; } cout << g[n] << endl; int cur = n, j = k; for(int j = k; j >= 2; j--) { cur = used[j][cur]; cout << cur << " "; } }

Compilation message (stderr)

sequence.cpp: In function 'int main()':
sequence.cpp:51:18: warning: unused variable 'j' [-Wunused-variable]
   51 |     int cur = n, j = k;
      |                  ^
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.inp", "r", stdin);
      |         ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
sequence.cpp:21:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   21 |         freopen ("task.out", "w", stdout);
      |         ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
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".inp", "r", stdin);
      |         ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
sequence.cpp:25:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   25 |         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...