1007 Maximum Subsequence Sum (PAT(Advance))
Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to be { Ni, Ni+1, ..., Nj } 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))的更多相关文章
- python编写PAT 1007 Maximum Subsequence Sum(暴力 分治法 动态规划)
python编写PAT甲级 1007 Maximum Subsequence Sum wenzongxiao1996 2019.4.3 题目 Given a sequence of K integer ...
- 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 ...
- 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 ...
- PAT 1007 Maximum Subsequence Sum(最长子段和)
1007. Maximum Subsequence Sum (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...
- PAT Advanced 1007 Maximum Subsequence Sum
题目 1007 Maximum Subsequence Sum (25分) Given a sequence of K integers { N1, N2, ..., N**K }. A contin ...
- 1007 Maximum Subsequence Sum (25分) 求最大连续区间和
1007 Maximum Subsequence Sum (25分) Given a sequence of K integers { N1, N2, ..., NK }. A ...
- 1007 Maximum Subsequence Sum (25 分)
1007 Maximum Subsequence Sum (25 分) Given a sequence of K integers { N1, N2, ..., NK }. A ...
- PTA (Advanced Level) 1007 Maximum Subsequence Sum
Maximum Subsequence Sum Given a sequence of K integers { N1, N2, ..., NK }. A continuous su ...
- PAT 解题报告 1007. Maximum Subsequence Sum (25)
Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to be { Ni, ...
随机推荐
- 【Poj1017】Packets
http://poj.org/problem?id=1017 艰难啊 弄了很久咧 拍了几十万组,以后拍要多组数据 Solution 从大wangxiaofang 从大往小放,有空余的从大往小填 注意细 ...
- 在mac下设置环境变量
在mac下设置环境变量 在基于unix/linux的操作系统下进行程序开发,使用环境变量将会方便.通过设置环境变量将可以在任意目录通过输入程序名来执行设定目录下的程序.不需要通过cd将工作目录改变 ...
- 【POJ 2983】 Is the information reliable?
[题目链接] 点击打开链接 [算法] 差分约束系统,SPFA判负环 [代码] #include <algorithm> #include <bitset> #include & ...
- POJ2115 C-Loop
传送门 这道题是求解不定方程的一道好练习题. 题目描述的很诡异……还说什么k进制,其实就是要求一个数A,每次加C,问到B要加多少次,所有的数对2k取模. 也就是说我们能列出如下方程:A+xC ≡ B ...
- RDA DEBUG
DEBUG寄存器:word 0xa0000010 word 0xa0000010 1 //debug开 word 0xa0000010 0 //debug关 当然也可以按模块打开/关闭debug信息, ...
- Vue.js实战 5.5章 购物车
<!DOCTYPE html> <html lang="en"> <head> <title>购物车示例</title> ...
- IFRAME动态加载触发onload事件(转)
原文地址:http://blog.ops.cc/webtech/javascript/f5nhm.html <body> <script>var iframe = docume ...
- java运行代码连接mysql时提示:找不到类错误
使用IntelliJ IDEA Community Edition进行代码编写.. 使用一下代码连接mysql时出现了:java.lang.ClassNotFoundException: com.my ...
- Postman发送GET请求带中文
当使用Postman进行GET请求,并且请求参数里携带中文得时候,会请求失败 这时,需要对GET请求参数携带的中文进行编码即可请求成功
- 清北考前刷题day1早安
立方数(cubic) Time Limit:1000ms Memory Limit:128MB 题目描述 LYK定义了一个数叫“立方数”,若一个数可以被写作是一个正整数的3次方,则这个数就是立方数 ...