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个物品全部套在一起,且剩下的物品都 ...
随机推荐
- 苹果 appstore 上架 ipv6 服务 配置
前言 好久之前的事了,苹果审核突然要求ipv6,一片哀嚎. 研究了好久找到了基于阿里云经典网络Windows Server的配置方法. https://bbs.aliyun.com/read/2849 ...
- Win10,Anaconda,tensorflow-gpu安装教程
,参考于:https://www.cnblogs.com/guoyaohua/p/9265268.html 目录 前言 第一步:安装Anaconda 1.下载和安装 2.配置Anaconda环境变量 ...
- WebUploader 被 FormData 函数坑了,用了他的页面千万别定FormData变量
WebUploader 被 FormData 函数坑了,用了他的页面千万别定FormData变量 被这个坑了,页面中变量var FormData=null;后又为他赋了值,所有一直上传不了,也没有错 ...
- ES6高级技巧(三)
html转译 let c='<a href="#">Me & you</a>'.replace(/[&<>'"]/g, ...
- [转帖]算法精解:DAG有向无环图
算法精解:DAG有向无环图 https://www.cnblogs.com/Evsward/p/dag.html DAG是公认的下一代区块链的标志.本文从算法基础去研究分析DAG算法,以及它是如何运用 ...
- php-微信分享签名
//测试 $APPID='*************************'; $SECRET='***********************'; $url = trim($_REQUEST['u ...
- Go语言【项目】 websocket消息服务
websocket消息服务 目的:搭建websocket服务,用浏览器与服务进行消息交互(写的第一个Go程序) 代码目录结构: 前端html页面: <!DOCTYPE html> < ...
- c# 读取文件目录下的信息
private void button1_Click(object sender, System.EventArgs e) { //浏览文件夹 this.folderBrowserDialog1.Sh ...
- azure 上传blob到ams(CreateFromBlob)
遇到的错误:The destination storage credentials must contain the account key credentials,参数名: destinationS ...
- MySQL之查询篇(三)
一:查询 1.创建数据库,数据表 -- 创建数据库 create database python_test_1 charset=utf8; -- 使用数据库 use python_test_1; -- ...