PAT -- 甲级1007(1007 Maximum Subsequence Sum)

1007 Maximum Subsequence Sum (25 分)

Given a sequence of KKK integers { N1N_1N1, N2N_2N2, ..., NKN_KNK }. A continuous subsequence is defined to be { NiN_iNi, Ni+1N_{i+1}Ni+1, ..., NjN_jNj } where 1≤i≤j≤K1 \le i \le j \le K1ijK. 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 KKK (≤10000\le 1000010000). The second line contains KKK 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 iii and jjj (as shown by the sample case). If all the KKK 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

给一个数n,接下来有n个整数,问这些数字的最大连续子段和为多少,输出三个数字分别为最大子段和、该最大子段第一个数字、最后一个数字。存在多个最大子段和则输出靠前的子段,如果序列的每个数字全为负数则输出的最大子段和为0,并且输出该序列的第一个数字以及最后一个数字。

思路

  • 这种题有两种做法,一个是在线,一个是离线,关于离线的做法是要写出状态转移方程,然后利用前缀和。这里采取在线的做法,策略如下:枚举每个位置的值,如果该数字加到我目前的和(sum)中,使得sum大于0的话,我就接着考虑下一个,否则如果小于0了,那么子段和就从下个位置开始考虑。每次个位置操作完毕后都要判断是否能够更新全局的max。
  • 坑点:
    1,当有个0,其他都为负数要输出0 0 0
    2,输出的是头和尾的下标的值而不是下下标

Code:

#include <iostream>
#include <algorithm>

using namespace std;
const int maxn = (int)1e4+5;
const int inf = (1 << 31);
int val[maxn], n;

int DP (int& l, int& r) {
	int nl, nr, tmp = 0;
	int mmax = 0;
	bool flag = false;
	nl = l = 0;
	nr = r = n - 1;
	for (int i = 0; i < n; i++) {
		tmp += val[i];
		if (tmp >= 0) {
			nr = i;
			if (!flag) {
				l = i;
				r = i;
				flag = true;
			}
		} else {
			tmp = 0;
			nl = i + 1;
			nr = i + 1;
		}

		if (tmp > mmax) {
			mmax = tmp;
			l = nl;
			r = nr;
		}
	}
	return mmax;
}

int main() {
	int l, r;
	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> val[i];
	}
	cout << DP(l, r);
	cout << " " << val[l] << " " << val[r] << endl;
	return 0;
}
全部评论

相关推荐

自从我室友在计算机导论课上听说了“刷&nbsp;LeetCode&nbsp;是进入大厂的敲门砖”,整个人就跟走火入魔了一样。他在宿舍门口贴了一张A4纸,上面写着:“正在&nbsp;DP,请勿打扰,否则&nbsp;Time&nbsp;Limit&nbsp;Exceeded。”日记本的扉页被他用黑色水笔加粗描了三遍:“Talk&nbsp;is&nbsp;cheap.&nbsp;Show&nbsp;me&nbsp;the&nbsp;code。”连宿舍聚餐,他都要给我们讲解:“今天的座位安排可以用回溯算法解决,但为了避免栈溢出,我建议用动态规划。来,这是状态转移方程:dp[i][j]&nbsp;代表第&nbsp;i&nbsp;个人坐在第&nbsp;j&nbsp;个位置的最优解。”我让他去楼下取个快递,他不直接去,非要在门口踱步,嘴里念念有词:“这是一个图的遍历问题。从宿舍楼(root)到驿站(target&nbsp;node),我应该用&nbsp;BFS&nbsp;还是&nbsp;DFS?嗯,求最短路径,还是广度优先好。”和同学约好出去开黑,他会提前发消息:“集合点&nbsp;(x,&nbsp;y),我们俩的路径有&nbsp;k&nbsp;个交点,为了最小化时间复杂度,应该在&nbsp;(x/2,&nbsp;y/2)&nbsp;处汇合。”有一次另一个室友低血糖犯了,让他帮忙找颗糖,他居然冷静地分析道:“别急,这是一个查找问题。零食箱是无序数组,暴力查找是&nbsp;O(n)。如果按甜度排序,我就可以用二分查找,时间复杂度降到&nbsp;O(log&nbsp;n)。”他做卫生也要讲究算法效率:“拖地是典型的岛屿问题,要先把连通的污渍区块都清理掉。倒垃圾可以用双指针法,一个指针从左往右,一个从右往左,能最快匹配垃圾分类。”现在我们宿舍的画风已经完全变了,大家不聊游戏和妹子,对话都是这样的:“你&nbsp;Two&nbsp;Sum&nbsp;刷了几遍了?”“别提了,昨天遇到一道&nbsp;Hard&nbsp;题,我连暴力解都想不出来,最后只能看题解。你呢?”“我动态规划还不行,总是找不到最优子结构。今天那道接雨水给我整麻了。”……LeetCode&nbsp;真的害了我室友!!!
老六f:编程嘉豪来了
AI时代还有必要刷lee...
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
03-20 12:46
瘦嘟嘟右卫门:百度文库网盘的暑期也没约面吗
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务