题目链接: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. C++必知必会(1)

    条款1数据抽象 抽象数据类型的用途在于将变成语言扩展到一个特定的问题领域.一般对抽象数据类型的定义须要准训下面步骤: 1.     为类型取一个描写叙述性的名字 2.     列出类型所能运行的操作 ...

  2. 微信小程序-使用腾讯Wxpage

    微信小程序想要更快的速度吗? 满足你 https://github.com/tvfe/wxpage#-c%E5%AE%9A%E4%B9%89 使用超简单(导入wxpage.js,最后使用对象名:P): ...

  3. xshell的快捷键(很有用)

    删除 ctrl + d      删除光标所在位置上的字符相当于VIM里x或者dl ctrl + h      删除光标所在位置前的字符相当于VIM里hx或者dh ctrl + k      删除光标 ...

  4. js 解析json字符串

    server端返回的数据例如以下: {"list":[{"id":1,"name":"汉族"},{"id&qu ...

  5. 27:简单错误记录SimpleErrorLog

    题目描述 开发一个简单错误记录功能小模块,能够记录出错的代码所在的文件名称和行号. 处理: 1. 记录最多8条错误记录,循环记录,对相同的错误记录(净文件名称和行号完全匹配)只记录一条,错误计数增加: ...

  6. ubuntu16.04 Cmake学习二

    本节主要总结编译程序的时候使用了第三方库的情况,以调用开源opencv-2.4.9为例子,具体安装详见http://www.cnblogs.com/xsfmg/p/5900420.html. 工程文件 ...

  7. HBase 系统架构及数据结构

    一.基本概念     2.1 Row Key (行键)     2.2 Column Family(列族)     2.3 Column Qualifier (列限定符)     2.4 Column ...

  8. 过滤XSS的HTMLPurifier使用

    什么是HTMLPurifier? 在php里解决XSS最简单的方法是使用htmlspecialchars转义xml实体,但对于需要使用xml的时候就搏手无策了. HTML Purifier是基于php ...

  9. oracle函数中lead,lag,over,partition by 的使用

    lead,lag函数的分析 http://blog.csdn.net/mazongqiang/article/details/7621328 举例如下: SQL> select *  from ...

  10. 【Android】第三方库使用的问题集

    Google/百度地图Key的获取 百度地图UnsatisfiedLinkError错误 async-http-client中的FATAL EXCEPTION Google/百度地图Key的获取 无论 ...