PAT甲级1007 Maximum Subsequence Sum

news/2024/10/21 18:38:32/

 题目地址:

编程题 - PAT (Advanced Level) Practice (pintia.cn)

介绍

前缀和

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.

代码

#include<bits/stdc++.h>
using namespace std;
#define ll long long  
#define ull unsigned long longvoid solve() {int n;cin>>n;ll num_add[10001];ll max1=INT_MIN;int begin,end;ll sum=0;int i,j;int key=1;for(i=0;i<=n-1;i++){cin>>j;if(j>=0){key=0;}sum+=j;num_add[i]=sum;}max1=num_add[0];begin=0;end=0;if(key){cout<<0<<" "<<num_add[0]<<" "<<num_add[n-1]-num_add[n-2];;return ;}for(i=0;i<=n-1;i++){for(j=i+1;j<=n-1;j++){if(num_add[j]-num_add[i]>max1){max1=num_add[j]-num_add[i];begin=i+1;end=j;}}}if(max1==num_add[0]){cout<<max1<<" "<<num_add[begin]<<" "<<num_add[end];}elsecout<<max1<<" "<<num_add[begin]-num_add[begin-1]<<" "<<num_add[end]-num_add[end-1];
} signed main() {ll t = 1; // std::cin >> t;while (t--) {solve();}
}


http://www.ppmy.cn/news/1538443.html

相关文章

Linux基础与C语言总结

1.常用的Linux命令 常用:cd ls clear man sudo size time strace 文件:touch cat/more/head/tail(查看文件) cp mv rm umask chmod 目录:mkdir rmdir rm -rf cp -frp 网络:ping ifconfig ssh/telent/ftp 其他:ps -aux find grep reboot tar 2.vim文本编辑器的常用操作? …

涂鸦革新WebRTC技术!让IPC监测低延时、高可靠更安全

随着科技的飞速发展&#xff0c;越来越多人开始关注居家安全、食品安全、校园安全等领域&#xff0c;大家对实时监测的需求也在不断升级。想象一下&#xff0c;无论身处何地&#xff0c;只需轻触屏幕&#xff0c;就能实时查看家中、办公室或任何你关心的地方&#xff0c;这不再…

CSS 命名规范及 BEM 在前端开发中的实践

一&#xff1a;CSS命名规范的重要性 1、提高代码可读性 对于开发者自身来说&#xff0c;遵循规范的命名可以让你在日后回顾代码时&#xff0c;快速理解每个样式类的用途。例如&#xff0c;使用 “.header-logo” 这样的命名&#xff0c;一眼就能看出是头部的 logo 元素的样式…

LangChain中使用Prompt01

1.引入提示模板 from langchain.prompts import (SystemMessagePromptTemplate,AIMessagePromptTemplate,HumanMessagePromptTemplate, )2.设置系统提示 system_template_text"你是一位专业的翻译&#xff0c;能够将{input_language}翻译成{output_language}&#xff0c…

开发一个UniApp需要多长时间

开发一个UniApp所需的时间因项目的规模、复杂度、开发团队的经验水平以及开发过程中的需求变更等多种因素而异。因此&#xff0c;很难给出一个确切的时间范围。然而&#xff0c;我们可以从以下几个方面来大致估算开发时间&#xff1a; 项目规划与需求分析&#xff1a; 在项目开…

metahuman如何导入UE5

1.启动 通过EPIC启动UE5(UE5内置有Bridge, 但是UE4是需要单独下在Bridge软件) 2.打开Quixel Bridge 在window(窗口)中打开Quixel Bridge 3.Bridge界面 在弹出的Bridge界面选择模型 需要先下载&#xff0c;然后再导入 4.下载模型 点击需要的模型右上方的绿色箭头下载 5.下…

Java利用ChromeDriver插件网页截图(Wondows版+Linux版)

chromedriver是谷歌浏览器驱动,用来模拟谷歌运行操作的一个工具&#xff0c;此处主要讲解Java后端利用此插件进行网页截图&#xff0c;并且适配Linux部署。 环境准备 Wondows服务器或电脑 本机需安装Chrome谷歌浏览器&#xff0c;根据本机浏览器版本&#xff0c;下载对应的chr…

【FastAdmin】全栈视角下的页面跳转实现:从原生html、javascrpt、php技术到jQuery、FastAdmin框架

全栈视角下的页面跳转实现&#xff1a;从原生html、javascrpt、php技术到jQuery、FastAdmin框架 1 引言 页面跳转是Web开发中的基本操作&#xff0c;不同的技术栈提供了不同的实现方法。本文将详细介绍在原生JavaScript、原生HTML、原生PHP、jQuery以及FastAdmin框架中实现页…