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的更多相关文章

  1. Educational Codeforces Round 69 D. Yet Another Subarray Problem

    Educational Codeforces Round 69 (Rated for Div. 2) D. Yet Another Subarray Problem 题目链接 题意: 求\(\sum_ ...

  2. 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 \( ...

  3. Educational Codeforces Round 69 (Rated for Div. 2) D. Yet Another Subarray Problem 【数学+分块】

    一.题目 D. Yet Another Subarray Problem 二.分析 公式的推导时参考的洛谷聚聚们的推导 重点是公式的推导,推导出公式后,分块是很容易想的.但是很容易写炸. 1 有些地方 ...

  4. maximum subarray problem

    In computer science, the maximum subarray problem is the task of finding the contiguous subarray wit ...

  5. 动态规划法(八)最大子数组问题(maximum subarray problem)

    问题简介   本文将介绍计算机算法中的经典问题--最大子数组问题(maximum subarray problem).所谓的最大子数组问题,指的是:给定一个数组A,寻找A的和最大的非空连续子数组.比如 ...

  6. Educational Codeforces Round 67 D. Subarray Sorting

    Educational Codeforces Round 67 D. Subarray Sorting 传送门 题意: 给出两个数组\(a,b\),现在可以对\(a\)数组进行任意次排序,问最后能否得 ...

  7. D. Yet Another Subarray Problem 思维 难 dp更好理解

    D. Yet Another Subarray Problem 这个题目很难,我比赛没有想出来,赛后又看了很久别人的代码才理解. 这个题目他们差不多是用一个滑动窗口同时枚举左端点和右端点,具体如下: ...

  8. [题解]Yet Another Subarray Problem-DP 、思维(codeforces 1197D)

    题目链接:https://codeforces.com/problemset/problem/1197/D 题意: 给你一个序列,求一个子序列 a[l]~a[r] 使得该子序列的 sum(l,r)-k ...

  9. CodeForces 1197 D Yet Another Subarray Problem

    题面 不得不说CF还是很擅长出这种让人第一眼看摸不着头脑然后再想想就发现是个SB题的题的hhh(请自行断句). 设sum[]为前缀和数组,那么区间 [l,r]的价值为 sum[r] - sum[l-1 ...

随机推荐

  1. 【Linux开发】【DSP开发】利用CCS6.1生成out文件的同时生成bin文件

    [Linux开发][DSP开发]利用CCS6.1生成out文件的同时生成bin文件 标签:[DSP开发] [Linux开发] 尝试在windows上安装的CCS6.1开发AM4378-Linux下的应 ...

  2. 关于cors 跨域的一些问题

    system.webServer节点写配置 <httpProtocol> <customHeaders> <add name="Access-Control-A ...

  3. java 中的equals()小结

    转载自http://www.cnblogs.com/jackyrong/archive/2006/08/20/481994.html Java中的equals是十分重要的,和= =要区别开来,最近在看 ...

  4. 使用itchat完成微信自动回复

    import itchat from itchat.content import * # 微信自动回复 @itchat.msg_register([TEXT]) def text_reply(msg) ...

  5. 只需要2个工具,百度云盘大文件就能用迅雷和IDM下载

    不会代码,不懂脚本,没关系 ,能找到一座通往它们的桥梁,照样能到达彼岸. 这里以360极速浏览器为例. 在浏览器地址框输入以下地址直接到达浏览器安装扩展插件的地方(偷个懒,复制网址吧),https:/ ...

  6. C语言I-2019博客作业02

    这个作业属于哪个课程 C语言程序设计I 这个作业要求在哪里 C语言I-2019秋作业02 我在这个课程的目标是 学会编程及提问的技能 这个作业在哪个具体目标方面帮助我实现目标 深入了解C语言程序设计中 ...

  7. Linux文件与目录操作 ls 命令(2)

    说文件操作是最频繁地操作也不为过,在Linux中,使用ls命令可以列出当前目录中所有内容,本篇就先说说ls命令.本文所说的文件指文件和目录. ls命令常用选项 -a:显示指定目录下所有子目录与文件,包 ...

  8. Java 5种单例模式

    /*单例模式: 指某个类中只能存在一个对象实例,并且该类中只提供一个取得其对象实例的方法 优点:减少系统性能开销 应用场景:网站的计数器,任务管理器,回收站等*/   //单例模式1 -- 静态内部类 ...

  9. [Codeforces 1239D]Catowise City(2-SAT)

    [Codeforces 1239D]Catowise City(2-SAT) 题面 有n个主人,每个主人都有一只猫.每个主人认识一些猫(包括自己的猫).现在要选出一些人和一些猫,个数均大于0且总共为n ...

  10. P1397 [NOI2013]矩阵游戏(递推)

    P1397 [NOI2013]矩阵游戏 一波化式子,$f[1][m]=a^{m-1}+b\sum_{i=0}^{m-2}a^i$,用快速幂+逆元求等比数列可以做到$logm$ 设$v=a^{m-1}, ...