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.求出某一个数的和. 解法:树状数组+前缀和优化.数组中每位存和前一位的数的差,这样区间修改只用改两位,单点询问就是求前缀和 ...
随机推荐
- mootools客户端框架
mootools客户端框架 学习:http://www.chinamootools.com/ 官网:https://mootools.net/ 下载地址: https://github.com/moo ...
- 香蕉派(or 皮?)上手初体验 -- 外观鉴赏,安装,配置&总结
一.前言及简单介绍 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbG9uZ2Vyem9uZQ==/font/5a6L5L2T/fontsize/400/f ...
- 【剑指Offer面试题】 九度OJ1368:二叉树中和为某一值的路径
题目链接地址: http://ac.jobdu.com/problem.php? pid=1368 题目1368:二叉树中和为某一值的路径 时间限制:1 秒内存限制:32 兆特殊判题:否提交:2252 ...
- Android API Guides---Layouts
布局定义了视觉结构的用户界面.如活动或应用程序插件的用户界面. 您能够通过两种方式申报的布局: 声明在XML UI元素. Android提供了相应视图类和子类,如那些部件和布局一个简单的XML词汇表. ...
- es6 includes(), startsWith(), endsWith()
传统上,JavaScript 只有indexOf方法,可以用来确定一个字符串是否包含在另一个字符串中.ES6 又提供了三种新方法. includes():返回布尔值,表示是否找到了参数字符串. sta ...
- Windows Thin PC体验 & 语言包更改(win 7 included)
本作品由Man_华创作,采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可.基于http://www.cnblogs.com/manhua/上的作品创作. 简介: Window ...
- java移位运算符详解
http://soft.chinabyte.com/database/195/11553695.shtml java移位运算符不外乎就这三种:<<(左移).>>(带符号右移)和 ...
- Android学习笔记(24):进度条组件ProgressBar及其子类
ProgressBar作为进度条组件使用,它还派生了SeekBar(拖动条)和RatingBar(星级评分条). ProgressBar支持的XML属性: Attribute Name Related ...
- iOS 从UITableViewController中分离数据源
之前看objc.io #1 Light View Controllers看到一个非常不错的技巧:从UITableViewController中分离数据源,这样能够减小UITableViewContro ...
- ubuntu下matlab的无界面启动---命令行操作
命令行下运行 Matlab 及 函数 首先参考命令行下matlab的运行参数的定义与作用:http://www.cnblogs.com/beanocean/p/3677404.html 创建示例程序: ...