P2034 选择数字

题目描述

给定一行n个非负整数a[1]..a[n]。现在你可以选择其中若干个数,但不能有超过k个连续的数字被选择。你的任务是使得选出的数字的和最大。


错误日志: longlong 的 \(inf\) 没有设为 0xfffffffffffffff


Solution

正难则反

正难则反

复习看到了双倍经验就回来看看

求最大值有点难, 转化为求最小去除值

设 \(dp[i]\) 为 \(i\) 为断点, 考虑到 \(i\) 处的最小去除值

发现每隔 \(K + 1\) 必有一个断点

所以 \(dp[i] = \min_{j = i - k - 1}^{i - 1}dp[j] + a[i]\)

然后单调队列优化, 答案在区间 \([n - K, n]\) 内

总值减一下即可

Code

#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#include<algorithm>
#include<climits>
#define LL long long
#define REP(i, x, y) for(LL i = (x);i <= (y);i++)
using namespace std;
LL RD(){
LL out = 0,flag = 1;char c = getchar();
while(c < '0' || c >'9'){if(c == '-')flag = -1;c = getchar();}
while(c >= '0' && c <= '9'){out = out * 10 + c - '0';c = getchar();}
return flag * out;
}
const LL maxn = 200019, inf = 0xfffffffffffffff;
LL num, K;
LL a[maxn], sum;
LL dp[maxn];
struct Que{
LL Index, val;
}Q[maxn];
LL head = 1, tail;
void push_back(LL Index, LL val){
while(head <= tail && Q[tail].val >= val)tail--;
Q[++tail] = (Que){Index, val};
}
LL get_min(LL p){
while(head <= tail && p - Q[head].Index > K + 1)head++;
return Q[head].val;
}
void init(){
num = RD(), K = RD();
REP(i, 1, num)a[i] = RD(), sum += a[i];
memset(dp, 127, sizeof(dp));
push_back(0, 0);
}
void solve(){
REP(i, 1, num){
dp[i] = get_min(i) + a[i];
push_back(i, dp[i]);
}
LL ans = inf;
REP(i, num - K, num)ans = min(ans, dp[i]);
printf("%lld\n", sum - ans);
return ;
REP(i, 1, num)printf("%lld ", dp[i]);
}
int main(){
init();
solve();
return 0;
}

P2034 选择数字的更多相关文章

  1. 「单调队列优化DP」P2034 选择数字

    「单调队列优化DP」P2034 选择数字 题面描述: 给定一行n个非负整数a[1]..a[n].现在你可以选择其中若干个数,但不能有超过k个连续的数字被选择.你的任务是使得选出的数字的和最大. 输入格 ...

  2. P2034 选择数字——线性dp(单调队列优化)

    选择数字 题目描述 给定一行 \(n\) 个非负整数 \(a[1]...a[n]\) .现在你可以选择其中若干个数,但不能有超过 \(k\) 个连续的数字被选择.你的任务是使得选出的数字的和最大. 输 ...

  3. P2034 选择数字 / P2627 [USACO11OPEN]Mowing the Lawn G

    Link 题目描述 给定一行 \(n\) 个非负整数 \(a[1]..a[n]\) .现在你可以选择其中若干个数,但不能有超过 \(k\) 个连续的数字被选择.你的任务是使得选出的数字的和最大. 输入 ...

  4. codevs 3327 选择数字

    3327 选择数字  时间限制: 1 s  空间限制: 256000 KB 题目描述 Description 给定一行n个非负整数a[1]..a[n].现在你可以选择其中若干个数,但不能有超过k个连续 ...

  5. input只读属性 设置和移除 选择数字

    设置只读属性 $('#stage').attr("readonly", "readonly"); 移除 只读属性  $("input").r ...

  6. codevs3327选择数字(单调队列优化)

    3327 选择数字  时间限制: 1 s  空间限制: 256000 KB  题目等级 : 钻石 Diamond     题目描述 Description 给定一行n个非负整数a[1]..a[n].现 ...

  7. 科普:为什么 String hashCode 方法选择数字31作为乘子

    作者:coolblog 此文章转载自:https://segmentfault.com/a/1190000010799123 1. 背景 某天,我在写代码的时候,无意中点开了 String hashC ...

  8. 选择数字(codevs 3327)

    题目描述 Description 给定一行n个非负整数a[1]..a[n].现在你可以选择其中若干个数,但不能有超过k个连续的数字被选择.你的任务是使得选出的数字的和最大. 输入描述 Input De ...

  9. asp.net中选择数字时,另外的数字同时发生变化(适用dev控件)

      关键: <ClientSideEvents ValueChanged="AgioChanged" />   <div class="col-sm-4 ...

随机推荐

  1. 第三个sprint冲刺第二阶段

    内测版:

  2. MySQLi面向对象实践--insert、update、delete

    执行insert <?php $mysqli = new Mysqli(); $mysqli->connect("localhost","root" ...

  3. Java微信二次开发(五)

    消息加密 需要到入库:commons-io-2.4.jar,commons-codec-1.9.jar(在官网的Java微信加密demo下) 第一步:访问https://mp.weixin.qq.co ...

  4. C#.字符串转数组,数组转字符串

    string str = "1,2,3,4,5,6,7"; string[] strArray = str.Split(','); //字符串转数组 str = string.Em ...

  5. BZOJ4530 BJOI2014大融合(线段树合并+并查集+dfs序)

    易知所求的是两棵子树大小的乘积.先建出最后所得到的树,求出dfs序和子树大小.之后考虑如何在动态加边过程中维护子树大小.这个可以用树剖比较简单的实现,但还有一种更快更优美的做法就是线段树合并.对每个点 ...

  6. echarts之简单的入门——【一】做个带时间轴的柱状统计图

    百度Echarts 官网首页  http://echarts.baidu.com/ 配置项手册 http://echarts.baidu.com/option.html#title GL配置项手册 h ...

  7. wamp下php报错session_start(): open(d:/wamp/tmp\sess_ku776hvb06ko4lv9d11e7mnfj1, O_RDWR) failed: No such file or directory

    报错提示 session_start(): open(d:/wamp/tmp\sess_ku776hvb06ko4lv9d11e7mnfj1, O_RDWR) failed: No such file ...

  8. Problem B. Harvest of Apples HDU - 6333(莫队)

    Problem Description There are n apples on a tree, numbered from 1 to n.Count the number of ways to p ...

  9. QAU 18校赛 J题 天平(01背包 判断能否装满)

    问题 J: 天平 时间限制: 1 Sec  内存限制: 128 MB提交: 36  解决: 9[提交][状态][讨论版][命题人:admin] 题目描述 天平的右端放着一件重量为w的物品.现在有n个重 ...

  10. UVa - 12050

    A palindrome is a word, number, or phrase that reads the same forwards as backwards. For example,the ...