PAT A1007Maximum Subsequence Sum(贪心算法)
来源:互联网 发布:氨基酸数据库 编辑:程序博客网 时间:2024/06/07 15:25
1007. Maximum Subsequence Sum (25)
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 -21Sample Output:
10 1 4
#include <stdio.h>
int main() {
int num[10000];
int path[10000][3];
int K, i, temp, start, end, max, max_start, max_end;
scanf("%d", &K);
for(i = 0; i < K; i ++) {
scanf("%d", &num[i]);
}
for(i = 0; i < K; i ++) {
if(i == 0) {
path[0][0] = num[0];
path[0][1] = 0;
path[0][2] = 0;
if(num[0] >= 0) {
temp = num[0];
start = 0;
end = 0;
} else {
temp = -1;
}
continue;
}
if(num[i] < 0) {
path[i][0] = path[i - 1][0];
path[i][1] = path[i - 1][1];
path[i][2] = path[i - 1][2];
if(temp != -1) {
if(temp + num[i] >= 0) {
temp = temp + num[i];
end = i;
} else {
temp = -1;
}
}
} else {
if(temp != -1) {
if(temp + num[i] > path[i - 1][0]) {
path[i][0] = temp + num[i];
path[i][1] = start;
path[i][2] = i;
temp = path[i][0];
start = path[i][1];
end = i;
} else {
path[i][0] = path[i - 1][0];
path[i][1] = path[i - 1][1];
path[i][2] = path[i - 1][2];
if(temp + num[i] >= 0) {
temp = temp + num[i];
end = i;
} else {
temp = num[i];
start = i;
end = i;
}
}
} else {
if(num[i] > path[i - 1][0]) {
path[i][0] = num[i];
path[i][1] = i;
path[i][2] = i;
} else {
path[i][0] = path[i - 1][0];
path[i][1] = path[i - 1][1];
path[i][2] = path[i - 1][2];
}
temp = num[i];
start = i;
end = i;
}
}
}
for(i = 0; i < K; i ++) {
if(i == 0) {
max = path[0][0];
max_start = path[0][1];
max_end = path[0][2];
continue;
}
if(max < path[i][0]) {
max = path[i][0];
max_start = path[i][1];
max_end = path[i][2];
}
}
if(max < 0) {
printf("0 %d %d\n", num[0], num[K - 1]);
} else {
printf("%d %d %d\n", max, num[max_start], num[max_end]);
}
return 0;
}
- PAT A1007Maximum Subsequence Sum(贪心算法)
- PAT A 1007. Maximum Subsequence Sum(贪心算法应用path[i] 与 path[i + 1)
- PAT 1007(甲级)Maximum Subsequence Sum
- PAT - Maximum Subsequence Sum
- pat 1007 Maximum Subsequence Sum
- pat 1007 Maximum Subsequence Sum
- (PAT)1007. Maximum Subsequence Sum
- (PAT)1007. Maximum Subsequence Sum
- pat 1007. Maximum Subsequence Sum
- PAT 1007. Maximum Subsequence Sum
- PAT Maximum Subsequence Sum (25)
- PAT 1007. Maximum Subsequence Sum
- PAT 1007. Maximum Subsequence Sum
- PAT Maximum Subsequence Sum (25)
- PAT Maximum subsequence sum (Python)
- PAT 1007. Maximum Subsequence Sum
- 【PAT】1007. Maximum Subsequence Sum
- PAT A1007. Maximum Subsequence Sum
- 3.7
- win7+ubuntu14.04双系统,重装win7后,修复grub方法
- mysql数据库备份
- [统计学习方法]决策树
- 关于typedef的用法总结
- PAT A1007Maximum Subsequence Sum(贪心算法)
- windows下字符集与编码方案
- Linux内核分析(1)
- VMware自动更新文件位置
- 【机房收费系统】——类型的转换
- Java:集合的作用
- LeetCode(62): Unique Paths
- 安卓蓝牙API(6)
- 1200 最大的两个数