5558 | 2023-07-26 13:46:27 | marcipan5000 | Majomház | cpp17 | Accepted 100/100 | 1.225s | 24776 KiB |
#include <bits/stdc++.h>
using namespace std;
#define int long long int
int n,k;
int t[100010];
int ans;
vector<int> dp;
deque<int> st;
vector<int> sums;
vector<int> db;
int sum(int x,int y) {
return(sums[y]-sums[x-1]);
}
int a,b;
int log_search(int x,int y) {
if (x==y) {
return(x);
}
int koz=(x+y)/2;
if ( dp[a]+sum(a+1,koz)*(koz-a) <= dp[b]+sum(b+1,koz)*(koz-b) ) {
return(log_search(koz+1,y));
} else {
return(log_search(x,koz));
}
}
int solve(int l) {
int kor,kes;
dp.assign(n+1,0);
db.assign(n+1,0);
st.clear();
dp[0]=0;
db[0]=0;
st.push_front(0);
for (int i=1;i<=n;i++) {
while ((st.size()>=2) && (dp[st[0]]+sum(st[0]+1,i)*(i-st[0]) >= dp[st[1]]+sum(st[1]+1,i)*(i-st[1]) )) {
//cout << st[0] << " " << dp[st[0]] << " " << st[1] << " " << st[st[1]] << " " << sum(st[0]+1,i) << " " << sum(st[1]+1,i) << endl;
st.pop_front();
}
dp[i]=l+sum(st[0]+1,i)*(i-st[0])+dp[st[0]];
db[i]=db[st[0]]+1;
while (i<n) {
if (st.size()<=1) {
break;
}
a=st[st.size()-1]; b=i;
kes=log_search(b+1,n+1);
a=st[st.size()-2]; b=st[st.size()-1];
kor=log_search(b+1,n+1);
if (kes<=kor) {
st.pop_back();
} else {
break;
}
}
st.push_back(i);
}
ans=dp[n]-db[n]*l;
/*
if (k==db[n]-1) {
for (int i=0;i<=n;i++) {
cout << dp[i] << " " << db[i] << endl;
}
}
*/
return(db[n]-1);
}
int bin_ker(int x,int y) {
int z=solve((x+y)/2);
if (z==k) {
return(ans);
}
if (z<k) {
return(bin_ker(x,(x+y)/2-1));
} else {
return(bin_ker((x+y)/2+1,y));
}
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> k;
for (int i=1;i<=n;i++) {
cin >> t[i];
}
sums.assign(n+1,0);
for (int i=1;i<=n;i++) {
sums[i]=sums[i-1]+t[i];
}
cout << bin_ker(0,1e17) << endl;
return(0);
}
Subtask | Sum | Test | Verdict | Time | Memory | ||
---|---|---|---|---|---|---|---|
subtask1 | 0/0 | ||||||
1 | Accepted | 3ms | 1840 KiB | ||||
2 | Accepted | 25ms | 2328 KiB | ||||
subtask2 | 10/10 | ||||||
3 | Accepted | 3ms | 2212 KiB | ||||
4 | Accepted | 3ms | 2344 KiB | ||||
5 | Accepted | 3ms | 2564 KiB | ||||
6 | Accepted | 3ms | 2652 KiB | ||||
7 | Accepted | 3ms | 2740 KiB | ||||
subtask3 | 10/10 | ||||||
8 | Accepted | 4ms | 3020 KiB | ||||
9 | Accepted | 4ms | 3236 KiB | ||||
10 | Accepted | 4ms | 3220 KiB | ||||
11 | Accepted | 4ms | 3368 KiB | ||||
12 | Accepted | 4ms | 3448 KiB | ||||
subtask4 | 20/20 | ||||||
13 | Accepted | 16ms | 3776 KiB | ||||
14 | Accepted | 17ms | 4100 KiB | ||||
15 | Accepted | 20ms | 4028 KiB | ||||
16 | Accepted | 28ms | 4224 KiB | ||||
17 | Accepted | 32ms | 4480 KiB | ||||
18 | Accepted | 39ms | 4620 KiB | ||||
subtask5 | 29/29 | ||||||
19 | Accepted | 160ms | 7644 KiB | ||||
20 | Accepted | 246ms | 8436 KiB | ||||
21 | Accepted | 272ms | 8420 KiB | ||||
22 | Accepted | 328ms | 8948 KiB | ||||
23 | Accepted | 384ms | 9220 KiB | ||||
subtask6 | 31/31 | ||||||
24 | Accepted | 32ms | 13072 KiB | ||||
25 | Accepted | 131ms | 14108 KiB | ||||
26 | Accepted | 163ms | 14848 KiB | ||||
27 | Accepted | 193ms | 15636 KiB | ||||
28 | Accepted | 230ms | 16200 KiB | ||||
29 | Accepted | 298ms | 16908 KiB | ||||
30 | Accepted | 412ms | 17832 KiB | ||||
31 | Accepted | 479ms | 18512 KiB | ||||
32 | Accepted | 541ms | 19336 KiB | ||||
33 | Accepted | 694ms | 19960 KiB | ||||
34 | Accepted | 783ms | 20596 KiB | ||||
35 | Accepted | 883ms | 21220 KiB | ||||
36 | Accepted | 981ms | 21892 KiB | ||||
37 | Accepted | 941ms | 22464 KiB | ||||
38 | Accepted | 1.215s | 23172 KiB | ||||
39 | Accepted | 1.116s | 24140 KiB | ||||
40 | Accepted | 1.225s | 24776 KiB |