Problem Description

The description of this problem is very short. Now give you a string(length N), and ask you the max sum of the substring which the length can't small than M.

Input

The first line is one integer T(T≤20) indicates the number of the test cases. Then for every case, the first line is two integer N(1≤N≤1000000) and M(1≤M≤N).

Then one line contains N integer indicate the number. All the number is between -10000 and 10000.

Output

Output one line with an integer.

SampleInput

2
5 1
1 -2 -2 -2 1
5 2
1 -2 -2 -2 1

SampleOutput

1

-1

题意:T组数据,每组给n和m,n表示下面要给你的序列的长度,m表示要求的子连续序列的最短长度。接下来n个数给定序列。求一个长度大于等于m的子连续序列使得该序列所有元素之和最大。

思路:

比如第二个样例

5 2

1 -2 -2 -2 1

可以选择区间[1,2]包含1和-2,和为-1;也可以选择区间[4,5],和也为-1。

由于n有,所以的算法如果剪枝不好,比较容易TLE。

为描述方便,假设题目数据给我们的序列为数组a(下标从1-n),我们可以先预处理一个前缀和sum。

然后题目要求的就是最大的sum[r]-sum[l]:要求r–l+1 >= m,l>=1,r<=n。

这里的l的r分别表示区间左端点和右端点。我们可以从左到右遍历整个序列,由于要求的序列长度必须大于等于m,所以r要从m开始。

于是对于每一个r我们要求的是:当l满足1<=l<=r-m+1时,最小的sum[l](要sum[r]-sum[l]最大,sum[l]最小)。

那么我们只要在从左到右遍历的时候维护当前最小的sum[l]就可以了。

AC代码:562ms

 #include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll; const ll INFLL = 0x7fffffffffffffffLL;
const int MAXN = ;
int a[MAXN];
ll sum[MAXN]; int main() {
int T, n, m, i;
scanf("%d", &T);
while(T--) {
scanf("%d%d", &n, &m);
for(i = ; i <= n; i++) {
scanf("%d", &a[i]);
sum[i] = sum[i - ] + a[i];
}
int r = m;
ll mi = , ans = -INFLL;
while(r <= n) {
mi = min(sum[r - m], mi);
ans = max(ans, sum[r] - mi);
r++;
}
printf("%I64d\n", ans);
}
return ;
}

FOJ-2013 A Short Problem (前缀和)的更多相关文章

  1. 贪心 FZU 2013 A short problem

    题目传送门 /* 题意:取长度不小于m的序列使得和最大 贪心:先来一个前缀和,只要长度不小于m,从m开始,更新起点k最小值和ans最大值 */ #include <cstdio> #inc ...

  2. FZU2013 A short problem —— 线段树/树状数组 + 前缀和

    题目链接:https://vjudge.net/problem/FZU-2013  Problem 2013 A short problem Accept: 356    Submit: 1083Ti ...

  3. HDU----(4291)A Short problem(快速矩阵幂)

    A Short problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  4. hdu 4291 A Short problem(矩阵+取模循环节)

    A Short problem                                                          Time Limit: 2000/1000 MS (J ...

  5. HDU 4291 A Short problem(2012 ACM/ICPC Asia Regional Chengdu Online)

    HDU 4291 A Short problem(2012 ACM/ICPC Asia Regional Chengdu Online) 题目链接http://acm.hdu.edu.cn/showp ...

  6. HDU 4291 A Short problem(矩阵+循环节)

    A Short problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  7. HDU——4291A Short problem(矩阵快速幂+循环节)

    A Short problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  8. 循环节 + 矩阵快速幂 - HDU 4291 A Short problem

    A Short problem Problem's Link Mean: 给定一个n,求:g(g(g(n))) % 1000000007 其中:g(n) = 3g(n - 1) + g(n - 2), ...

  9. hdu4291 A Short problem

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission ...

随机推荐

  1. Dijkstra算法2

    // 再来一手精髓的Dijkstra // 复杂度O( E*log(V) ) #include <cstdio> #include <iostream> #include &l ...

  2. R语言矩阵维度“消失”的问题

    矩阵(matrix)是R语言中很基础的一种数据结构,也是R语言使用者经常使用的一种数据结构.矩阵的维度一般为二维(m*n). R语言中矩阵的操作是非常简单易懂的,但是在对R语言做矩阵操作时,有个地方需 ...

  3. iMacros 入门教程-基础函数介绍(2)

    imacros 的 pos 参数是什么意思 position的缩写,如果有 2 个以上的元素共用完全相同的属性(比方说同一个小区的同一栋楼),这个 POS 的参数可以借由不同位置来帮助明确定位(也就是 ...

  4. [APIO2018] New Home 新家 [线段树,multiset]

    线段树的每个点表示当前点的前驱,即这个颜色上一次出现的位置,这个玩意multiset随便写写就完了. 重要的是怎么查询答案,无解显然先判掉. 线段树上二分就可以了 #include <bits/ ...

  5. 基于topsis和熵权法

    % % X 数据矩阵 % % n 数据矩阵行数即评价对象数目 % % m 数据矩阵列数即经济指标数目 % % B 乘以熵权的数据矩阵 % % Dist_max D+ 与最大值的距离向量 % % Dis ...

  6. opencv —— src.at<Vec3b>(i, j)[0]、src.at<uchar>(i, j)、src.ptr<uchar>(i) 访问图像的单个像素

    动态地址访问像素:src.at<Vec3b>(i, j)[0].src.at<uchar>(i, j)  int b = src.at<Vec3b>(i, j)[0 ...

  7. Spring boot mvn

    https://www.cnblogs.com/xiebq/p/9181517.html https://www.cnblogs.com/sun-yang-/p/7700415.html https: ...

  8. Centos 7 安装配置git

    Centos 7 安装配置git 1.安装git yum install git 2.验证git git -version 3.配置基本信息

  9. 怎么利用 ChromeDriver 和 Selenium对 CEF应用进行自动化测试-java实现

    Overview ChromeDriver and Selenium are tools for automated testing of Chromium-based applications. T ...

  10. VAR向量自回归模型学习笔记2

    向量自回归模型 今天的你 和昨天的你 和前天的你,是否具有相关性. 1. 定义 向量自回归(VAR,Vector Auto regression)分析联合内生变量间的动态关系 联合:n个变量间的相互影 ...