제출 #33585

#제출 시각아이디문제언어결과실행 시간메모리
33585sinhriv수열 (APIO14_sequence)C++14
71 / 100
1633 ms31304 KiB
#include <bits/stdc++.h> using namespace std; const int N = 10010; const int K = 220; int n, k; int arr[N]; int pre[N][K]; long long f[N][K]; template < class T > struct LineIT{ /// Lim static const T inf = -2e9; static const T Worst = -1e18; struct Line{ T a; T b; int idx; T get(T x){ if(a < inf) return Worst; return a * x + b; } }; T low, high; int root, cnt; vector < int > L; vector < int > R; vector < Line > it; Line Create(T u, T v, int i){ Line ret; ret.a = u; ret.b = v; ret.idx = i; return ret; } LineIT(T from, T to){ low = from; high = to; L.resize(N * 10 + 10, 0); R.resize(N * 10 + 10, 0); it.resize(N * 10 + 10, Create(inf - 1, inf - 1, 0)); root = cnt = 1; } void re(){ fill(L.begin(), L.end(), 0); fill(R.begin(), R.end(), 0); fill(it.begin(), it.end(), Create(inf - 1, inf - 1, 0)); cnt = 1; } T lef, rig; void upd(int &x, T l, T r, Line val){ if(l > rig || r < lef) return; if(x == 0) x = ++cnt; T mid = ((l + r) >> 1); if(l >= lef && r <= rig){ if(it[x].get(l) >= val.get(l) && it[x].get(r) >= val.get(r)){ return; } if(it[x].get(l) <= val.get(l) && it[x].get(r) <= val.get(r)){ it[x] = val; return; } if(it[x].get(l) <= val.get(l) && it[x].get(mid) <= val.get(mid)){ upd(R[x], mid + 1, r, it[x]); it[x] = val; return; } if(it[x].get(l) >= val.get(l) && it[x].get(mid) >= val.get(mid)){ upd(R[x], mid + 1, r, val); return; } if(it[x].get(mid + 1) <= val.get(mid + 1) && it[x].get(r) <= val.get(r)){ upd(L[x], l, mid, it[x]); it[x] = val; return; } if(it[x].get(mid + 1) >= val.get(mid + 1) && it[x].get(r) >= val.get(r)){ upd(L[x], l, mid, val); return; } } upd(L[x], l, mid, val); upd(R[x], mid + 1, r, val); } Line M(Line x, Line y, T val){ if(x.get(val) > y.get(val)) return x; return y; } Line query(int x, int l, int r, T val){ if(l > val || r < val || x == 0) return Create(inf - 1, inf - 1, 0); if(l == r) return it[x]; T mid = ((l + r) >> 1); Line ans = it[x]; ans = M(ans, query(L[x], l, mid, val), val); ans = M(ans, query(R[x], mid + 1, r, val), val); return ans; } void update(T a, T b, int idx, pair < T, T > seg = make_pair(-inf - 1, -inf - 1)){ if(seg.first != -inf - 1){ lef = seg.first; rig = seg.second; } else{ lef = low; rig = high; } upd(root, low, high, Create(a, b, idx)); } pair < T, int > ask(T x){ Line p = query(root, low, high, x); return make_pair(p.get(x), p.idx); } }; long long solve(vector < int > a){ LineIT < long long > tree(0, 1e9); vector < long long > sum(a.size()); partial_sum(a.begin(), a.end(), sum.begin()); long long ans = 0; for(int i = 1; i <= n; ++i){ for(int j = 1; j <= k; ++j){ f[i][j] = -1e9; } } f[0][0] = 0; for(int j = 1; j <= k; ++j){ tree.re(); for(int i = j; i <= n; ++i){ tree.update(sum[i - 1], -sum[i - 1] * sum[n] + f[i - 1][j - 1], i - 1); auto p = tree.ask(sum[i]); pre[i][j] = p.second; f[i][j] = p.first + sum[i] * sum[n] - sum[i] * sum[i]; if(j == k) ans = max(ans, f[i][j]); } } return ans; } int main(){ if(fopen("1.inp", "r")){ freopen("1.inp", "r", stdin); } scanf("%d%d", &n, &k); vector < int > x(1, 0); for(int i = 1; i <= n; ++i){ scanf("%d", arr + i); x.push_back(arr[i]); } vector < int > lst; long long ans = solve(x); int pos = 0; for(int i = 1; i < n; ++i){ if(f[i][k] == ans){ pos = i; } } for(int j = k; j >= 1; --j){ lst.push_back(pos); pos = pre[pos][j]; } reverse(lst.begin(), lst.end()); printf("%lld\n", ans); for(int x : lst){ printf("%d ", x); } return 0; }

컴파일 시 표준 에러 (stderr) 메시지

sequence.cpp: In function 'int main()':
sequence.cpp:180:31: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
   freopen("1.inp", "r", stdin);
                               ^
sequence.cpp:183:23: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d%d", &n, &k);
                       ^
sequence.cpp:187:23: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", arr + i);
                       ^
#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...