Educational Codeforces Round 69 D. Yet Another Subarray Problem
Educational Codeforces Round 69 (Rated for Div. 2) D. Yet Another Subarray Problem
题意:
求\(\sum_{i=l}^{r}-k\lceil\frac{r-l+1}{m}\rceil\)的最小值,\(n\leq 3*10^5,m\leq 10\)。
思路:
因为\(m\)很小,那么对于一段区间,中间部分可以划分为多个长度为\(m\)的区间,两端都长度不超过\(m\)。
所以令\(dp(i)\)表示以\(i\)为终点的区间最小值,那么一种情况是它作为区间终点,另一种情况就是其作为中间的一段,分情况转移即可。
代码如下:
#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int N = 3e5 + 5;
int n, m, k;
ll a[N], sum[N];
ll dp[N];
int main() {
ios::sync_with_stdio(false); cin.tie(0);
cin >> n >> m >> k;
for(int i = 1; i <= n; i++) cin >> a[i], sum[i] = sum[i - 1] + a[i];
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= min(m, i); j++) {
dp[i] = max(dp[i], sum[i] - sum[i - j] - k);
}
if(i >= m) dp[i] = max(dp[i], dp[i - m] + sum[i] - sum[i - m] - k);
}
ll ans = 0;
for(int i = 1; i <= n; i++) ans = max(ans, dp[i]);
cout << ans;
return 0;
}
还有一种算法,因为余数比较小,所以就以\(i\)%\(m\)的值分类,然后对于每个余数维护一个前缀最小值。
更新答案时用目前前缀减去所维护的最小值就行了。
Educational Codeforces Round 69 D. Yet Another Subarray Problem的更多相关文章
- Educational Codeforces Round 69 D E
Educational Codeforces Round 69 题解 题目编号 A B C D E F 完成情况 √ √ √ ★ ★ - D. Yet Another Subarray Problem ...
- Educational Codeforces Round 69 (Rated for Div. 2) E. Culture Code
Educational Codeforces Round 69 (Rated for Div. 2) E. Culture Code 题目链接 题意: 给出\(n\)个俄罗斯套娃,每个套娃都有一个\( ...
- Educational Codeforces Round 69 (Rated for Div. 2) D. Yet Another Subarray Problem 背包dp
D. Yet Another Subarray Problem You are given an array \(a_1, a_2, \dots , a_n\) and two integers \( ...
- Educational Codeforces Round 69 (Rated for Div. 2) D. Yet Another Subarray Problem 【数学+分块】
一.题目 D. Yet Another Subarray Problem 二.分析 公式的推导时参考的洛谷聚聚们的推导 重点是公式的推导,推导出公式后,分块是很容易想的.但是很容易写炸. 1 有些地方 ...
- Educational Codeforces Round 69 (Rated for Div. 2)
A. DIY ...
- Educational Codeforces Round 69
目录 Contest Info Solutions A. DIY Wooden Ladder B. Pillars C. Array Splitting D. Yet Another Subarray ...
- Educational Codeforces Round 69 (Rated for Div. 2) A~D Sloution
A. DIY Wooden Ladder 题意:有一些不能切的木板,每个都有一个长度,要做一个梯子,求梯子的最大台阶数 做梯子的木板分为两种,两边的两条木板和中间的若干条台阶木板 台阶数为 $k$ 的 ...
- Educational Codeforces Round 69 (Rated for Div. 2) C. Array Splitting 水题
C. Array Splitting You are given a sorted array
- Educational Codeforces Round 69 E - Culture Code (最短路计数+线段树优化建图)
题意:有n个空心物品,每个物品有外部体积outi和内部体积ini,如果ini>outj,那么j就可以套在i里面.现在我们要选出n个物品的一个子集,这个子集内的k个物品全部套在一起,且剩下的物品都 ...
随机推荐
- Windows_pycharm下安装numpy
https://blog.csdn.net/haishu_zheng/article/details/77489309 一.下载在网站https://pypi.python.org/pypi/nump ...
- 【Gamma】Scrum Meeting 7
前言 会议定点:大运村公寓 会议时间:2019/6/5 会议目的:明确下阶段目标 一.任务进度 组员 上周任务进度 下阶段任务 大娃 修复后端bug 辅助做好引导录屏 二娃 撰写会议博客 撰写会议博客 ...
- Gamma阶段事后分析
设想和目标 我们的软件要解决什么问题?是否定义得很清楚?是否对典型用户和典型场景有清晰的描述? 我们的软件要解决的是安卓游戏的自动化异常检测问题,定义的足够清楚,对于典型用户的描述和典型场景的描述也足 ...
- docker 镜像加速,修改为阿里云镜像
首先访问 登录阿里云 https://cr.console.aliyun.com/cn-hangzhou/instances/mirrors,会获取专属的镜像地址 centos用户执行下列操作即可 s ...
- php Access-Control-Allow-Origin 解决跨域问题
第1种 在代码里面加 header信息(推荐) header("Access-Control-Allow-Origin: *"); //如果需要设置允许所有域名发起的跨域请求,可以 ...
- LOJ3124 CTS2019 氪金手游 概率、容斥、树形DP
传送门 D2T3签到题可真是IQ Decrease,概率独立没想到然后就20pts滚粗了 注意题目是先对于所有点rand一个权值\(w\)然后再抽卡. 先考虑给出的关系是一棵外向树的情况.那么我们要求 ...
- Cmder下ssh免密登录配置
1.本地生成ssh-key 在本地cmder终端下运行下面的命令生成ssh的公钥和私钥文件: ssh-keygen -t rsa 其中,.ssh/id_rsa为私钥文件,留在本地使用,而.ssh/id ...
- BFS --- 模板题
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 36079 Accepted: 11123 ...
- 生成Makefile文件全过程
[1]生成Makefile文件全过程 整体流程如下图: 注意:以下文件根目录为testmake(任意位置新建即可) (1)测试程序 1.1 建立两个目录:mkdir include source 1. ...
- scala 函数式编程之集合操作
Scala的集合体系结构 // Scala中的集合体系主要包括:Iterable.Seq.Set.Map.其中Iterable是所有集合trait的根trai.这个结构与Java的集合体系非常相似. ...