CodeForces 1197D Yet Another Subarray Problem
Time limit 2000 ms
Memory limit 262144 kB
Source Educational Codeforces Round 69 (Rated for Div. 2)
Tags dp greedy math *1900
Editorial Announcement (en) Tutorial #1 (en) Tutorial #2 (en) Tutorial #3 (ru)
官方题解
At first let's solve this problem when m=1" role="presentation">m=1m=1 and k=0" role="presentation">k=0k=0 (it is the problem of finding subarray with maximum sum). For each position from 1" role="presentation">11 to n" role="presentation">nn we want to know the value of maxli=max1≤j≤i+1sum(j,i)" role="presentation">maxli=max1≤j≤i+1sum(j,i)maxli=max1≤j≤i+1sum(j,i), where sum(l,r)=∑k=lk≤rak" role="presentation">sum(l,r)=∑k=lk≤raksum(l,r)=∑k=lk≤rak, and sum(x+1,x)=0" role="presentation">sum(x+1,x)=0sum(x+1,x)=0.
We will calculate it the following way. maxli" role="presentation">maxlimaxli will be the maximum of two values:
- 0" role="presentation">00 (because we can take segments of length 0" role="presentation">00);
- ai+maxli−1" role="presentation">ai+maxli−1ai+maxli−1.
The maximum sum of some subarray is equal to max1≤i≤nmaxli" role="presentation">max1≤i≤nmaxlimax1≤i≤nmaxli.
So, now we can calculate the values of besti=max0≤len,i−len⋅m≥0(sum(i−len⋅m+1,i)−len∗k)" role="presentation">besti=max0≤len,i−len⋅m≥0(sum(i−len⋅m+1,i)−len∗k)besti=max0≤len,i−len⋅m≥0(sum(i−len⋅m+1,i)−len∗k) the same way.
besti" role="presentation">bestibesti is the maximum of two values:
- 0;
- sum(i−m+1,i)−k+besti−m" role="presentation">sum(i−m+1,i)−k+besti−msum(i−m+1,i)−k+besti−m.
After calculating all values besti" role="presentation">bestibesti we can easily solve this problem. At first, let's iterate over the elements besti" role="presentation">bestibesti. When we fix some element besti" role="presentation">bestibesti, lets iterate over the value len=1,2,…,m" role="presentation">len=1,2,…,mlen=1,2,…,m and update the answer with value besti+sum(i−len,i−1)−k" role="presentation">besti+sum(i−len,i−1)−kbesti+sum(i−len,i−1)−k.
源代码
#include<stdio.h>
#include<algorithm>
int n,m,k;
long long a[300010];
long long dp[300010],ans;
int main()
{
//freopen("test.in","r",stdin);
scanf("%d%d%d",&n,&m,&k);
for(int i=1;i<=n;i++) scanf("%lld",a+i),a[i]+=a[i-1];
for(int i=1;i<=n;i++)
{
for(int j=i;j+m>=i;j--)
dp[i]=std::max(dp[i],a[i]-a[j]);
dp[i]-=k;
dp[i]=std::max(0LL,dp[i]);
if(i>m) dp[i]=std::max(dp[i],dp[i-m]+a[i]-a[i-m]-k);
ans=std::max(dp[i],ans);
}
printf("%lld\n",ans);
return 0;
}
CodeForces 1197D Yet Another Subarray Problem的更多相关文章
- Educational Codeforces Round 69 D. Yet Another Subarray Problem
Educational Codeforces Round 69 (Rated for Div. 2) D. Yet Another Subarray Problem 题目链接 题意: 求\(\sum_ ...
- 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 有些地方 ...
- maximum subarray problem
In computer science, the maximum subarray problem is the task of finding the contiguous subarray wit ...
- 动态规划法(八)最大子数组问题(maximum subarray problem)
问题简介 本文将介绍计算机算法中的经典问题--最大子数组问题(maximum subarray problem).所谓的最大子数组问题,指的是:给定一个数组A,寻找A的和最大的非空连续子数组.比如 ...
- Educational Codeforces Round 67 D. Subarray Sorting
Educational Codeforces Round 67 D. Subarray Sorting 传送门 题意: 给出两个数组\(a,b\),现在可以对\(a\)数组进行任意次排序,问最后能否得 ...
- D. Yet Another Subarray Problem 思维 难 dp更好理解
D. Yet Another Subarray Problem 这个题目很难,我比赛没有想出来,赛后又看了很久别人的代码才理解. 这个题目他们差不多是用一个滑动窗口同时枚举左端点和右端点,具体如下: ...
- [题解]Yet Another Subarray Problem-DP 、思维(codeforces 1197D)
题目链接:https://codeforces.com/problemset/problem/1197/D 题意: 给你一个序列,求一个子序列 a[l]~a[r] 使得该子序列的 sum(l,r)-k ...
- CodeForces 1197 D Yet Another Subarray Problem
题面 不得不说CF还是很擅长出这种让人第一眼看摸不着头脑然后再想想就发现是个SB题的题的hhh(请自行断句). 设sum[]为前缀和数组,那么区间 [l,r]的价值为 sum[r] - sum[l-1 ...
随机推荐
- 红帽学习笔记[RHCSA] 第三课[输出重定向、Vi编辑器]
第三课 关于Linux的输入输出 输入输出 0 stdin 标准输入 仅读取 1 stdout 标准输出 仅写入 2 stderr 标准错误 仅写入 3 filename 其他文件 读取和/或写入 输 ...
- 【Linux 架构】Linux内核架构
(1)System Call Interface(SCI)------系统调用接口(2)Process Management(PM)-------进程管理模块(3)Memory Management( ...
- (3.5)常用知识-NULL与零长度、字符串尾部填充空格
概述:NULL与零长度是不同的,NULL表示数据未知或不可用,它是与零(数值或2进制).零长度字符串不 同的一种值,也可以理解为一种状态. 即可以理解为:所有的变量都有2种状态,一种有值,一种为NUL ...
- 一分钟安装mysql
学数据库的人都知道,MySQL数据库是比较基本的掌握要求,不仅开源而且社区版本是免费使用的.由于工作上或者经常更换系统的原因,有时候会需要安装MySQL数据库.为了不至于每次安装都要查阅资料,现把安装 ...
- 通过Spark Streaming处理交易数据
Apache Spark 是加州大学伯克利分校的 AMPLabs 开发的开源分布式轻量级通用计算框架. 由于 Spark 基于内存设计,使得它拥有比 Hadoop 更高的性能(极端情况下可以达到 10 ...
- mysql5.7 修改用户密码
修改vi /etc/my.cnf,增加skip-grant-tables可以免密码登录mysql use mysql ; update user set authentication_string=P ...
- SQL Server之索引解析(二)
1.堆表 堆表通过IAM连接一起,查询时全表扫描. 1.1 非聚集索引 结构 叶子节点数据结构:行数据结构+Rid(8字节) 中间节点数据结构: (非聚集非唯一索引)行数据结构+Page(4)+2+ ...
- 【模板】最长上升子序列(LIS)及其优化 & 洛谷 AT2827 LIS
最长上升子序列 传送门 题意 对于给定的一个n个数的序列,找到它的一个最长的子序列,并且保证这个子序列是由低到高排序的. 例如,1 6 2 5 4 6 8的最长上升子序列为1 2 4 6 8. 基本思 ...
- 【ES6】对象的新功能与解构赋值
ES6 通过字面量语法扩展.新增方法.改进原型等多种方式加强对象的使用,并通过解构简化对象的数据提取过程. 一.字面量语法扩展 在 ES6 模式下使用字面量创建对象更加简洁,对于对象属性来说,属性初始 ...
- WindowsForms使用Telerik Reporting
新建一个WindowsForms窗体项目 然后拖动ReportViewer这个控件到WindowsForms的窗体中 如上图所示,用来呈现报表的控件,这个控件可以打印报表,转换报表这类的功能 接下来我 ...