Problem Description
给定K个整数的序列{ N1, N2, ..., NK },其任意连续子序列可表示为{ Ni, Ni+1, ..., Nj },其中 1 <= i <= j <= K。最大连续子序列是所有连续子序列中元素和最大的一个, 例如给定序列{ -2, 11, -4, 13, -5, -2 },其最大连续子序列为{ 11, -4, 13 },最大和 为20。 在今年的数据结构考卷中,要求编写程序得到最大和,现在增加一个要求,即还需要输出该 子序列的第一个和最后一个元素。
 
Input
测试输入包含若干测试用例,每个测试用例占2行,第1行给出正整数K( < 10000 ),第2行给出K个整数,中间用空格分隔。
 
Output
对每个测试用例,在1行里输出最大和、最大连续子序列的第一个和最后一个元 素,中间用空格分隔。如果最大连续子序列不唯一,则输出序号i和j最小的那个(如输入样例的第2、3组)。若所有K个元素都是负数,则定义其最大和为0,输出整个序列的首尾元素。
 
Sample Input
6
-2 11 -4 13 -5 -2
 
10
-10 1 2 3 4 -5 -23 3 7 -21
 
6
5 -8 3 2 5 0
 
1
10
 
3
-1 -5 -2
 
3 -
1 0 -2
 
Sample Output
20 11 13
10 1 4
10 3 5
10 10 10
0 -1 -2
0 0 0
 
我用树状数组写的
code:

#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【最大连续子序列+树状数组解决】的更多相关文章

  1. PAT 1007 Maximum Subsequence Sum 最大连续子序列和

    Given a sequence of K integers { N1, N2, …, NK }. A continuous subsequence is defined to be { Ni, Ni ...

  2. PAT 1007 Maximum Subsequence Sum (最大连续子序列之和)

    Given a sequence of K integers { N1, N2, ..., *N**K* }. A continuous subsequence is defined to be { ...

  3. poj2352树状数组解决偏序问题

    树状数组解决这种偏序问题是很厉害的! /* 输入按照y递增,对于第i颗星星,它的level就是之前出现过的星星中,横坐标小于i的总数 */ #include<iostream> #incl ...

  4. 树状数组解决LIS---O(nlogn)

    树状数组解决LIS---O(nlogn)之前写过二分查找的LIS,现在不怎么记得了,正好用Bit来搞一波.f[i]表示以a[i]结尾的LIS的长度.t[x]表示以数值x结尾的LIS的长度.即t[x]= ...

  5. CF452F Permutations/Luogu2757 等差子序列 树状数组、Hash

    传送门--Luogu 传送门--Codeforces 如果存在长度\(>3\)的等差子序列,那么一定存在长度\(=3\)的等差子序列,所以我们只需要找长度为\(3\)的等差子序列.可以枚举等差子 ...

  6. 【bzoj5157】[Tjoi2014]上升子序列 树状数组

    题目描述 求一个数列本质不同的至少含有两个元素的上升子序列数目模10^9+7的结果. 题解 树状数组 傻逼题,离散化后直接使用树状数组统计即可.由于要求本质不同,因此一个数要减去它前一次出现时的贡献( ...

  7. bzoj5157: [Tjoi2014]上升子序列(树状数组LIS)

    5157: [Tjoi2014]上升子序列 题目:传送门 题解: 学一下nlogn的树状数组求最长上生子序列就ok(%爆大佬) 离散化之后,用一个数组记录一下,直接树状数组做 吐槽:妈耶...一开始不 ...

  8. 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] ...

  9. 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), ...

随机推荐

  1. Carrot2 in action 初步印象

    RawCluster:聚类中的类别单位 RawCluster.getDocuments():获得该类的文档列表 RawDocument:每个类的文档单位 STC:后缀树表示法 2008-11-13 C ...

  2. 二、Android应用的界面编程(二)布局管理器

    一.线性布局所有布局都可以作为容器类使用,因此可以调用多个重载的addView()向布局管理器中添加组件.实际上,我们完全可以用一个布局管理器嵌套到其他布局管理器中---因为布局管理器也继承了View ...

  3. 解决IE,z-index失效

    在影响显示顺序的模块加上: style="position:relative;z-index:-1;" 解决IE,z-index失效

  4. git常用配置项

    1.默认的编辑器:core.editor git config --global core.editor emacs 2.默认提交模版:commit.template 假设你创建了一个叫 ~/.git ...

  5. 教你管理SQL实例系列(1-15)

    全系列转自:51CTO ->jimshu http://jimshu.blog.51cto.com 目录及原本连接如下: 教你管理SQL实例(1)数据库实例 教你管理SQL实例(2)服务启动帐户 ...

  6. 002-java反编译工具jd-gui

    官网:https://github.com/java-decompiler 下载:https://github.com/java-decompiler/jd-gui/releases 使用: java ...

  7. 使用Kotlin开发Android应用 - 环境搭建 (1)

    一. 在Android Studio上安装Kotlin插件 按快捷键Command+, -> 在Preferences界面找到Plugins -> 点击Browse repositorie ...

  8. Latex技巧:LaTex插图命令includegraphics参数详解

    Latex插图的命令是\includegraphics[选项]{文件} 这里的选项在表 7.1, 7.2, 7.3 中列出. 因为 \includegraphics 不会结束 当前段落,所以它能够在文 ...

  9. Python基础-生成器和迭代器

    生成器都是迭代器,迭代器不一定是生成器 def fansik(max): n, before, after = 0, 0, 1 while n < max: print(before) befo ...

  10. 标准c时间与日期函数

    标准c时间与日期函数 asctime 语法:     #include <time.h>   char *asctime( const struct tm *ptr ); 功能: 函数将p ...