1007 Maximum Subsequence Sum (25 分)
 

Given a sequence of K integers { N​1​​, N​2​​, ..., N​K​​ }. A continuous subsequence is defined to be { N​i​​, N​i+1​​, ..., N​j​​ } where 1≤i≤j≤K. The Maximum Subsequence is the continuous subsequence which has the largest sum of its elements. For example, given sequence { -2, 11, -4, 13, -5, -2 }, its maximum subsequence is { 11, -4, 13 } with the largest sum being 20.

Now you are supposed to find the largest sum, together with the first and the last numbers of the maximum subsequence.

Input Specification:

Each input file contains one test case. Each case occupies two lines. The first line contains a positive integer K (≤10000). The second line contains K numbers, separated by a space.

Output Specification:

For each test case, output in one line the largest sum, together with the first and the last numbers of the maximum subsequence. The numbers must be separated by one space, but there must be no extra space at the end of a line. In case that the maximum subsequence is not unique, output the one with the smallest indices i and j (as shown by the sample case). If all the K numbers are negative, then its maximum sum is defined to be 0, and you are supposed to output the first and the last numbers of the whole sequence.

Sample Input:

10
-10 1 2 3 4 -5 -23 3 7 -21

Sample Output:

10 1 4


求最大的连续子序列的和,并输出子序列的首尾元素,就是没注意这点,一直输出首尾索引号,一直WA
思路有两种:
1)子序列的和sum = sum[j] - sum[i] (j >= i),所以只需在求每个sum[j]的时候,找到j前面最小的那个sum[i],即可,这里的最小的sum[i]应该与求sum[j]的时候同时维护,否则就是暴力破解。
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define MAXN 10005
struct node{
int num;
int l;
int r;
int l_sum;
} seq[MAXN];
int sum[MAXN];
int main()
{
int n;
int flag = ;
int min;
int index;
min = ;
index = -;
scanf("%d", &n);
for (int i = ; i < n; i++){
scanf("%d", &seq[i].num);
if(seq[i].num >= )
flag = ;
sum[i] = sum[i - ] + seq[i].num;
seq[i].l_sum = sum[i] - min;
seq[i].l = index + ;
seq[i].r = i;
if(sum[i] < min){
min = sum[i];
index = i;
}
}
if(!flag){
printf("0 %d %d\n", seq[].num, seq[n - ].num);
}
else{
int max;
int l, r;
max = -;
for (int i = ; i < n; i++){
if(max < seq[i].l_sum){
max = seq[i].l_sum;
l = seq[i].l;
r = seq[i].r;
}
}
printf("%d %d %d\n", max, seq[l].num, seq[r].num);
}
system("pause");
return ;
}

2)见https://blog.csdn.net/liuchuo/article/details/52144554



1007 Maximum Subsequence Sum (PAT(Advance))的更多相关文章

  1. python编写PAT 1007 Maximum Subsequence Sum(暴力 分治法 动态规划)

    python编写PAT甲级 1007 Maximum Subsequence Sum wenzongxiao1996 2019.4.3 题目 Given a sequence of K integer ...

  2. PAT甲 1007. Maximum Subsequence Sum (25) 2016-09-09 22:56 41人阅读 评论(0) 收藏

    1007. Maximum Subsequence Sum (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...

  3. PAT 甲级 1007 Maximum Subsequence Sum (25)(25 分)(0不是负数,水题)

    1007 Maximum Subsequence Sum (25)(25 分) Given a sequence of K integers { N~1~, N~2~, ..., N~K~ }. A ...

  4. PAT 1007 Maximum Subsequence Sum(最长子段和)

    1007. Maximum Subsequence Sum (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...

  5. PAT Advanced 1007 Maximum Subsequence Sum

    题目 1007 Maximum Subsequence Sum (25分) Given a sequence of K integers { N1, N2, ..., N**K }. A contin ...

  6. 1007 Maximum Subsequence Sum (25分) 求最大连续区间和

    1007 Maximum Subsequence Sum (25分)   Given a sequence of K integers { N​1​​, N​2​​, ..., N​K​​ }. A ...

  7. 1007 Maximum Subsequence Sum (25 分)

    1007 Maximum Subsequence Sum (25 分)   Given a sequence of K integers { N​1​​, N​2​​, ..., N​K​​ }. A ...

  8. PTA (Advanced Level) 1007 Maximum Subsequence Sum

    Maximum Subsequence Sum Given a sequence of K integers { N​1​​, N​2​​, ..., N​K​​ }. A continuous su ...

  9. PAT 解题报告 1007. Maximum Subsequence Sum (25)

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

随机推荐

  1. YTU 2517: 打倒魔王↖(^ω^)↗

    2517: 打倒魔王↖(^ω^)↗ 时间限制: 1 Sec  内存限制: 128 MB 提交: 231  解决: 112 题目描述 从前有一个王子,他喜欢上了邻国的一个公主.终于有一天他向公主表白了, ...

  2. Scala 归约操作- - - - -reduce

    object 归约操作_reduce { def main(args: Array[String]): Unit = { val list=List(,,,,) val result=list.red ...

  3. samba - linux客户端访问samba服务器的指令(转载)

    转自:http://linux.sheup.com/linux/linux5303.htm linux客户端访问samba服务器的指令2004-04-23 15:18 pm来自:Linux文档现载:W ...

  4. (前缀和 内存分配)51NOD 1081 子段求和

    给出一个长度为N的数组,进行Q次查询,查询从第i个元素开始长度为l的子段所有元素之和.   例如,1 3 7 9 -1,查询第2个元素开始长度为3的子段和,1 {3 7 9} -1.3 + 7 + 9 ...

  5. codechef: ADAROKS2 ,Ada Rooks 2

    又是道原题... (HDU 6313 Hack It , 多校 ACM 里面的题) 题目说构造一个 n * n 矩阵,染色点不得构成矩形...然后染色点个数至少 8 * n 然后我们生成一个数 m , ...

  6. $CF1153A\ Serval\ and\ Bus$

    看大佬的代码都好复杂(不愧是大佬\(orz\) 蒟蒻提供一种思路 因为求的是最近的车对吧\(qwq\) 所以我们可以用一个\(while\)循环所以没必要去用什么 \(for...\) 至于这是\(d ...

  7. Javascript 排序算法(转)

    1.快速排序 class QuickSort { Sort(originalArray) { // 复制 originalArray 数组防止它被修改 const array = [...origin ...

  8. web项目无法部署到eclipse配置的本地tomcat

    一.发现问题 在eclipse中新建Dynamic Web Project,配置好本地的tomcat并写好代码后选择Run on Server,但运行后发现在tomcat的安装目录下的webapps并 ...

  9. Ceph在手,天下我有

    有人问我,你是如何做到统一存储的?我微微一笑,大声告诉他:Ceph在手,天下我有. Ceph是一个统一的分布式存储系统,旨在实现出色的性能,可靠性和可扩展性.认了OpenStack做大哥之后更是一发不 ...

  10. js实现左右点击图片层叠滚动特效

    需要加载js有 <script type="text/javascript" src="js/jquery-1.7.2.min.js"></s ...