PAT 1007. Maximum Subsequence Sum (25)

告别外置电阻!揭秘MAX22201集成电流检测的5大设计优势(附PCB布局案例) 本文深入解析MAX22201集成电流检测技术在直流有刷电机驱动中的5大设计优势,包括BOM成本降低、PCB空间优化、动态性能提升等。通过与传统方案(如DRV8871+外置电阻)的实测对比,展示其在高精度电流检测、抗干扰能力和PCB布局简化方面的突破,并附有详细的PCB布局案例和配置指南。 阅读详情

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

#include <iostream>
using namespace std;

int main()
{
	int K;
	long long int maxValue = -1;
	long long int before = -10;
	cin >> K;
	long long int tmp = 0;

	int start = 0, end = 0;
	int maxstart = 0, maxend = 0;
	int firstnum = 0, lastnum = 0;
	int i;
	for (i = 0;i<K; i++)
	{
		cin >> tmp;
		if (i == 0)
		{
			firstnum = tmp;
		}
		if (i == K-1)
		{
			lastnum = tmp;
		}
		if (before > 0)
		{
			before += tmp;
			end = tmp;
		}
		else
		{
			before = tmp;
			start = tmp;
			end = tmp;
		}

		if (i == 0|| before > maxValue)
		{
			maxValue = before;
			maxstart = start;
			maxend = end;
		}
	}

	if (maxValue < 0)
	{
		maxValue = 0;
		maxstart = firstnum;
		maxend = lastnum;
	}
	//printf("%lld %lld %lld", maxValue, maxstart, maxend);
	cout << maxValue << " "<< maxstart << " "<< maxend;
	return 0;
}


心!!好!!累!!

再做一遍怎么都做不出来、、、、、、、、、、、老是有一个case无法通过。。。。。。。。

附上错误的代码:

#include <iostream>
	using namespace std;

int main(void)
{
	int K;
	int i;
	long long int n[10005];
	scanf("%d", &K);
	for (i = 0;i<K;i++)
	{
		scanf("%lld", &n[i]);
	}

	long long int result = n[0];
	int start = 0;
	for (i = 0;i<K;i++)
	{
		if (n[i] >= 0)
		{
			break;
		}

		if (result < n[i])
		{
			result = n[i];
			start = i;
		}
	}

	if (n[start] < 0 && i == K)
	{
		//printf("%d %d %d", result, start, start);
		printf("0 0 %d", K-1);
		return 0;
	}

	int end = i;
	//result = n[i];
	result = 0;
	start = i;
	int maxstart = start;
	int maxend = end;

	long long int maxValue = 0;
	while(i<K)
	{
		if (result + n[i] < 0)
		{
			result = 0;
			start = i + 1;
			end = i + 1;
		}
		else
		{
			result += n[i];
			end = i;
			if (maxValue < result)
			{
				maxValue = result;
				maxend = end;
				maxstart = start;
			}
		}
		i++;
	}

	printf("%lld %lld %lld",maxValue, n[maxstart], n[maxend]);
	return 0;
}


初识colmap COLMAP是一款开源的计算机视觉软件,主要用于从一系列二维图像中进行三维重建。它全称为“COLLISION-MAPping”,是一个高度集成的Structure-from-Motion (SfM) 和 Multi-View Stereo (MVS) 管道工具,由斯坦福大学开发和维护。COLMAP的主要功能包括特征检测与匹配、增量式SfM、多视图立体匹配以及优化。它能够从无序或有序的二维照片集合中恢复出三维场景的几何结构(点云)以及每张图像对应的相机姿态。 阅读详情

相关推荐

C++学习(四六六)Multiple parse contexts are available for this file

Note: Multiple parse contexts are available for this file. Choose the preferred one from the editor menu 参考: "Multiple parse contexts" in Qt creator | Qt ForumHi Do you use sessions? https://bugreports.qt.io/browse/QTCREATORBUG-18521 Anyway, its not.

hankern的专栏 4610

【PAT甲级】1007 Maximum Subsequence Sum (25 分)

Maximum Subsequence Sum 题目描述 Given a sequence of K integers { N​1 , N​2, …, NK }. A continuous subsequence is defined to be { N​i​ , N​i+1​​ , …, Nj​​ } where 1≤i≤j≤K. The Maximum Subsequence is the...

初见还是重逢的博客 392

利用python爬取东方财富网股吧评论并进行情感分析(一)

利用python爬取东方财富网股吧评论(一) python-东方财富网贴吧文本数据爬取 分享一下写论文时爬数据用到的代码,有什么问题或者改善的建议的话小伙伴们一起评论区讨论。 新的改变 我们对Markdown编辑器进行了一些功能拓展与语法支持,除了标准的Markdown编辑器功能,我们增加了如下几点新功能,帮助你用它写博客: 全新的界面设计 ,将会带来全新的写作体验; 在创作中心设置你喜爱的代码高亮样式,Markdown 将代码片显示选择的高亮样式 进行展示; 增加了 图片拖拽 功能,你可以将本地的图片直

YMG521000的博客 1万+

【PAT】1007. Maximum Subsequence Sum (25)

题目链接:http://pat.zju.edu.cn/contests/pat-a-practise/1007 分析: (1)求最大连续子序列,最终输出最大子序列值以及子序列起始和终止元素。如果数组全为负数,则输出0以及数组的第一个和最后一个元素。 (2)一个特殊情况。如,输入的是:         4        -1 0 0 -1        那么我们输出的应该是: 0 0 0

此去经年 1814

告别双系统!用PhyFusion在麒麟V10上无缝融合Win11,文件互传、GPU加速全搞定

本文详细介绍了在飞腾D3000平台上使用PhyFusion实现麒麟V10与Win11无缝融合的解决方案。通过视觉无界、数据互通和性能无损的三大突破,PhyFusion显著提升了跨系统工作效率,支持GPU加速和文件互传,是国产化平台的革命性工具。

weixin_28707365的博客 1017

Nginx/OpenResty目录穿越漏洞复现

Nginx/OpenResty目录穿越漏洞复现 漏洞背景: 2020年03月18日, 360CERT监测发现 openwall oss-security 邮件组披露了两枚漏洞。在特定配置下 nginx/openresty 存在 内存泄漏漏洞/目录穿越漏洞。 Nginx 是异步框架的网页服务器,也可以用作反向代理、负载平衡器和HTTP缓存。 OpenResty是一个基于nginx的Web平台,它对nginx增加LuaJIT引擎使其运行Lua脚本。 影响版本: nginx <= v1.17

小小猪的博客 1万+

[PAT] 1007 Maximum Subsequence Sum (25 分)Java

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

Simma1101的博客 274

【VTKExamples::PolyData】第十六期 GradientFilter

本文分享VTK样例GradientFilter,并解析接口vtkGradientFilter & vtkAssignAttribute,希望对各位小伙伴有所帮助!

qq_40041064的博客 319

PAT甲级 1007 Maximum Subsequence Sum 25 分

PAT甲级 1007 Maximum Subsequence Sum 25 分题目思考代码 题目 原题描述 即:找到一个字符串的和最大的连续子序列,输出:和,子序列开始元素,子序列结束元素 若子序列全为负数,则输出:0,数组开头元素,数组结束元素 思考 动态规划 sum[i]表示的是以a[i]元素结尾的连续子序列的最大和 状态转移方程: sum[i] = max (sum[i-1]+a[i], a...

weixin_41699413的博客 318

2011-2024年间各省数字普惠金融指数数据(覆盖广度、使用深度和数字化程度)

2、指标:数字金融普惠指数、数字金融覆盖广度指数、数字金融使用深度指数、普惠金融数字化程度指数、数字金融使用深度指数下属的支付、保险、货币基金、信用服务、投资、信贷等分类指数。中国各省的数字普惠金融指数数据,从数字金融覆盖广度、数字金融使用深度和普惠金融数字化程度3个维度来构建数字普惠金融指标体系。数字普惠金融指数一共3个维度、33项具体指标。全套指数包含数字金融覆盖广度指数、数字金融使用深度指数和普惠金融数字化程度指数,以及数字金融使用深度指数下属的支付、保险、货币基金、信用服务、投资、信贷等分类指数。

【优快云】 1万+

LPDDR6来了!带你一文看懂它和LPDDR5/5X的差距 + 技术演进逻辑全解析

🧠从速率到架构,从总线宽度到协议设计,LPDDR6 每一个改动都不是拍脑袋做的,它针对的是AI / HPC / 云计算 / 下一代笔记本平台的真实需求。作为工程师的我们,不仅要“用得上”,更要“测得准”,因为没有测试验证,就没有量产落地!🎯LPDDR6不是未来,而是现在进行时!

weixin_43199439的博客 2173

PAT 1007 Maximum Subsequence Sum (25 分)

Maximum Subsequence Sum (25 分) 最大子列和问题

Gnomeshgh的博客 919

MIKE水动力笔记18_如何将dfsu流场模拟结果的数据导出成txt文件

MIKE的模拟结果dfsu文件的数据是可以导出的,导出格式为xyz,我们也可以将其改后缀改为txt文本格式,其中包含了某一时刻下所有网格坐标点的数据。

晏长街的博客 4410

[PAT-Advanced] A1007 Maximum Subsequence Sum (25)

A1007 Maximum Subsequence Sum (25point(s)) 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 Ma...

Duke 290

锂电池充电器电路设计 里面有电路

锂电池充电器电路设计 里面有电路 3.7V升压到5V电路设计。

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

1007 Maximum Subsequence Sum (25 分) PAT甲级

1007 Maximum Subsequence Sum (25 分) 链接: 1007 Maximum Subsequence Sum (25 分) 来源: PTA程序设计类实验辅助教学平台 题目大意:给你一个K长度的数组,要求你去找到这个数组中 最大连续子序列的和 ,然后输出这个最大的和、这个序列第一个数、这个序列最后一个数,如果这个序列都为负数,那么输出0和这个数组第一个数和最后一个数,题意还是很好理解的,具体就是怎么去做了。 我们定义maxx为我们的最大和,startx为输出的序列中第一个数,e

323的博客 355

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

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

题目链接:PAT甲级 - 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. T...

227

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

【PAT甲级】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 subsequen...

能饮一杯否 369

pat 1007. Maximum Subsequence Sum (25)

1007. Maximum Subsequence Sum (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Given a sequence of K integers { N

chen1007124127的专栏 383

PAT甲级 1007 Maximum Subsequence Sum (25 分)

题意: 给定长度为n的序列,寻找最大字段和,按要求输出 思路: 滑动窗口,当前子段sum>=0时,右指针一直往右边滑动,如果当前序列sum变成的负数,那就把左指针往右滑动,直到序列sum变成非负数或者序列变空了; ps:如果最大子段和为负,如下操作:"If all theKnumbers are negative, then its maximum sum is defin...

床前明月光 332

1007. Maximum Subsequence Sum (25)-PAT

1007. Maximum Subsequence Sum (25) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Given a sequence of K integers { N

兵临城下 1069

【PAT甲级题解记录】1007 Maximum Subsequence Sum (25 分)

问题描述 求最大连续子序列和,输出这个连续子序列的首值、尾值和序列和(优先最靠前的),如果都为这个序列都为负的输出0和整个序列的首尾值。 解题思路 一道经典又特殊的动态规划,本身很简单,但考虑到要求最靠前的首尾值,容易出错,难度相应提高。

retr0的博客 448

PAT甲级 1007 Maximum Subsequence Sum (25 分)

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 Su...

liuxinghui_SJTU的博客 337

PAT甲级——1007 Maximum Subsequence Sum (25分)

一开始DP思路: 这个最长子序列的开头元素肯定是正数 结尾的元素也肯定是正数 但是中间可能包含若干个负数 所以从两头开始找,遇到符合条件1,2的就计算中间所包含的子序列的和并记录 可以用递归解决 最后再遍历一遍找到最大的子序列和 这种算法最坏的复杂度是10000*10000,肯定超时 所以想办法用DP void f(int r,int t){ if(r>=t){ ...

沉迷单车的追风少年 554

Day4 [PAT] 1007 Maximum Subsequence Sum (25 分)

Day4: [PAT] 1007 Maximum Subsequence Sum (25 分) 题源: 来自PAT甲级题库1007: https://pintia.cn/problem-sets/994805342720868352/problems/994805514284679168 思路: 经典题目最大子列和,我用的是“在线处理”的方法,其思想就是在遍历数列的同时查看目前子列和是否对后面...

qq2215459786的博客 118

PAT 1007 Maximum Subsequence Sum (25)

1007 Maximum Subsequence Sum (25)(25 分) Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to be { Ni, Ni+1, ..., Nj } where 1 &lt;= i &lt;= j &lt;= K. The Maximu...

ryo_218的博客 148

[PAT 甲级] 1007 Maximum Subsequence Sum (25 分)【动态规划】

1007 Maximum Subsequence Sum (25 分) 原题链接 Given a sequence of K integers { N ​​ }. A continuous subsequence is defined to be { N ​​ } where 1≤i≤j≤K. The Maximum Subsequence is the continuous subseque...

cp0328的博客 3586
上一篇: PAT 1004. Counting Leaves (30)
下一篇: PAT 1009. Product of Polynomials (25)
xuxumoi
博客等级 码龄16年 16粉丝 151原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值