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. 应用安全 - Web框架 - 数据库管理 - phpMyAdmin - 漏洞汇总

    CVE-2019-18622 Date: 2019.10.28 类型: SQL injection in Designer feature 影响范围: phpMyAdmin versions prio ...

  2. 11g Oracle Rac安装(基于linux6)

    安装 Oracle 11gR2 RAC on Linux 6 本文介绍如何在Oracle Linux 6上安装2节点Oracle 11gR2 Real Application Cluster(RAC) ...

  3. Insertion Sort List(单链表插入排序)

    来源:https://leetcode.com/problems/insertion-sort-list Sort a linked list using insertion sort. 方法: 1. ...

  4. 【烦人的字符集】linux字符集问题,中文乱码

    [1]快速修改命令 [2]locale 查看现在服务器的字符 [root@Master ~]# localeLANG=en_US.UTF-8LC_CTYPE="zh_CN.UTF-8&quo ...

  5. 前端导出excel文件

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. centos6配置本地yum源

    在无法访问外网时,yum安装软件会失败,这时候可以配置yum源为本地的镜像iso来解决这个问题 1. 使用Xftp上传iso镜像文件到服务器 2. 使用如下命令新建挂载点并挂载 sudo mkdir ...

  7. Python和Js打印心形

    看到一行Python写的代码,会用LovePython输出心形: print('\n'.join([''.join([('LovePython'[(x-y)%10]if((x*0.05)**2+(y* ...

  8. P2220 [HAOI2012]容易题

    传送门 首先 $(\sum_{i=1}^{n}a_i)(\sum_{i=1}^{m}b_i)$ 展开以后包含了所有 $ab$ 两两相乘的情况并且每种组合只出现一次 发现展开后刚好和题目对序列价值的定义 ...

  9. Python 余弦相似度与皮尔逊相关系数 计算

    夹角余弦(Cosine) 也可以叫余弦相似度. 几何中夹角余弦可用来衡量两个向量方向的差异,机器学习中借用这一概念来衡量样本向量之间的差异. (1)在二维空间中向量A(x1,y1)与向量B(x2,y2 ...

  10. VS 2012 Unit Test

    1,Open Tool->Custmoize 2,Create Unit Tests Move Down Run Test 3,Restart run VS 4,Create UnitTest ...