Submission #941276

#TimeUsernameProblemLanguageResultExecution timeMemory
941276Zero_OPSplit the sequence (APIO14_sequence)C++14
100 / 100
653 ms83152 KiB
#include<bits/stdc++.h> using namespace std; using int64 = int64_t; #define REP(i, n) for(int i = 0, _n = n; i < _n; ++i) #define REPD(i, n) for(int i = n - 1; i >= 0; --i) #define FOR(i, l, r) for(int i = l, _r = r; i <= _r; ++i) #define FORD(i, r, l) for(int i = r, _l = l; i >= _l; --i) #define left __left #define right __right #define prev __prev #define next __next #define div __div #define pb push_back #define pf push_front #define sz(v) (int)v.size() #define range(v) begin(v), end(v) #define compact(v) v.erase(unique(range(v)), end(v)) #define debug(v) "[" #v " = " << (v) << "]" template<typename T> bool minimize(T& a, const T& b){ if(a > b){ a = b; return true; } return false; } template<typename T> bool maximize(T& a, const T& b){ if(a < b){ a = b; return true; } return false; } template<int dimension, class T> struct vec : public vector<vec<dimension - 1, T>> { static_assert(dimension > 0, "Dimension must be positive !\n"); template<typename... Args> vec(int n = 0, Args... args) : vector<vec<dimension - 1, T>>(n, vec<dimension - 1, T>(args...)) {} }; template<class T> struct vec<1, T> : public vector<T> { vec(int n = 0, T val = T()) : vector<T>(n, val) {} }; void init(void); void process(void); int main(){ ios_base::sync_with_stdio(0); cin.tie(0); #define task "antuvu" if(fopen(task".inp", "r")){ freopen(task".inp", "r", stdin); freopen(task".out", "w", stdout); } int T = 1; //cin >> T; while(T--) { init(); process(); } return 0; } struct line{ int64 a, b; int id; line(int64 a = 0, int64 b = 0, int id = 0) : a(a), b(b), id(id) {} int64 eval(int64 x){ return a * x + b; } }; bool bad(const line& d1, const line& d2, const line& d3){ return (d2.a - d3.a) * (d3.b - d1.b) <= (d3.b - d2.b) * (d1.a - d3.a); } struct ConvexHullTrick{ deque<line> dq; void add(line d){ while(sz(dq) > 1 && bad(dq[sz(dq) - 2], dq[sz(dq) - 1], d)){ dq.pop_back(); } dq.push_back(d); } pair<int64, int> query(int64 x){ if(dq.empty()) return make_pair(0, 0); while(sz(dq) > 1 and dq[0].eval(x) <= dq[1].eval(x)) dq.pop_front(); return make_pair(dq[0].eval(x), dq[0].id); } void reset(){ dq.clear(); add(line(0, 0, 0)); } }; const int MAX = 1e5 + 5; const int64 inf = 1e18; int n, k, trace[202][MAX]; int64 pref[MAX], a[MAX], dp[2][MAX]; void init(){ cin >> n >> k; FOR(i, 1, n) { cin >> a[i]; pref[i] = pref[i - 1] + a[i]; } } void process(){ bool next = true, prev = false; ConvexHullTrick trick; REP(j, k){ swap(next, prev); trick.reset(); FOR(i, 1, n){ pair<int64, int> cur = trick.query(pref[n] - pref[i]); dp[next][i] = cur.first + pref[i] * (pref[n] - pref[i]); trace[j][i] = cur.second; trick.add(line(-pref[i], dp[prev][i], i)); } } int64 res = -1; int cut = -1; FOR(i, 1, n){ if(res < dp[next][i]){ res = dp[next][i]; cut = i; } } vector<int> cuts; REPD(i, k){ cuts.pb(cut); cut = trace[i][cut]; } reverse(range(cuts)); cout << res << '\n'; REP(i, sz(cuts)) cout << cuts[i] << ' '; }

Compilation message (stderr)

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