Submission #1115257

#TimeUsernameProblemLanguageResultExecution timeMemory
1115257gdragonSplit the sequence (APIO14_sequence)C++17
100 / 100
1224 ms82116 KiB
#include <bits/stdc++.h> using namespace std; #define TASK "long" #define fi first #define se second #define ll long long #define pb push_back #define ALL(x) (x).begin(), (x).end() #define GETBIT(mask, i) ((mask) >> (i) & 1) #define MASK(i) ((1LL) << (i)) #define SZ(x) ((int)(x).size()) #define mp make_pair #define CNTBIT(mask) __builtin_popcount(mask) template<class X, class Y> bool maximize(X &x, Y y){ if (x < y) {x = y; return true;} return false;}; template<class X, class Y> bool minimize(X &x, Y y){ if (x > y) {x = y; return true;} return false;}; typedef pair<int, int> ii; const int N = 1e5 + 5; const int INF = 1e9 + 7; const int mod = 1e9 + 7; long long dp[2][N], f[N]; int trace[205][N]; int n, k; int a[N]; void read() { cin >> n >> k; for(int i = 1; i <= n; i++) cin >> a[i]; } long long sum(int l, int r) { if (l > r) return 0; return f[r] - f[l - 1]; } long long cost(int l, int r) { return -(1LL * sum(l, r) * sum(r + 1, n)); } void calDp(int g, int l, int r, int optl, int optr) { if (l > r) return; int best = optl; int mid = (l + r) >> 1; for(int i = optl; i <= min(mid, optr); i++) { if (minimize(dp[(g & 1)][mid], dp[(g & 1) ^ 1][i - 1] + cost(i, mid))) { trace[g][mid] = i - 1; // cerr << g << ' ' << mid << ' ' << -dp[g][mid] << endl; best = i; } } calDp(g, l, mid - 1, optl, best); calDp(g, mid + 1, r, best, optr); } void solve() { for(int i = 1; i <= n; i++) f[i] = f[i - 1] + a[i]; memset(dp, 0x3f, sizeof(dp)); dp[0][0] = 0; ++k; for(int i = 1; i <= k; i++) { memset(dp[i & 1], 0x3f, sizeof(dp[i & 1])); calDp(i, 1, n, 1, n); } cout << -dp[k & 1][n] << endl; int j = k, i = n; vector<int> c; while(i) { i = trace[j][i]; if (i == 0) break; c.push_back(i); --j; } reverse(ALL(c)); for(int &x: c) cout << x << ' '; } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); if (fopen(TASK".inp", "r")) { freopen(TASK".inp", "r", stdin); freopen(TASK".out", "w", stdout); } int test = 1; // cin >> test; while(test--) { read(); solve(); } return 0; }

Compilation message (stderr)

sequence.cpp: In function 'int main()':
sequence.cpp:73:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   73 |         freopen(TASK".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
sequence.cpp:74:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   74 |         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...