当前位置: 首页 > news >正文

1007 Maximum Subsequence Sum (PAT甲级)

惭愧,知道该用DP做,但是又翻了参考书才想起来。dp[i]表示以i项为结尾的最大子列。

#include <cstdio>
#include <vector>int K, maxx, u, pu, pv;
std::vector<int> vec, dp;int main(){scanf("%d", &K);vec.resize(K);dp.resize(K);maxx = -1;for(int i = 0; i < K; ++i){scanf("%d", &vec[i]);if(i == 0){dp[0] = vec[0];u = 0;} else{if(dp[i - 1] < 0){dp[i] = vec[i];u = i;} else{dp[i] = vec[i] + dp[i - 1];}}if(dp[i] > maxx){maxx = dp[i];pu = u;pv = i;}}if(maxx == -1){printf("0 %d %d", vec[0], vec[K - 1]);} else{printf("%d %d %d", maxx, vec[pu], vec[pv]);}return 0;
}

题目如下:

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
http://www.lryc.cn/news/114457.html

相关文章:

  • 虚拟机centos7配置网络
  • ChatGPT实战:创业咨询,少走弯路,少踩坑
  • LangChain手记 Overview
  • Vue_02:详细语法以及代码示例 + 知识点练习 + 综合案例(第二期)
  • [腾讯云 Cloud studio 实战训练营] 制作Scrapy Demo爬取起点网月票榜小说数据
  • 使用paddle进行酒店评论的情感分类5——batch准备
  • 04-1_Qt 5.9 C++开发指南_常用界面设计组件_字符串QString
  • Centos 从0搭建grafana和Prometheus 服务以及问题解决
  • 【代码解读】RRNet: A Hybrid Detector for Object Detection in Drone-captured Images
  • python人工智能可以干什么,python人工智能能干什么
  • K8s工作原理
  • go错误集(持续更新)
  • 【Docker】Docker中network的概要、常用命令、网络模式以及底层ip和容器映射变化的详细讲解
  • arcgis栅格数据之最佳路径分析
  • docker服务器部署Django
  • SpringBoot集成百度人脸识别实现登陆注册功能Demo(二)
  • FPGA纯verilog实现 LZMA 数据压缩,提供工程源码和技术支持
  • C++实现一个链栈
  • Vue电商项目--VUE插件的使用及原理
  • 2.部署kubernetes的组件
  • 后端开发4.Elasticsearch的搭建
  • 嵌入式该往哪个方向发展?
  • 非凸科技受邀参加中科大线上量化分享
  • Linux 命令之 - chown(改变文件拥有者及所属组)
  • 【基于openharmony的多路摄像头功能:USB设备插拔检测】
  • uni-app:实现数字文本框,以及左右加减按钮
  • 跨平台开发框架Qt:面向对象、丰富API
  • An unexpected error has occurred. Conda has prepared the above report
  • 考研C语言进阶题库——更新6-10题
  • 汽车BOOTLOADER开发经历