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 ...
随机推荐
- anaconda3,将python版本回退(python3.7---python3.5)
2019/6 安装anaconda3时,安装了默认的最新版本,但是由于不能兼容tensorflow,我又配置了一个python3.5的环境: 可惜这里真的不晓得咋回事,在python3.5中进入jup ...
- win10安装mysql时报错[MY-012576] [InnoDB] Unable to create temporary file; errno: 2
报错信息 解决: 在my.ini文件里面的 [mysqld]区段内加入: #自己指定的临时文件目录 tmpdir="临时目录" 添加好后初始化成功 接下来启动mysql服务的时候报 ...
- python 并发编程 多进程 Process对象的其他属性方法 terminate与is_alive name pid 函数
进程对象的其他方法一: terminate与is_alive is_alive() 立刻查看的子进程结果 是否存活 from multiprocessing import Process impor ...
- Linux-Maven部署
一.Maven是什么 二.Maven部署 1.环境信息: (1)centos7.3 (2)jdk1.8 (3)maven3.5.3 2.安装jdk (1)下载地址[http://www.oracle. ...
- 【监控笔记】【1.1】监控事件系列——SQL Server Profiler
声明:本系列是书目<sql server监控与诊断读书笔记> 联机丛书监控:https://docs.microsoft.com/en-us/sql/relational-database ...
- springboot swagger教程😀
传送门开启:https://www.ibm.com/developerworks/cn/java/j-using-swagger-in-a-spring-boot-project/index.html
- Tensorflow模型保存与加载
在使用Tensorflow时,我们经常要将以训练好的模型保存到本地或者使用别人已训练好的模型,因此,作此笔记记录下来. TensorFlow通过tf.train.Saver类实现神经网络模型的保存和提 ...
- openmvg中cmd模块解析
---恢复内容开始--- 在openmvg库中,定义了一个CmdLine类来定义例程的输入参数格式.源文件为.\openMVG\src\third_party\cmdLine\cmdLine.h. 先 ...
- j网络爬虫之WebMagic
WebMagic官网:http://webmagic.io/ 注意: 1.在自定义PageProcessor中使用System.out.println(“str”),Spider.create(new ...
- wordpress添加自定义菜单栏
1. 以下代码是wordpress的wp_nav_menu(); <?php $defaults = array( 'container'=>false, //父级div为空 'link_ ...