Maximum Subsequence Sum【最大连续子序列+树状数组解决】
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <algorithm> using namespace std; int lowbit(int x)
{
return x&(-x);
} int a[100000+10];
int c[100000+10];
int s[100000+10]; int Sum(int x)
{
int s=0;
while(x>0)
{
s+=c[x];
x=x-lowbit(x);
}
return s;
} bool fu(int *a, int n)
{
for(int i=1; i<=n; i++)
{
if(a[i]>=0) return false;
}
return true;
}
int main()
{
int n;
int i, j;
while(scanf("%d", &n)!=EOF)
{
for(i=1; i<=n; i++)
{
scanf("%d", &a[i]);
}
if(fu(a, n)==true)
{
printf("0 %d %d\n", a[1], a[n]);
continue;
}
for(i=1; i<=n; i++)
{
c[i]=0;
for(j=i-lowbit(i)+1; j<=i; j++)
c[i]+=a[j];
//printf("c[%d]=%d\n", i, c[i]);
}
int ans=-1;
for(i=1; i<=n; i++)
s[i]=Sum(i); int left, right;
for(i=1; i<=n; i++)
{
int dd=s[i], ff=0;
if(dd>ans)
{
ans=dd;
left=a[1];
right=a[i];
}
for(j=1; j<i; j++)
{
ff=s[j];
if(dd-ff > ans)
{
ans = dd-ff;
left=a[j+1];
right=a[i];
}
}
}
printf("%d %d %d\n", ans, left, right);
}
return 0;
}
Maximum Subsequence Sum【最大连续子序列+树状数组解决】的更多相关文章
- PAT 1007 Maximum Subsequence Sum 最大连续子序列和
Given a sequence of K integers { N1, N2, …, NK }. A continuous subsequence is defined to be { Ni, Ni ...
- PAT 1007 Maximum Subsequence Sum (最大连续子序列之和)
Given a sequence of K integers { N1, N2, ..., *N**K* }. A continuous subsequence is defined to be { ...
- poj2352树状数组解决偏序问题
树状数组解决这种偏序问题是很厉害的! /* 输入按照y递增,对于第i颗星星,它的level就是之前出现过的星星中,横坐标小于i的总数 */ #include<iostream> #incl ...
- 树状数组解决LIS---O(nlogn)
树状数组解决LIS---O(nlogn)之前写过二分查找的LIS,现在不怎么记得了,正好用Bit来搞一波.f[i]表示以a[i]结尾的LIS的长度.t[x]表示以数值x结尾的LIS的长度.即t[x]= ...
- CF452F Permutations/Luogu2757 等差子序列 树状数组、Hash
传送门--Luogu 传送门--Codeforces 如果存在长度\(>3\)的等差子序列,那么一定存在长度\(=3\)的等差子序列,所以我们只需要找长度为\(3\)的等差子序列.可以枚举等差子 ...
- 【bzoj5157】[Tjoi2014]上升子序列 树状数组
题目描述 求一个数列本质不同的至少含有两个元素的上升子序列数目模10^9+7的结果. 题解 树状数组 傻逼题,离散化后直接使用树状数组统计即可.由于要求本质不同,因此一个数要减去它前一次出现时的贡献( ...
- bzoj5157: [Tjoi2014]上升子序列(树状数组LIS)
5157: [Tjoi2014]上升子序列 题目:传送门 题解: 学一下nlogn的树状数组求最长上生子序列就ok(%爆大佬) 离散化之后,用一个数组记录一下,直接树状数组做 吐槽:妈耶...一开始不 ...
- Mishka and Interesting sum Codeforces Round #365 (树状数组)
树状数组,与Turing Tree类似. xr[i]表示从1到i的抑或,树状数组维护从1到i每个数只考虑一次的异或,结果为sum(r) ^ sum(l) ^ xr[r] ^ xr[l] 其中xr[r] ...
- Leetcode 2——Range Sum Query - Mutable(树状数组实现)
Problem: Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), ...
随机推荐
- Linq to SQL 语法查询(子查询 & in操作 & join )
var 子查询 = from c in ctx.Customers where (from o in ctx.Ord ...
- Python_Selenium之鼠标右键
Python_Selenium之鼠标右键 一.步骤: (以百度为例)获取百度网址 找到需要右键的元素(定位),xpath表达式为“//*[@id='lg']/img” 然后,右键选择“在新标签页中打开 ...
- 利用Lucene将被索引文件目录中的所有文件建立索引
1.新建两个文件夹htm和index,其中htm中存放被索引的文件,index文件中存放建立的索引文件. 2.新建解析目录中所有文件的类,用来解析指定目录下的所有文件. import java.io. ...
- http://www.haolizi.net/example/view_2380.html
null
- Linux 的字符串截取很有用。有八种方法。
假设有变量 var=http://www.aaa.com/123.htm 1. # 号截取,删除左边字符,保留右边字符. echo ${var#*//} 其中 var 是变量名,# 号是运算符,*// ...
- svn 更新文件冲突,提示中文乱码解决
问题描述: update 操作提示错误信息,中文乱码 和 “Please execute the 'Cleanup' command.” Cleanup 操作报错: 解决办法: 1. 工具下载(sql ...
- 【BZOJ2186】[Sdoi2008]沙拉公主的困惑 线性筛素数
[BZOJ2186][Sdoi2008]沙拉公主的困惑 Description 大富翁国因为通货膨胀,以及假钞泛滥,政府决定推出一项新的政策:现有钞票编号范围为1到N的阶乘,但是,政府只发行编号与M! ...
- POJ 3026 Borg Maze【BFS+最小生成树】
链接: http://poj.org/problem?id=3026 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ...
- Cookies with curl the command line tool
w https://curl.haxx.se/docs/http-cookies.html curl has a full cookie "engine" built in. If ...
- Python菜鸟之路:DOM基础
前言 DOM 是 Document Object Model(文档对象模型)的缩写,定义了访问和操作 HTML 文档的标准方法.DOM把网页和脚本以及其他的编程语言联系了起来.DOM属于浏览器,而不是 ...