P2034 选择数字
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 选择数字的更多相关文章
- 「单调队列优化DP」P2034 选择数字
「单调队列优化DP」P2034 选择数字 题面描述: 给定一行n个非负整数a[1]..a[n].现在你可以选择其中若干个数,但不能有超过k个连续的数字被选择.你的任务是使得选出的数字的和最大. 输入格 ...
- P2034 选择数字——线性dp(单调队列优化)
选择数字 题目描述 给定一行 \(n\) 个非负整数 \(a[1]...a[n]\) .现在你可以选择其中若干个数,但不能有超过 \(k\) 个连续的数字被选择.你的任务是使得选出的数字的和最大. 输 ...
- P2034 选择数字 / P2627 [USACO11OPEN]Mowing the Lawn G
Link 题目描述 给定一行 \(n\) 个非负整数 \(a[1]..a[n]\) .现在你可以选择其中若干个数,但不能有超过 \(k\) 个连续的数字被选择.你的任务是使得选出的数字的和最大. 输入 ...
- codevs 3327 选择数字
3327 选择数字 时间限制: 1 s 空间限制: 256000 KB 题目描述 Description 给定一行n个非负整数a[1]..a[n].现在你可以选择其中若干个数,但不能有超过k个连续 ...
- input只读属性 设置和移除 选择数字
设置只读属性 $('#stage').attr("readonly", "readonly"); 移除 只读属性 $("input").r ...
- codevs3327选择数字(单调队列优化)
3327 选择数字 时间限制: 1 s 空间限制: 256000 KB 题目等级 : 钻石 Diamond 题目描述 Description 给定一行n个非负整数a[1]..a[n].现 ...
- 科普:为什么 String hashCode 方法选择数字31作为乘子
作者:coolblog 此文章转载自:https://segmentfault.com/a/1190000010799123 1. 背景 某天,我在写代码的时候,无意中点开了 String hashC ...
- 选择数字(codevs 3327)
题目描述 Description 给定一行n个非负整数a[1]..a[n].现在你可以选择其中若干个数,但不能有超过k个连续的数字被选择.你的任务是使得选出的数字的和最大. 输入描述 Input De ...
- asp.net中选择数字时,另外的数字同时发生变化(适用dev控件)
关键: <ClientSideEvents ValueChanged="AgioChanged" /> <div class="col-sm-4 ...
随机推荐
- warning C4996: 'strcpy': This function or variable may be unsafe.
mkdir 写成 _mkdir strcpy 写成为 strcpy_s 或是在项目处右击-->属性-->C/C++-->预处理器-->在预处理器定义后添加";_CR ...
- XMLHttpRequest.withCredentials
https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/withCredentials var xhr = new XMLHtt ...
- MySQL的basedir
C:\Documents and Settings\All Users\Application Data\MySQL\MySQL Server 5.7\Data
- JavaScript浏览器历史的语法小问题
https://www.w3schools.com/jsref/met_his_back.asp This is the same as clicking the "Back button& ...
- [wiki]陶德曼调停
陶德曼调停[编辑] 维基百科,自由的百科全书 凯申物流差点和谈 目录 1背景 2调停经过 3评价 4参见 背景[编辑] 主条目:中德合作 (1911年-1941年) 1936年11月25日,德国与日本 ...
- Centos6.5下VNC Server配置
整个流程如下~ 1.首先,检查下本机是否安装了vnc: 提示没有安装,那么开始装吧: 2.先关闭防火墙 查看防火墙状态: 关闭防火墙: 注:要切到root下 [carsonzhu@localhost ...
- D3.js & Data Visualization & SVG
D3.js & Data Visualization & SVG https://davidwalsh.name/learning-d3 // import {scaleLinear} ...
- 【转】Thread Local的正确原理与适用场景
本文转发自技术世界,原文链接 http://www.jasongj.com/java/threadlocal/ ThreadLocal解决什么问题 由于 ThreadLocal 支持范型,如 Thre ...
- Java之数组遍历
package basic; //数组遍历方法 public class ForEach { public static void main(String[] args) { // 原始数组 Stri ...
- cordic——sincos
phase format :scaled radians,归化到多少pi roundmode :nearest even 近似值 coarse rotation: selected-pi——pi.no ...