Submission #876570

#TimeUsernameProblemLanguageResultExecution timeMemory
876570rukashiiSplit the sequence (APIO14_sequence)C++17
71 / 100
135 ms19032 KiB
#include <bits/stdc++.h> using namespace std; #define file if (fopen("input.txt", "r")) { freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); } #define int long long #define s second #define f first void setIn(string s) { freopen(s.c_str(),"r",stdin); } void setOut(string s) { freopen(s.c_str(),"w",stdout); } void setIO(string s = "") { if (s.size()) setIn(s+".in"), setOut(s+".out"); } const int maxn = 1e5 + 2, maxk = 202; int a[maxn], prf[maxn], n, k; namespace Sub2 { const int s2maxn = 52, s2maxk = 52; int dp[s2maxn][s2maxn][s2maxk]; int cal(int l, int r, int krem) { if (l == r || krem == 0) return dp[l][r][krem] = 0; int res = dp[l][r][krem]; if (res != -1) return res; res = 0; for (int i = l; i < r; i++) { for (int lk = 0; lk < krem; lk++) { int rk = krem - lk - 1; res = max(res, cal(l, i, lk) + cal(i + 1, r, rk) + (prf[r] - prf[i]) * (prf[i] - prf[l - 1])); } } return dp[l][r][krem] = res; } void trace(int l, int r, int krem) { bool skip = 0; for (int i = l; i < r; i++) { for (int lk = 0; lk < krem; lk++) { int rk = krem - lk - 1; if (dp[l][r][krem] == cal(l, i, lk) + cal(i + 1, r, rk) + (prf[r] - prf[i]) * (prf[i] - prf[l - 1])) { cout << i << ' '; trace(1, i, lk); trace(i + 1, r, rk); skip = 1; break; } } if (skip) break; } } void solve() { memset(dp, -1, sizeof(dp)); cout << cal(1, n, k) << '\n'; trace(1, n, k); } } namespace Sub4 { const int s4maxn = 1002, s4maxk = 202; int dp[s4maxn][s4maxk]; void solve() { // dp[0][0] = 0; memset(dp, -0x3f, sizeof(dp)); dp[0][0] = 0; k++; for (int i = 1; i <= n; i++) { for (int j = 1; j <= k; j++) { for (int p = 0; p < i; p++) { //from n -> i + 1 //from i -> p + 1 dp[i][j] = max(dp[p][j - 1] + (prf[n] - prf[i]) * (prf[i] - prf[p]), dp[i][j]); } } } cout << dp[n][k] << '\n'; int curk = k, curb = n; while (curk > 0) { for (int i = 0; i < curb; i++) { if (dp[i][curk - 1] + (prf[n] - prf[curb]) * (prf[curb] - prf[i]) == dp[curb][curk]) { if (i) cout << i << ' '; curk--; curb = i; } } } } } namespace Sub5 { const int s5maxn = 1e4 + 2, s5maxk = 202; int dp[s5maxk][s5maxn]; void compute(int l, int r, int optl, int optr, int curk) { if (l > r) return; int mid = (l + r) / 2; pair <int, int> best = {LLONG_MIN, -1}; for (int i = optl; i <= min(optr, mid); i++) { best = max(best, {dp[curk - 1][i] + (prf[n] - prf[mid]) * (prf[mid] - prf[i]), i}); } int opt = best.second; dp[curk][mid] = best.first; compute(l, mid - 1, optl, opt, curk); compute(mid + 1, r, opt, optr, curk); } void solve() { k++; memset(dp, -0x3f, sizeof(dp)); dp[0][0] = 0; for (int i = 1; i <= k; i++) compute(0, n, 0, n, i); cout << dp[k][n] << '\n'; int curk = k, curb = n; while (curk > 0) { for (int i = 0; i < curb; i++) { if (dp[curk - 1][i] + (prf[n] - prf[curb]) * (prf[curb] - prf[i]) == dp[curk][curb]) { if (i) cout << i << ' '; curk--; curb = i; } } } } } namespace Sub6 { int dp[6][maxn], pt, trace[202][maxn]; vector <pair <int, int>> A, B; bool ccw(int l1, int l2, int l3) { return (B[l1].f - B[l2].f) * (A[l3].f - A[l1].f) >= (B[l1].f - B[l3].f) * (A[l2].f - A[l1].f); } void add(int a, int b, int id) { // if (A.size() && A.back().f == a) // { // if (B.back().f >= b) // return; // else // A.pop_back(), B.pop_back(); // } A.push_back({a, id}), B.push_back({b, id}); pt++; while (A.size() >= 3 && ccw(A.size() - 3, A.size() - 2, A.size() - 1)) { A.erase(A.end() - 2); B.erase(B.end() - 2); } } pair <int, int> query(int x) { pt = min(pt, (int)A.size() - 1); int sz = A.size(); while (pt && ((A[pt - 1].f - A[pt].f) * x ) > (B[pt].f - B[pt - 1].f)) pt--; return {A[pt].f * x + B[pt].f, A[pt].s}; } /* * dp[j][i] = 0..i with j-th split happening at between a[i] and a[i+1] * * dp[j][i] = max(dp[j-1][k] + (p[i] - p[k]) * (p[n] - p[i])) * = p[i]p[n] - p[i]p[i] + max(p[i]p[k] + dp[j-1][k] - p[k]p[n]) **/ void solve() { k++; for (int i = 1;i <= n;i++) dp[1][i] = prf[i] * (prf[n] - prf[i]); for (int i = 2; i <= k; i++) { A.clear(), B.clear(); pt = 0; add(prf[i - 1], -prf[i - 1] * prf[n] + dp[!(i & 1)][i - 1], i - 1); for (int j = i; j <= n; j++) { auto tmp = query(prf[j]); dp[(i & 1)][j] = prf[j] * prf[n] - prf[j] * prf[j] + tmp.f; trace[i][j] = tmp.s; if (i > 1) add(prf[j], dp[!(i & 1)][j] - prf[j] * prf[n], j); } } cout << dp[k % 2][n] << '\n'; int curk = k, curb = n; while (curk > 0) { if (trace[curk][curb]) cout << trace[curk][curb] << ' '; curb = trace[curk][curb]; curk--; } } } signed main() { // setIO(); file; ios::sync_with_stdio(0); cin.tie(0); cin >> n >> k; for (int i = 1; i <= n; i++) cin >> a[i], prf[i] = prf[i - 1] + a[i]; if (n <= 50) return Sub2::solve(), 0; if (n <= 1000) return Sub4::solve(), 0; if (n <= 10000) return Sub5::solve(), 0; if (n <= 100000) return Sub6::solve(), 0; cout << "From ITK20 w love UwU"; }

Compilation message (stderr)

sequence.cpp: In function 'std::pair<long long int, long long int> Sub6::query(long long int)':
sequence.cpp:206:13: warning: unused variable 'sz' [-Wunused-variable]
  206 |         int sz = A.size();
      |             ^~
sequence.cpp: In function 'void setIn(std::string)':
sequence.cpp:9:31: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
    9 | void setIn(string s) { freopen(s.c_str(),"r",stdin); }
      |                        ~~~~~~~^~~~~~~~~~~~~~~~~~~~~
sequence.cpp: In function 'void setOut(std::string)':
sequence.cpp:10:32: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   10 | void setOut(string s) { freopen(s.c_str(),"w",stdout); }
      |                         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
sequence.cpp: In function 'int main()':
sequence.cpp:4:53: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
    4 | #define file  if (fopen("input.txt", "r")) { freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); }
      |                                              ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
sequence.cpp:257:5: note: in expansion of macro 'file'
  257 |     file;
      |     ^~~~
sequence.cpp:4:87: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
    4 | #define file  if (fopen("input.txt", "r")) { freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); }
      |                                                                                ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
sequence.cpp:257:5: note: in expansion of macro 'file'
  257 |     file;
      |     ^~~~
#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...