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>
using namespace std;
#define fi first
#define se second
#define gcd __gcd
#define sz(v) (int) v.size()
#define pb push_back
#define pi pair<int,int>
#define all(v) (v).begin(), (v).end()
#define compact(v) (v).erase(unique(all(v)), (v).end())
#define FOR(i, a, b) for(int i = (a); i <= (b); i++)
#define dbg(x) "[" #x " = " << (x) << "]"
///#define int long long
using ll = long long;
using ld = long double;
using ull = unsigned long long;
template<typename T> bool ckmx(T& a, const T& b){if(a <= b) return a = b, true; return false;}
template<typename T> bool ckmn(T& a, const T& b){if(a >= b) return a = b, true; return false;}
const int N = 1e5+1;
const int K = 201;
const ll INF = 1e18;
struct Line{
ld a,b; int id;
Line(ld a = 0, ld b = 0, int id = 0): a(a), b(b), id(id) {}
ld intersect(Line& x){return (x.b - b)/(a - x.a);}
bool operator < (const Line& x) const{return a < x.a;}
};
struct CHT{
deque<pair<Line,ld>> dq;
void ins(Line x){
while(!dq.empty()){
if(dq.back().first.a != x.a){
if(dq.back().second >= dq.back().first.intersect(x)) dq.pop_back();
else{
break;
}
}
else{
if(dq.back().first.b <= x.b) dq.pop_back();
else{
return;
}
}
}
if(dq.empty()){
dq.push_back(make_pair(x, -INF));
}
else dq.push_back(make_pair(x, dq.back().first.intersect(x)));
return;
}
Line get(ll x){
while(dq.size() > 1 && dq[1].second <= x) dq.pop_front();
dq.front().second = -INF;
return dq.front().first;
}
void rest(){while(dq.size()) dq.pop_back();}
};
int n, k, a[N], trace[K][N];
ll dp[2][N];
void solve()
{
cin >> n >> k;
for(int i = 1; i <= n; i++){
cin >> a[i];
a[i] += a[i-1];
}
CHT hull;
for(int i = 1; i <= k; i++){
for(int j = 1; j <= n; j++){
hull.ins(Line(a[j-1], dp[0][j-1] - 1ll*a[j-1] * a[j-1], j));
Line x = hull.get(a[j]);
ll aL = x.a, bL = x.b;
dp[1][j] = aL * (1ll*a[j]) + bL;
trace[i][j] = x.id;
}
swap(dp[0], dp[1]);
hull.rest();
}
cout << dp[0][n] <<"\n";
vector<int> path;
for(int cur_k = k, cur_pos = n; cur_k; cur_pos = trace[cur_k][cur_pos]-1, cur_k--){
path.push_back(trace[cur_k][cur_pos]-1);
}
reverse(all(path));
for(int i = 0; i < sz(path); i++) cout << path[i] <<" "; cout <<"\n";
return;
}
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
#define name "InvMOD"
if(fopen(name".INP", "r")){
freopen(name".INP","r",stdin);
freopen(name".OUT","w",stdout);
}
int t = 1; //cin >> t;
while(t--) solve();
return 0;
}
Compilation message (stderr)
sequence.cpp: In function 'void solve()':
sequence.cpp:106:5: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
106 | for(int i = 0; i < sz(path); i++) cout << path[i] <<" "; cout <<"\n";
| ^~~
sequence.cpp:106:62: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
106 | for(int i = 0; i < sz(path); i++) cout << path[i] <<" "; cout <<"\n";
| ^~~~
sequence.cpp: In function 'int main()':
sequence.cpp:119:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
119 | freopen(name".INP","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
sequence.cpp:120:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
120 | freopen(name".OUT","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# | 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... |