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 st first
#define nd second
#define mp make_pair
#define pb push_back
#define mod 1000000007
#define N 100005
using namespace std;
typedef long long ll;
typedef pair < int , ll > ii;
ll dp[N][205];
int git[N][205], pre[N], a[N], n, k, bas, son;
pair < int , ii > q[N];
inline double kesisim(ii a, ii b){return 1.0 * ( b.nd - a.nd ) / (1.0 * (a.st - b.st)) ;}
void ekle(ll i, ii d){
if(son - bas + 1 >= 1 and d.st == q[son].nd.st){
if(d.nd < q[son].nd.nd)
return;
else
son--;
}
while(son - bas + 1 >= 2){
ii d1 = q[son].nd;
ii d2 = q[son - 1].nd;
if(kesisim(d, d2) <= kesisim(d1, d2))
son--;
else
break;
}
q[++son] = mp(i, d);
}
ll bul(ll x){
while(son - bas + 1 >= 2){
ii d1 = q[bas].nd;
ii d2 = q[bas + 1].nd;
if(kesisim(d1, d2) <= x)
bas++;
else
break;
}
return q[bas].st;
}
int main() {
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
scanf("%d %d",&n ,&k);
for(int i = 1; i <= n; i++){
scanf("%d",a + i);
pre[i] = pre[i - 1] + a[i];
}
for(int x = 1; x <= k; x++){
bas = 1, son = 0;
ekle(0, mp(0, 0));
for(int i = 1; i <= n; i++){
ll j = bul(pre[i]);
dp[i][x] = dp[j][x - 1] + 1ll*pre[j]*pre[i] - 1ll*pre[j]*pre[j];
git[i][x] = j;
ekle(i, mp(pre[i], dp[i][x - 1] - 1ll*pre[i]*pre[i]) );
}
}
printf("%lld\n", dp[n][k]);
son = n;
bas = k;
// if(dp[n][k] == 0){
// for(int i = 1; i <= k; i++)
// printf("%d ", i);
// return 0;
// }
for(ll i = 1; i <= k; i++){
printf("%d ", git[son][bas]);
son = git[son][bas];
bas--;
}
return 0;
}
Compilation message (stderr)
sequence.cpp: In function 'int main()':
sequence.cpp:52:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d",&n ,&k);
~~~~~^~~~~~~~~~~~~~~~
sequence.cpp:54:8: 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... |