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<iostream>
#include<ctime>
#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<climits>
#include<cstring>
#include<iomanip>
#include<string>
#include<bitset>
#include<unordered_map>
#include<unordered_set>
#include<set>
#include<vector>
#include<map>
#include<queue>
#include<stack>
#include<deque>
#include<algorithm>
#include<functional>
#include<chrono>
//#include<windows.h>
//#include<direct.h>
#include<random>
#include<sstream>
#define y0 asdahsdlkahsdad
#define y1 aasdfasdfasdf
#define yn askfhwqriuperikldjk
#define j1 assdgsdgasghsf
#define taskname "sequence"
//#define GuiltiaSinJurai
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
using namespace std;
template <typename T> inline void Cin(T &x)
{
register char c;
for (c = getchar(); c < '0' || c > '9'; c = getchar());
for (x = 0; c >= '0' && c <= '9'; c = getchar())
x = x * 10 + c - '0';
}
const int maxN = 1e5, maxK = 201;
int trace[maxN][maxK], s[maxN], nId, N, K, a;
static ll f[maxN][maxK];
struct TLine
{
int x;
ll y;
inline TLine operator - (const TLine &l) const & { return {x - l.x, y - l.y}; }
inline bool operator * (const TLine &l) const & { return x * l.y <= y * l.x; }
} p[maxN];
int id[maxN];
inline bool Erase(int a, int b, int c)
{
return (p[b] - p[a]) * (p[a] - p[c]);
}
int main()
{
#ifdef GuiltiaSinJurai
auto start = chrono::steady_clock::now();
#endif
ios_base::sync_with_stdio(0);
cin.tie(0);
//freopen(taskname".inp", "r", stdin);
//freopen(taskname".out", "w", stdout);
Cin(N); Cin(K);
for (int i = 0, val = 0; i < N; ++i)
Cin(a), s[i] = (val += a);
for (int k = 1; k <= K; ++k)
{
nId = -1;
int j = 0;
for (int i = k, val = f[i][k - 1]; i < N; ++i)
{
--i;
p[i] = {s[i], f[i][k - 1] - (ll)s[i] * s[i]};
while (nId >= 1 && Erase(id[nId - 1], id[nId], i)) --nId;
id[++nId] = i;
val = f[++i][k - 1];
int t = s[i];
#define Get(i) ((ll)t * p[i].x + p[i].y)
if (j >= nId) j = nId;
else while (j < nId && Get(id[j + 1]) > Get(id[j])) ++j;
f[i][k] = Get(id[j]);
trace[i][k] = id[j];
}
}
cout << f[N - 1][K] << '\n';
int i = N - 1;
for (; K > 0; --K)
cout << (i = trace[i][K]) + 1 << ' ';
#ifdef GuiltiaSinJurai
auto end = chrono::steady_clock::now();
cerr << "In milliseconds : "
<< chrono::duration_cast<chrono::milliseconds>(end - start).count();
cerr << '\n' << "In seconds : "
<< chrono::duration_cast<chrono::seconds>(end - start).count() << '\n';
#endif
return 0;
}
Compilation message (stderr)
sequence.cpp: In function 'int main()':
sequence.cpp:83:25: warning: variable 'val' set but not used [-Wunused-but-set-variable]
for (int i = k, val = f[i][k - 1]; i < N; ++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... |