Submission #814126

#TimeUsernameProblemLanguageResultExecution timeMemory
814126TCLMinhSplit the sequence (APIO14_sequence)C++17
89 / 100
1772 ms87844 KiB
#include <iostream> #include <deque> #include <algorithm> #include <utility> #include <vector> #define int long long using namespace std; struct Line { int a = 0, b = 0; Line(int a, int b) :a(a), b(b) {} Line() {} const inline int operator()(int x) const { return a * x + b - x * x; } const bool operator<(const Line& other) const { return a == other.a ? b < other.b : a < other.a; } const inline int intersect(const Line& other) const { return (other.b - b) / (a - other.a) + ((other.b - b) % (a - other.a) != 0); } }; struct Node { int idx = 0; int itsPos = 0; Line line = Line(); Node(int itsPos, const Line& line, int idx) :itsPos(itsPos), line(line), idx(idx) {} Node(int itsPos) {} Node() {} const bool operator<(const Node& other) const { return itsPos < other.itsPos; } }; std::deque<Node> dq; void addLine(const Line& line, int idx) { while (!dq.empty()) { if (dq.back().line.a == line.a) { if (dq.back().line.b < line.b) dq.pop_back(); return; } if (dq.back().line.intersect(line) <= dq.back().itsPos) dq.pop_back(); else break; } if (dq.empty()) dq.push_back({ -99999999999999999LL,line,idx }); else dq.push_back({ dq.back().line.intersect(line),line, idx }); } void clearDeque() { while (!dq.empty()) dq.pop_back(); } pair<int, int> query(int x) { int l = 0, r = dq.size() - 1; int result = 0; int idx = 0; while (l <= r) { int mid = (l + r) / 2; if (result < dq[mid].line(x)) { result = dq[mid].line(x); idx = dq[mid].idx; } if (dq[mid].itsPos < x) { l = mid + 1; } else { r = mid - 1; } } return{ result,idx }; } int n, k; signed trace[201][100001]; int dp[2][100001]; Line pre[2][100001]; int a[100001]; int prx[100001]; void debug() { for (int i = 0; i <= k; i++) { for (int j = 1; j <= n; j++) { cerr << trace[i][j] << " "; } cerr << "\n"; } cerr << "\n"; } int sum(int l, int r) { return prx[r] - prx[l - 1]; } signed main() { cin >> n >> k; //k++; k--; for (int i = 1; i <= n; i++) { cin >> a[i]; prx[i] = prx[i - 1] + a[i]; } //maximize this dp: dp[t][i] = max(dp[t][i],dp[t - 1][j - 1] + sum(j,i) * sum(i + 1,n)); for (int i = 2; i <= n; i++) { dp[0][i] = prx[i - 1] * (prx[n] + prx[0]) - prx[i - 1] * prx[i - 1] - prx[0] * prx[n]; pre[0][i] = Line((prx[n] + prx[i - 1]), dp[0][i] - prx[i - 1] * prx[n]); } for (signed t = 1; t <= k; t++) { //cerr << t << "\n"; clearDeque(); for (signed i = t + 1; i <= n; i++) { //add previous one addLine(pre[(t-1)&1][i - 1], i - 1); //get pair<int,int> x = query(prx[i - 1]); int val = x.first; int idx = x.second; dp[t&1][i] = val; trace[t][i] = idx; //apply if (i < n) { pre[t&1][i] = Line(prx[n] + prx[i - 1], val - prx[i - 1] * prx[n]); } } /* for (auto x : dq) { cerr << x.itsPos << " " << x.line.a << "x " << x.line.b << " -x^2 \n"; } */ } vector<int> res; int result = 0, idx = 0; for (int i = n; i >= 1; i--) { if (result < dp[k&1][i]) { result = dp[k&1][i]; idx = i; } } res.push_back(idx-1); for (int i = k; i >= 1; i--) { res.push_back(trace[i][idx] - 1); idx = trace[i][idx]; } //debug(); cout << result << '\n'; sort(res.begin(), res.end()); int id = 1; for (auto x : res) { if (x < 0 || x < id) { cout << id++ << " "; }else cout << x << " "; } return 0; }

Compilation message (stderr)

sequence.cpp: In constructor 'Node::Node(long long int, const Line&, long long int)':
sequence.cpp:35:10: warning: 'Node::line' will be initialized after [-Wreorder]
   35 |     Line line = Line();
      |          ^~~~
sequence.cpp:33:9: warning:   'long long int Node::idx' [-Wreorder]
   33 |     int idx = 0;
      |         ^~~
sequence.cpp:36:5: warning:   when initialized here [-Wreorder]
   36 |     Node(int itsPos, const Line& line, int idx) :itsPos(itsPos), line(line), idx(idx) {}
      |     ^~~~
#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...