Submission #46530

#TimeUsernameProblemLanguageResultExecution timeMemory
46530tieunhiSplit the sequence (APIO14_sequence)C++14
0 / 100
2 ms512 KiB
#include <bits/stdc++.h> #define pii pair<int, int> #define mp make_pair #define F first #define S second #define PB push_back #define N 100005 #define maxc 1000000007 using namespace std; int n, k, a[N], tr[N][202]; long long s[N], dp[N][202]; struct ConvexHullTrick { vector<long long> U; vector<long long> V; vector<int> ID; int pointer = 0; bool bad(int l1, int l2, int l3) { return (V[l3] - V[l1])*(U[l1] - U[l2]) < (V[l2] - V[l1])*(U[l1] - U[l3]); } void add(long long u, long long v, int id) { U.PB(u); V.PB(v); ID.PB(id); while (U.size() >= 3 && bad(U.size()-3, U.size()-2, U.size()-1)) { U.erase(U.end()-1); V.erase(V.end()-1); ID.erase(ID.end()-1); } } long long gett(int pos, long long val) { return U[pos]*val + V[pos]; } pair<long long, int> get(long long val) { if (pointer >= U.size()) pointer = U.size()-1; while (pointer < U.size()-1 && gett(pointer, val) < gett(pointer+1, val)) pointer++; return mp(gett(pointer, val), ID[pointer]); } }CHT[202]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); //freopen("INP.TXT", "r", stdin); cin >> n >> k; for (int i = 1; i <= n; i++) cin >> a[i], s[i] = s[i-1] + a[i]; CHT[0].add(0, 0, 0); for (int i = 1; i <= n; i++) for (int j = 1; j <= min(i, k+1); j++) { pair<long long, int> z = CHT[j-1].get(2*s[i]); dp[i][j] = s[n]*s[i] - s[i]*s[i] + z.F; tr[i][j] = z.S; CHT[j].add(s[i], dp[i][j] - s[n]*s[i] - s[i]*s[i], i); } int i = n, j = k+1; cout <<dp[n][k+1]/2<<'\n'; vector<int> res; while (i != 0) { res.PB(i); i = tr[i][j--]; } reverse(res.begin(), res.end()); for (int i = 0; i < res.size()-1; i++) cout <<res[i]<<" "; }

Compilation message (stderr)

sequence.cpp: In member function 'std::pair<long long int, int> ConvexHullTrick::get(long long int)':
sequence.cpp:42:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         if (pointer >= U.size()) pointer = U.size()-1;
             ~~~~~~~~^~~~~~~~~~~
sequence.cpp:43:24: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         while (pointer < U.size()-1 && gett(pointer, val) < gett(pointer+1, val))
                ~~~~~~~~^~~~~~~~~~~~
sequence.cpp: In function 'int main()':
sequence.cpp:74:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 0; i < res.size()-1; 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...