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), ...
随机推荐
- Carrot2 in action 初步印象
RawCluster:聚类中的类别单位 RawCluster.getDocuments():获得该类的文档列表 RawDocument:每个类的文档单位 STC:后缀树表示法 2008-11-13 C ...
- 二、Android应用的界面编程(二)布局管理器
一.线性布局所有布局都可以作为容器类使用,因此可以调用多个重载的addView()向布局管理器中添加组件.实际上,我们完全可以用一个布局管理器嵌套到其他布局管理器中---因为布局管理器也继承了View ...
- 解决IE,z-index失效
在影响显示顺序的模块加上: style="position:relative;z-index:-1;" 解决IE,z-index失效
- git常用配置项
1.默认的编辑器:core.editor git config --global core.editor emacs 2.默认提交模版:commit.template 假设你创建了一个叫 ~/.git ...
- 教你管理SQL实例系列(1-15)
全系列转自:51CTO ->jimshu http://jimshu.blog.51cto.com 目录及原本连接如下: 教你管理SQL实例(1)数据库实例 教你管理SQL实例(2)服务启动帐户 ...
- 002-java反编译工具jd-gui
官网:https://github.com/java-decompiler 下载:https://github.com/java-decompiler/jd-gui/releases 使用: java ...
- 使用Kotlin开发Android应用 - 环境搭建 (1)
一. 在Android Studio上安装Kotlin插件 按快捷键Command+, -> 在Preferences界面找到Plugins -> 点击Browse repositorie ...
- Latex技巧:LaTex插图命令includegraphics参数详解
Latex插图的命令是\includegraphics[选项]{文件} 这里的选项在表 7.1, 7.2, 7.3 中列出. 因为 \includegraphics 不会结束 当前段落,所以它能够在文 ...
- Python基础-生成器和迭代器
生成器都是迭代器,迭代器不一定是生成器 def fansik(max): n, before, after = 0, 0, 1 while n < max: print(before) befo ...
- 标准c时间与日期函数
标准c时间与日期函数 asctime 语法: #include <time.h> char *asctime( const struct tm *ptr ); 功能: 函数将p ...