This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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];
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, int pos)
{
if (pointer >= U.size()) pointer = U.size()-1;
while (pointer < U.size()-1 && gett(pointer, val) <= gett(pointer+1, val) && ID[pointer+1] < pos)
pointer++;
return mp(gett(pointer, val), ID[pointer]);
}
void xoa()
{
U.clear();
V.clear();
ID.clear();
}
}CHT[2];
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
//freopen("INP.TXT", "r", stdin);
//freopen("OUT.TXT", "w", stdout);
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 j = 1; j <= k+1; j++)
{
for (int i = j; i <= n; i++)
{
pair<long long, int> z = CHT[0].get(2*s[i], i);
dp[i] = s[n]*s[i] - s[i]*s[i] + z.F;
tr[i][j] = z.S;
CHT[1].add(s[i], dp[i] - s[n]*s[i] - s[i]*s[i], i);
}
swap(CHT[0], CHT[1]);
CHT[0].pointer = 0;
CHT[1].xoa();
}
int i = n, j = k+1;
cout <<dp[n]/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, 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) && ID[pointer+1] < pos)
~~~~~~~~^~~~~~~~~~~~
sequence.cpp: In function 'int main()':
sequence.cpp:86:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < res.size()-1; i++)
~~^~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |