Submission #390043

#TimeUsernameProblemLanguageResultExecution timeMemory
390043vonatlusSplit the sequence (APIO14_sequence)C++17
50 / 100
214 ms131072 KiB
/// adil sultanov | vonat1us #pragma GCC optimize("O3") //#pragma comment(linker, "/STACK:36777216") #include<bits/stdc++.h> #define x first #define y second #define pb push_back #define sz(x) (int) x.size() #define all(z) (z).begin(), (z).end() using namespace std; using ll = long long; using pii = pair<int, int>; const int MOD = 1e9 + 7; const int INF = 1e9 + 1e2; void fin() { #ifdef AM freopen(".in", "r", stdin); #endif } const bool flag = 0; const int N = 1e5+10; const int K = 210; int p[N]; ll dp[N][2]; int pr[N][K]; void f(int r, int k) { if (k == 1) return; int cut = pr[r][k]; cout << cut+1 << " "; f(cut, k-1); } struct Line { ll k, b, id; Line() {} Line(int k, ll b, int id): k(k), b(b), id(id) {} ll get(int x) { return x*1ll*k + b; } ll cross(Line l) { if (k == l.k) return -INF; return (b-l.b)/(l.k-k); } } v[N*2]; int l, r; void add(Line L) { while (r > 1 && v[r-1].cross(L) <= v[r-2].cross(v[r-1])) { r--; } v[r++] = L; } int get(int x) { while (l+1 < r && v[l].get(x) > v[l+1].get(x)) ++l; return v[l].get(x); } int n; void solve(int k) { l = 0, r = 0; int h = (k&1); for (int i = 0; i < n; ++i) { if (i >= k-1) { int pos = get(p[i]); dp[i][h] = v[pos].get(p[i]); pr[i][k] = v[pos].id; } add(Line(p[i], dp[i][1-h]-p[i]*1ll*p[i], i)); } } void ma1n() { int k; cin >> n >> k; k++; for (int i = 0, x; i < n; ++i) { cin >> x; p[i] = (i ? p[i-1] : 0)+x; } memset(pr, -1, sizeof(pr)); if (n <= 1000) { for (int l = 2; l <= k; ++l) { int h = (l&1); for (int i = 0; i < n; ++i) { dp[i][h] = -INF; for (int j = 0; j < i; ++j) { ll res = dp[j][1-h] + (p[i]-p[j])*1ll*p[j]; if (res >= dp[i][h]) { dp[i][h] = res, pr[i][l] = j; } } } } } else { for (int l = 2; l <= k; ++l) { solve(l); } } cout << dp[n-1][k&1] << "\n"; f(n-1, k); } int main() { ios_base::sync_with_stdio(0); cin.tie(nullptr), fin(); int ts = 1; if (flag) { cin >> ts; } while (ts--) { ma1n(); } return 0; }
#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...