Submission #876566

#TimeUsernameProblemLanguageResultExecution timeMemory
876566rukashiiSplit the sequence (APIO14_sequence)C++17
0 / 100
15 ms11964 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 bad(int l1, int l2, int l3) { return 1LL * (A[l2].f - A[l1].f) * (B[l3].f - B[l1].f) - 1LL * (A[l3].f - A[l1].f) * (B[l2].f - B[l1].f) > 0; } void add(int x, int y, int id) { // if (A.size() > 0 && A.back().f == x) // { // if (B.back().f >= y) // return; // else // A.pop_back(), B.pop_back(); // } A.push_back({x, id}), B.push_back({y, id}); // pt++; while (A.size() >= 3 && bad(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) { if (pt >= A.size()) pt = A.size() - 1; while (pt + 1 < A.size() && ((A[pt + 1].f * x + B[pt + 1].f) > (A[pt].f * x + B[pt].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 <= k; i++) { add(0, 0, 0); for (int j = 1; j <= n; j++) { dp[(i & 1)][j] = prf[j] * prf[n] - prf[j] * prf[j] + query(prf[j]).f; // cout << "-->" << i << ' ' << j << ' ' << prf[j] << '\n'; // for (int pp = 0; pp < A.size(); pp++) // cout << A[pp].f << ' ' << B[pp].f << ' ' << A[pp].s << '\n'; // cout << i << ' ' << j << ' ' << prf[j] * prf[n] - prf[j] * prf[j] + query(prf[j]).f << ' ' << query(prf[j]).f << ' ' << query(prf[j]).s << '\n'; trace[i][j] = query(prf[j]).s; if (i > 1) add(prf[j], dp[!(i & 1)][j] - prf[j] * prf[n], j); // cout << A.size() << '\n'; } A.clear(), B.clear(); pt = 0; } 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:201:16: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  201 |         if (pt >= A.size())
      |             ~~~^~~~~~~~~~~
sequence.cpp:204:23: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  204 |         while (pt + 1 < A.size() && ((A[pt + 1].f * x + B[pt + 1].f) > (A[pt].f * x + B[pt].f)))
      |                ~~~~~~~^~~~~~~~~~
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:258:5: note: in expansion of macro 'file'
  258 |     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:258:5: note: in expansion of macro 'file'
  258 |     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...