FZU2013 A short problem —— 线段树/树状数组 + 前缀和
题目链接:https://vjudge.net/problem/FZU-2013
Problem 2013 A short problemAccept: 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
5 1
1 -2 -2 -2 1
5 2
1 -2 -2 -2 1
Sample Output
-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 —— 线段树/树状数组 + 前缀和的更多相关文章
- CSU - 1551 Longest Increasing Subsequence Again —— 线段树/树状数组 + 前缀和&后缀和
题目链接:http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1551 题意: 给出一段序列, 删除其中一段连续的子序列(或者不删), 使得剩下的序列 ...
- 51nod 1081 子段求和(线段树 | 树状数组 | 前缀和)
题目链接:子段求和 题意:n个数字序列,m次询问,每次询问从第p个开始L长度序列的子段和为多少. 题解:线段树区间求和 | 树状数组区间求和 线段树: #include <cstdio> ...
- Codeforces Round #365 (Div. 2) D. Mishka and Interesting sum (离线树状数组+前缀xor)
题目链接:http://codeforces.com/contest/703/problem/D 给你n个数,m次查询,每次查询问你l到r之间出现偶数次的数字xor和是多少. 我们可以先预处理前缀和X ...
- ACM学习历程—HDU5700 区间交(树状数组 && 前缀和 && 排序)
http://acm.hdu.edu.cn/showproblem.php?pid=5700 这是这次百度之星初赛2B的第五题.省赛回来看了一下,有这样一个思路:对于所有的区间排序,按左值排序. 然后 ...
- MooFest 树状数组 + 前缀和
比较友好的数据结构题 建议读者好好思考思考--. 可以分析出与前缀和的关系, 之后就愉快的套起树状数组辣 #include <cstdio> #include<queue> # ...
- BZOJ 3787 Gty的文艺妹子序列(分块+树状数组+前缀和)
题意 给出n个数,要求支持单点修改和区间逆序对,强制在线. n,m<=50000 题解 和不带修改差不多,预处理出smaller[i][j]代表前i块小于j的数的数量,但不能用f[i][j]代表 ...
- [noip科普]关于LIS和一类可以用树状数组优化的DP
预备知识 DP(Dynamic Programming):一种以无后效性的状态转移为基础的算法,我们可以将其不严谨地先理解为递推.例如斐波那契数列的递推求法可以不严谨地认为是DP.当然DP的状态也可以 ...
- 【转】关于LIS和一类可以用树状数组优化的DP 预备知识
原文链接 http://www.cnblogs.com/liu-runda/p/6193690.html 预备知识 DP(Dynamic Programming):一种以无后效性的状态转移为基础的算法 ...
- 【洛谷 p3368】模板-树状数组 2(数据结构)
题目:已知一个数列,你需要进行下面两种操作:1.将某区间每一个数数加上x:2.求出某一个数的和. 解法:树状数组+前缀和优化.数组中每位存和前一位的数的差,这样区间修改只用改两位,单点询问就是求前缀和 ...
随机推荐
- 利用iptables的NAT代理实现内网访问外网
利用NAT代理实现内网访问外网 背景及原理 若局域网中的两台计算机只能有一台能够访问外网,而这两台计算机之间能相互通信,那么可以配置能访问外网的那台服务器实现路由器的功能,即实现其他机器的NAT转换, ...
- Android API Guides---Layouts
布局定义了视觉结构的用户界面.如活动或应用程序插件的用户界面. 您能够通过两种方式申报的布局: 声明在XML UI元素. Android提供了相应视图类和子类,如那些部件和布局一个简单的XML词汇表. ...
- Solidworks如何改变零件颜色
如图所示装配体有三个零件,现在我想把移动件的颜色变成红色 鼠标左键单击要改变颜色的零件(这里点击"移动件"),然后在弹出的菜单中选择颜色,最后点击"编辑颜色" ...
- leetcode 46-Permutations and 47-Permutations II
Permutations Given a collection of numbers, return all possible permutations. For example, [1,2,3] h ...
- BZOJ 4128 Matrix BSGS+矩阵求逆
题意:链接 方法: BSGS+矩阵求逆 解析: 这题就是把Ax=B(mod C)的A和B换成了矩阵. 然而别的地方并没有修改. 所以就涉及到矩阵的逆元这个问题. 矩阵的逆元怎么求呢? 先在原矩阵后接一 ...
- iOS开发中地图与定位
不管是QQ还是微信的移动client都少不了定位功能,之前在微信demo中没有加入定位功能,今天就写个定位的小demo来了解一下定位和地图的东西. 地图和定位看上去是挺高大上一东西.其有使用方法比Ta ...
- qs库使用
1.npm地址 https://www.npmjs.com/package/qs 2.概述 将url中的参数转为对象: 将对象转为url参数形式 3.示例 import qs from 'qs'; c ...
- Battery Charging Specification 1.2 中文详解
转: http://blog.csdn.net/liglei/article/details/228 1. Introduction 1.1 Scope 规范定义了设备通过USB端口充电的 ...
- mybatis数据查询返回值
查询: 返回值是整数. 小于0是查询的数据不存在,大于0是查询的数据已经存在. 修改: 返回值是整数. 大于0是修改的数据成功,否则就是失败. 添加: 和修改同理.
- NGINX下如何自定义404页面
什么是404页面 如果碰巧网站出了问题,或者用户试图访问一个并不存在的页面时,此时服务器会返回代码为404的错误信息,此时对应页面就是404页面.404页面的默认内容和具体的服务器有关.如果后台用的是 ...