# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
240617 | mhy908 | Feast (NOI19_feast) | C++14 | 186 ms | 12092 KiB |
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;
typedef long long LL;
typedef pair<LL, LL> pll;
const LL llinf=10000000000000;
int n, k;
LL arr[300010], dp[300010][2], cnt[300010][2];
int solve(LL x){
for(int i=1; i<=n; i++){
if(pll(dp[i-1][0], -cnt[i-1][0])>pll(dp[i-1][1]-x, -cnt[i-1][1]))dp[i][0]=dp[i-1][0]+arr[i], cnt[i][0]=cnt[i-1][0];
else dp[i][0]=dp[i-1][1]+arr[i]-x, cnt[i][0]=cnt[i-1][1]+1;
if(pll(dp[i-1][0], -cnt[i-1][0])>pll(dp[i-1][1], -cnt[i-1][1]))dp[i][1]=dp[i-1][0], cnt[i][1]=cnt[i-1][0];
else dp[i][1]=dp[i-1][1], cnt[i][1]=cnt[i-1][1];
}
if(pll(dp[n][0], -cnt[n][0])>pll(dp[n][1], -cnt[n][1]))return cnt[n][0];
return cnt[n][1];
}
int main(){
dp[0][0]=-llinf, dp[0][1]=0;
scanf("%d %d", &n, &k);
for(int i=1; i<=n; i++){
scanf("%lld", &arr[i]);
arr[i]*=2;
}
LL l=0, r=llinf;
while(l<r){
LL mid=(l+r)/2;
if(solve(mid*2+1)>k)l=mid+1;
else r=mid;
}
solve(l*2);
printf("%lld", max(dp[n][0], dp[n][1])/2+l*k);
}
Compilation message (stderr)
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |