pat PAT (Advanced Level) Practise 1007. Maximum Subsequence Sum (25)

PAT (Advanced Level) L1-6. 连续因子 for循环暴力 L1-6. 连续因子 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 一个正整数N的因子中可能存在若干连续的数字。例如630可以分解为3*5*6*7,其中5、6、7就是3个连续的数字。给定任一正整数N, 阅读详情

1007. Maximum Subsequence Sum (25)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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


在线处理,用一个ts保存临时的最大子列和区间的左端点,s为确定的最终左端点,e为右端点


#include <cstdio>
#include <cstring>
#include <iostream>

using namespace std;

const int maxn = 10000, INF = 0x7fffffff;
int a[maxn + 10];

int main()
{
	int K;
	cin >> K;
	bool check = false;
	for (int i = 0; i < K; i++) {
		scanf("%d", &a[i]);
		if (a[i] >= 0) check = true;
	}
	if (!check) {
		printf("0 %d %d\n", a[0], a[K - 1]);
		return 0;
	}
	int s = a[0], e = a[0], ts = a[0];
	int tsum = 0, maxx = -INF;
	bool flag = false;
	for (int i = 0; i < K; i++) {
		//flag == true时说明上一次迭代的时候tsum < 0了,要重新计算最大子列和s
		if (flag) {
			ts = a[i]; //计算新的子列和,取新的左端点
			flag = false; //注意解除标记
		}
		tsum += a[i];
		if (maxx < tsum) {
			s = ts; //更新最终的左端点s
			e = a[i]; //更新右端点e
			maxx = tsum;
		}
		if (tsum < 0) { //debug 不要放到if(maxx < tsum)上面
			flag = true;
			tsum = 0;
		}
	}
	printf("%d %d %d\n", maxx, s, e);
	return 0;
}



团体程序设计天梯赛-练习集 L2-2. 链表去重 数组模拟链表 L2-2. 链表去重 时间限制 300 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 给定一个带整数键值的单链表L,本题要求你编写程序,删除那些键值的绝对值有重复的结点。即对任意键值K,只有键值或其绝对值等于K的第一个 阅读详情

相关推荐

Python量化入门:用pytdx获取800个交易日K线数据的完整指南(附代码)

本文为Python量化交易初学者提供了使用pytdx库免费获取股票日线级别K线数据的完整指南。文章详细介绍了从环境搭建、单只股票数据获取,到构建健壮的批量下载器、数据清洗与验证,再到基础分析与可视化的全流程,并附有可直接运行的代码示例,帮助读者快速建立本地股票数据库。

weixin_29050513的博客 1505

浙大 PAT Advanced level 1007. Maximum Subsequence Sum

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

fanxingzju的专栏 439

【OpenClaw 故障排查完全指南】

OpenClaw 故障排查指南摘要 本指南针对OpenClaw 2026.2.26版本常见问题提供解决方案,适用于Windows/Linux/macOS平台。 核心问题及修复方案: 飞书插件连接失败:删除冲突插件或重装OpenClaw Ollama认证错误:设置环境变量OLLAMA_API_KEY="ollama-local" 配置警告:可安全忽略的安全扫描提示 端口占用:查找并终止占用18789端口的进程 插件重复加载:清理重复插件目录 快速修复: 提供一键修复PowerShell脚本

weixin_37572294的博客 1万+

PAT1007

1007. 素数对猜想 (20)让我们定义 dn 为:dn = pn+1 - pn,其中 pi 是第i个素数。显然有 d1=1 且对于n&gt;1有 dn 是偶数。“素数对猜想”认为“存在无穷多对相邻且差为2的素数”。现给定任意正整数N (&lt; 105),请计算不超过N的满足猜想的素数对的个数。输入格式:每个测试输入包含1个测试用例,给出正整数N。输出格式:每个测试用例的输出占一行,不超过N的...

TNTZS666的博客 669

窥秘Windows 10:SAM密码破解

Windows 10作为广受欢迎的操作系统,拥有一套强大的安全措施来保护用户账号和数据安全。然而,有时候我们需要了解更多关于密码破解的知识,以加强我们的防护措施。本文将深入探讨如何突破Windows 10的SAM密码,以便更好地了解密码安全性并加强系统的保护。

Sgirll的博客 6888

基于MATLAB的PDT DMR物理层仿真

PDT DMR物理层仿真的目标是评估系统的误码率(Bit Error Rate,BER),以及验证和优化各种调制方案、编码方案和信道模型。下面将介绍如何进行PDT DMR物理层的仿真,并提供相应的MATLAB源代码。通过上述步骤,我们可以进行基于MATLAB的PDT DMR物理层仿真,并评估系统的性能。根据实际需求,我们可以修改仿真参数和添加更复杂的信道模型,以更全面地评估系统性能。最后,我们可以将仿真结果进行可视化和分析。首先,我们需要设置仿真的参数,包括调制方案、编码方案、信道模型和信噪比等。

TechPulseZ的博客 468

PAT (Advanced Level) Practise - 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 {

Mr_Treeeee的博客 314

第二篇:APS智能排产实战:OR-Tools与coptpy从入门到案例

专注于具体的技术实现,包括 OR-Tools 和 coptpy 的详细使用指南、建模技巧、日志解读,以及一个完整的两阶段生产排程案例。适合准备动手开发 APS 系统的工程师和研究者。

算法工程师 488

1007. Maximum Subsequence Sum (25)——PAT (Advanced Level) Practise

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

969

PGM-Explainer实验总结

PGM-Explainer建立了一个概率图模型,为GNN提供实例级解释。 论文标题:PGM-Explainer: Probabilistic Graphical Model Explanations for Graph Neural Networks 论文地址:2010.05788.pdf (arxiv.org) PGM-Explainer实验 实验目的 研究PGM-Explainer的有效性 实验方案 合成数据集的节点分类 与其他解释器做对比实验 使用六个合成数据集。 每个输入图都是一个

笔记|思路|总结 1万+

【错误异常大全】:无法加载 DLL“ArcGISVersion.dll”: 找不到指定的模块。 (异常来自 HRESULT:0x8007007E)。

文章目录错误异常解决方案 错误异常 无法加载 DLL“ArcGISVersion.dll”: 找不到指定的模块。 (异常来自 HRESULT:0x8007007E)。 解决方案 将目标平台改为x86,具体步骤如下: 点击【配置管理器】。 下拉活动解决方案平台,点击【新建】。 选择x86,点击【确定】。 点击【关闭】。 大功告成! ...

智慧空间实验室 4659

PAT (Advanced Level) Practise 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

Occult 559

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

PAT (Advanced Level) Practise 1007 Maximum Subsequence Sum

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

wang_128的博客 436

IMSL CNL 7.0.0 (x86-64)Crack

IMSL CNL 7.0.0 (x86-64)Crack intel的算法库破解文件,这是C++版本的破解文件

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

团体程序设计天梯赛-练习集 L2-018. 多项式A除以B 模拟

L2-018. 多项式A除以B 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 这仍然是一道关于A/B的题,只不过A和B都换成了多项式。你需要计算两个多项式相除的商Q和余R,其中R的阶数必须小于B的阶数

EventQueue的博客 2514

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

团体程序设计天梯赛-练习集 L2-006 树的遍历 根据后序中序建树+BFS

L2-006. 树的遍历 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 给定一棵二叉树的后序遍历和中序遍历,请你输出其层序遍历的序列。这里假设键值都是互不相等的正整数。 输入格式: 输入第

EventQueue的博客 2221

团体程序设计天梯赛-练习集 L2-3. 月饼 贪心

L2-3. 月饼 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 月饼是中国人在中秋佳节时吃的一种传统食品,不同地区有许多不同风味的月饼。现给定所有种类月饼的库存量、总售价、以及市场的最大需求量,请你计

EventQueue的博客 1761

团体程序设计天梯赛-练习集 L2-005 集合相似度 STL set集合的交和并

L2-005. 集合相似度 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 给定两个整数集合,它们的相似度定义为:Nc/Nt*100%。其中Nc是两个集合都有的不相等整数的个数,Nt是两个集合一共有的不

EventQueue的博客 1427

团体程序设计天梯赛-练习集 L2-4. 这是二叉搜索树吗? 区间递归

L2-4. 这是二叉搜索树吗? 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 一棵二叉搜索树可被递归地定义为具有下列性质的二叉树:对于任一结点, 其左子树中所有结点的键值小于该结点的键值;

EventQueue的博客 1184

团体程序设计天梯赛-练习集 L2-1. 紧急救援 Dijstra单源最短路径拓展应用

L2-1. 紧急救援 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 作为一个城市的应急救援队伍的负责人,你有一张特殊的全国地图。在地图上显示有多个分散的城市和一些连接城市的快速道路。每个城市的救援队数

EventQueue的博客 1155

PAT (Advanced Level) Practise 1114 Family Property (25) 并查集orDFS

1114. Family Property (25) 时间限制 150 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue This time, you are supposed to help us col

EventQueue的博客 972

团体程序设计天梯赛-练习集 L2-014. 列车调度 构造非递减数列二分查找

L2-014. 列车调度 时间限制 300 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 火车站的列车调度铁轨的结构如下图所示。 Figure 两端分别是一条入口(Entrance)轨道和一条出口(Exit

EventQueue的博客 941

PAT (Advanced Level) Practise 1109. Group Photo (25) 结构体排序

1109. Group Photo (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Formation is very important when taking a grou

EventQueue的博客 934

pat甲级 1020. Tree Traversals map递归建树 + BFS

先用map建立一棵二叉树,mr存右孩子节点,ml存左孩子节点,然后输出BFS路径就行了 #include #include #include #include #include using namespace std; int N; int po[35], in[35], prin[35]; int root, ip, len; map ml, mr; int solve

EventQueue的博客 903

团体程序设计天梯赛 L3-007. 天梯地图 Dijkstra

L3-007. 天梯地图 时间限制 300 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 本题要求你实现一个天梯赛专属在线地图,队员输入自己学校所在地和赛场地点后,该地图应该推荐两条路线:一条是最快到达路线;一条是最短

EventQueue的博客 887
上一篇: PAT (Advanced Level) Practise 1003. Emergency (25) Dijstra扩展应用
下一篇: PAT (Advanced Level) Practise 1013. Battle Over Cities (25)
Daemoonn
博客等级 码龄11年 42粉丝 198原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值
⌂ 换网址