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>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/priority_queue.hpp>
using namespace std;
using ll = long long;
using ld = long double;
using vi = vector<int>;
using pi = pair<int, int>;
using pll = pair<ll, ll>;
constexpr int INF = 0x3f3f3f3f; constexpr ll LLINF = 0x3f3f3f3f3f3f3f3f;
#ifdef __MINGW32__
inline int getchar_unlocked() { return _getchar_nolock(); }
inline int putchar_unlocked(int c) { return _putchar_nolock(c); }
#endif
#define gc getchar_unlocked()
#define pc(x) putchar_unlocked(x)
template<typename T> void scan(T &x){x = 0;bool _=0; T c=gc;_=c==45;c=_?gc:c;while(c<48||c>57)c=gc;for(;c<48||c>57;c=gc);for(;c>47&&c<58;c=gc)x=(x<<3)+(x<<1)+(c&15);x=_?-x:x;}
template<typename T> void printn(T n){bool _=0;_=n<0;n=_?-n:n;char snum[65];int i=0;do{snum[i++]=n%10+48;n/= 10;}while(n);--i;if (_)pc(45);while(i>=0)pc(snum[i--]);}
template<typename First, typename ... Ints> void scan(First &arg, Ints&... rest){scan(arg);scan(rest...);}
template<typename T> void print(T n){printn(n);pc(10);}
template<typename First, typename ... Ints> void print(First arg, Ints... rest){printn(arg);pc(32);print(rest...);}
#define db(x) { cerr << #x << " = " << x << endl; }
template <typename T> void _dbarr(T* a, size_t sz){ for(int i = 0; i < sz; i++) cerr << a[i] << " \n"[i == sz-1]; }
template <typename T> void _dbarr(vector<T> a, size_t sz){ for(int i = 0; i < sz; i++) cerr << a[i] << " \n"[i == sz-1]; }
#define dbarr(x, n) {cerr << #x << ": "; _dbarr((x),(n));}
#define pb push_back
#define eb emplace_back
const int MM = 1e5+5;
int n,k,par[205][MM];
ll a[MM], pre[MM], cur[MM];
ld slope(int i, int j)
{
if(a[i] == a[j]) return -INF;
return (ld)((pre[i]-a[i]*a[i])-(pre[j] - a[j]*a[j]))/(a[j]-a[i]);
}
int main()
{
cin.tie(0)->sync_with_stdio(0);
cin >> n >> k;
for(int i = 1; i <= n; i++)
cin >> a[i], a[i] += a[i-1];
for(int j = 1; j <= k; j++)
{
deque<int> q;
q.push_back(0);
for(int i = 1; i <= n; i++)
{
while(q.size() >= 2 && slope(q[0], q[1]) <= a[i]) q.pop_front();
cur[i] = pre[q[0]]+a[q[0]]*(a[i]-a[q[0]]);
par[j][i] = q[0];
while(q.size() >= 2 && slope(q[q.size()-2], q[q.size()-1]) >= slope(q[q.size()-1], i)) q.pop_back();
q.push_back(i);
}
copy(cur, cur+n,pre);
}
cout << cur[n] << "\n";
stack<int> st;
int i = n, j = k;
while(j >= 1)
{
i = par[j][i];
j--;
st.push(i);
}
while(!st.empty())
{
cout << st.top() << " ";
st.pop();
}
cout << "\n";
}
# | 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... |