题目链接:https://vjudge.net/problem/FZU-2013

 Problem 2013 A short problem

Accept: 356    Submit: 1083
Time Limit: 1000 mSec    Memory Limit : 32768 KB

 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.

 Sample Input

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

 Sample Output

1
-1

 Source

FOJ有奖月赛-2011年03月

题意:

给出一个序列,求长度不小于m且和最大的子序列。

题解:

1.求出前缀和。

2.将0插入到线段树第0位,然后从第m为开始枚举:去线段树范围为0~i-m的地方找最小值,然后以i为结尾的子序列最大值即为:sum[i] - query(1,0,n,0,i-m)。答案即为max(sum[i] - query(1,0,n,0,i-m))。

3.类似的题目:CSU - 1551 Longest Increasing Subsequence Again

线段树:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e9+;
const int MAXN = 1e6; int minv[MAXN*];
void add(int u, int l, int r, int x, int val)
{
if(l==r)
{
minv[u] = val;
return;
}
int mid = (l+r)/;
if(x<=mid) add(u*, l, mid, x, val);
else add(u*+, mid+,r, x, val);
minv[u] = min(minv[u*], minv[u*+]);
} int query(int u, int l, int r, int x, int y)
{
if(l<=x&&y<=r)
return minv[u]; int mid = (l+r)/;
int ret = INF;
if(x<=mid) ret = min(ret, query(u*, l, mid, x, mid));
if(y>=mid+) ret = min(ret, query(u*+, mid+, r, mid+, y));
return ret;
} int sum[MAXN];
int main()
{
int T, n, m;
scanf("%d", &T);
while(T--)
{
scanf("%d%d", &n,&m);
sum[] = ;
for(int i = ; i<=n; i++)
{
int val;
scanf("%d", &val);
sum[i] = sum[i-]+val;
} for(int i = ; i<=n*; i++)
minv[i] = INF; int ans = -INF;
add(,,n,,);
for(int i = m; i<=n; i++)
{
ans = max(ans, sum[i]-query(,,n,,i-m));
add(,,n,i-m+,sum[i-m+]);
}
printf("%d\n", ans);
}
}

树状数组:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e9+;
const int MAXN = 1e6; int T, n, m;
int lowbit(int x)
{
return x&(-x);
} int c[MAXN];
void add(int x, int d)
{
while(x<=n)
{
c[x] = min(c[x], d);
x += lowbit(x);
}
} int query(int x)
{
int s = INF;
while(x>)
{
s = min(c[x], s);
x -= lowbit(x);
}
return s;
} int sum[MAXN];
int main()
{
scanf("%d", &T);
while(T--)
{
memset(c, , sizeof(c));
scanf("%d%d", &n,&m);
sum[] = ;
for(int i = ; i<=n; i++)
{
int val;
scanf("%d", &val);
sum[i] = sum[i-]+val;
}
for(int i = ; i<=n+; i++)
c[i] = INF; int ans = -INF;
add(, );
for(int i = m; i<=n; i++)
{
ans = max(ans, sum[i]-query(i-m+));
add(i-m+, sum[i-m+]);
}
printf("%d\n", ans);
}
}

FZU2013 A short problem —— 线段树/树状数组 + 前缀和的更多相关文章

  1. CSU - 1551 Longest Increasing Subsequence Again —— 线段树/树状数组 + 前缀和&后缀和

    题目链接:http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1551 题意: 给出一段序列, 删除其中一段连续的子序列(或者不删), 使得剩下的序列 ...

  2. 51nod 1081 子段求和(线段树 | 树状数组 | 前缀和)

    题目链接:子段求和 题意:n个数字序列,m次询问,每次询问从第p个开始L长度序列的子段和为多少. 题解:线段树区间求和 | 树状数组区间求和 线段树: #include <cstdio> ...

  3. Codeforces Round #365 (Div. 2) D. Mishka and Interesting sum (离线树状数组+前缀xor)

    题目链接:http://codeforces.com/contest/703/problem/D 给你n个数,m次查询,每次查询问你l到r之间出现偶数次的数字xor和是多少. 我们可以先预处理前缀和X ...

  4. ACM学习历程—HDU5700 区间交(树状数组 && 前缀和 && 排序)

    http://acm.hdu.edu.cn/showproblem.php?pid=5700 这是这次百度之星初赛2B的第五题.省赛回来看了一下,有这样一个思路:对于所有的区间排序,按左值排序. 然后 ...

  5. MooFest 树状数组 + 前缀和

    比较友好的数据结构题 建议读者好好思考思考--. 可以分析出与前缀和的关系, 之后就愉快的套起树状数组辣 #include <cstdio> #include<queue> # ...

  6. BZOJ 3787 Gty的文艺妹子序列(分块+树状数组+前缀和)

    题意 给出n个数,要求支持单点修改和区间逆序对,强制在线. n,m<=50000 题解 和不带修改差不多,预处理出smaller[i][j]代表前i块小于j的数的数量,但不能用f[i][j]代表 ...

  7. [noip科普]关于LIS和一类可以用树状数组优化的DP

    预备知识 DP(Dynamic Programming):一种以无后效性的状态转移为基础的算法,我们可以将其不严谨地先理解为递推.例如斐波那契数列的递推求法可以不严谨地认为是DP.当然DP的状态也可以 ...

  8. 【转】关于LIS和一类可以用树状数组优化的DP 预备知识

    原文链接 http://www.cnblogs.com/liu-runda/p/6193690.html 预备知识 DP(Dynamic Programming):一种以无后效性的状态转移为基础的算法 ...

  9. 【洛谷 p3368】模板-树状数组 2(数据结构)

    题目:已知一个数列,你需要进行下面两种操作:1.将某区间每一个数数加上x:2.求出某一个数的和. 解法:树状数组+前缀和优化.数组中每位存和前一位的数的差,这样区间修改只用改两位,单点询问就是求前缀和 ...

随机推荐

  1. 5.2 calendar--通用日期的相关函数(3)

    prmonth(theyear, themonth, w=0, l=0) 打印指定年和月的日历.格式与formatmonth()函数一样. 样例: #python 3.4 import calenda ...

  2. 【Python】分析文本split()

    分析单个文本 split()方法,是以空格为分隔符将字符串拆分成多个部分,并将这些部分存储到一个列表中 title = 'My name is oliver!' list = title.split( ...

  3. js向后台传递对象

    js: }; $.ajax({ url: "/.../...", type: "POST", async: false, data: JSON.stringif ...

  4. github 迁移google code 项目

    本文的原文连接是: http://blog.csdn.net/freewebsys/article/details/46692181 转载请一定注明出处. 1,关于google code google ...

  5. inflate, findViewById与setContentView的差别与联系

    protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentV ...

  6. 添加w3c验证图片到网站

    1.在http://validator.w3.org/网站验证 2.添加验证通过后的代码至自己的网站 类似<p>    <a href="http://validator. ...

  7. .NET C# 【小技巧】控制台程序,运行是否弹出窗口选择!

    选中控制台程序项目,右键→属性→应用程序栏→输出类型: 1.Windows 应用程序(不弹出提示框)! 2.控制台应用程序(弹出提示框)! 3.类库(类库生成dll,是不能直接运行的,类库供应用程序调 ...

  8. 华为P20无敌拍摄能力开放 如何即刻获得?

    在全球专业相机测评机构DXOmark发布的相机评测排行中,华为P20.P20 Pro成功登顶“全球拍照最好智能手机”.P20 Pro综合得分高达109分,P20综合得分102分.“华为并非简单地将第三 ...

  9. Go语言入门系列1:安装,How to Write Go Code

    https://golang.org/doc/code.html src contains Go source files, pkg contains package objects, and bin ...

  10. java 白皮书的关键术语

    [0]README 0.1) 本文转自 core java volume 1,仅供了解,所谓爱屋及乌嘛: 0.2) java的设计者编写了颇有影响力的白皮书,用来解释设计的初衷以及完成的情况,并发布了 ...