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 <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <algorithm>
#include <algorithm>
#include <deque>
#define X first
#define Y second
#define PB push_back
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair < ll, ll > pll;
const int N = 1e5 + 500;
const int K = 215;
inline bool ccw(const pll &A, const pll &B, const pll &C){
return (ld)A.X * (B.Y - C.Y) + (ld)B.X * (C.Y - A.Y) + (ld)C.X * (A.Y - B.Y) <= 0;
}
int ind[N], a[N];
pll hl[N];
int cur = 0, n, k;
ll P[N], lst[N], dp[N];
int rek2[N], cur_k, sz, cnt[N], rek[N][K];
ll cost, lo = 0, hi = 1e17;
inline void add_2(const pll &X,const int &i){
for(;sz >= 2 && ccw(X, hl[sz - 1], hl[sz - 2]); sz--);
hl[sz] = X; ind[sz++] = i;
cur = min(cur, sz - 1);
}
inline ll get_2(const int &i,const ll &x){
return hl[i].X * x + hl[i].Y;
}
inline int query_2(const ll &x){
if(sz == 0) return -1;
for(; cur + 1 < sz && get_2(cur, x) < get_2(cur + 1, x); cur++);
return cur;
}
inline int dinamika_2(){
sz = 0; cur = 0;
add_2({0, 0}, 0);
for(int i = 1;i <= n;i++){
int r = query_2(P[i]);
rek2[i] = ind[r];
cnt[i] = cnt[ind[r]] + 1;
dp[i] = get_2(r, P[i]) - cost;
add_2({P[i], dp[i] - P[i] * P[i]}, i);
//printf("%lld ", dp[i]);
}
// printf("\n");
cur_k++;
return cnt[n] <= k;
}
inline void add(const pll &X,const int &i){
for(;sz >= 2 && ccw(X, hl[sz - 1], hl[sz - 2]); sz--);
hl[sz] = X; ind[sz++] = i;
cur = min(cur, sz - 1);
}
inline ll get(const int &i,const ll &x){
return hl[i].X * x + hl[i].Y;
}
inline int query(const ll &x){
if(sz == 0) return -1;
for(; cur + 1 < sz && get(cur, x) < get(cur + 1, x); cur++);
return cur;
}
inline void dinamika(){
sz = 0; cur = 0;
for(int i = 0;i < n;i++){
int r = query(P[i]);
dp[i] = -1;
if(r >= 0){
rek[i][cur_k] = ind[r];
dp[i] = get(r, P[i]);
}
if(lst[i] >= 0)
add({P[i], lst[i] - P[i] * P[i]}, i);
lst[i] = dp[i];
}
cur_k++;
}
int main(){
scanf("%d%d", &n, &k);
if(n > 10000) {
for(int i = 0;i < n;i++){
scanf("%d", a + i);
P[i + 1] = a[i] + P[i];
}
//lo = hi = 12;
cost = (lo + hi) / 2;
for(int i = 0;i < 60;i++){
if(dinamika_2()){
hi = cost;
}
else{
lo = cost;
}
cost = (lo + hi) / 2;
//if(lo == hi) break;
}
printf("%lld\n", dp[n] + (ll)cnt[n] * cost);
vector < int > fin;
int cur = n;
for(int i = 0;i < k;i++){
cur = rek2[cur];
fin.PB(cur);
}
reverse(fin.begin(), fin.end());
for(int x : fin)
printf("%d ", x);
printf("\n");
}
else{
//if(n > 50000 && k > 150) return 0;
for(int i = 0;i < n;i++){
scanf("%d", a + i);
P[i] = a[i] + (i ? P[i - 1] : 0);
}
for(int i = 0;i < k;i++)
dinamika();
printf("%lld\n", dp[n - 1]);
vector < int > fin;
int cur = n - 1;
for(int i = k - 1;i >= 0;i--){
fin.PB(rek[cur][i]);
cur = fin.back();
}
reverse(fin.begin(), fin.end());
for(int x : fin)
printf("%d ", x + 1);
printf("\n");
}
}
Compilation message (stderr)
sequence.cpp: In function 'int main()':
sequence.cpp:102:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &n, &k);
~~~~~^~~~~~~~~~~~~~~~
sequence.cpp:106:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", a + i);
~~~~~^~~~~~~~~~~~~
sequence.cpp:136:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", a + 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... |