LBLT

  • 主页
  • 分类
  • 归档
所有文章 友链 关于我

LBLT

  • 主页
  • 分类
  • 归档
🍻 正在加载今日诗词.... 🔍

1007 Maximum Subsequence Sum (25分)


阅读数: 11次    2020-07-27

1. 题目

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

2. 样例输入

10 -10 1 2 3 4 -5 -23 3 7 -21

3. 样例输出

10 1 4

4. 思路

这一题是在慕课上讲过的
所以思路非常简单,寻找最长连续子列和就从做往右一直加,如果小于零了就舍弃进一步,然后就可以辣
还有就是注意审题,题目说如果找不到就输出第一和最后一个数字,之前的代码就是卡在这里

5. AC代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <vector>
using namespace std;

int main() {
int n; cin >> n;
vector<int>v(n);
int Thissum = 0, result = -1, head = 0, tail = n-1, temphead=0, temp=0;
for (int i = 0; i < n; i++) {
scanf("%d",&v[i]);
temp+=v[i];
if(temp<0){
temp=0;
temphead=i+1;
}else if(temp>result){
result=temp;
head=temphead;
tail=i;
}
}
if(result<0)result=0;
printf("%d %d %d",result,v[head],v[tail]);
system("pause");
return 0;
}
  • 刷题
  • pat
  • 学习
  • 刷题
  • pat

扫一扫,分享到微信

微信分享二维码
准备把自己的博客再装修装修
1006 Sign In and Sign Out (25分)
© 2021 LBLT
© 2021 LBLT
本站总访问量:5140次 | 本站访客数:4431人
  • 所有文章
  • 友链
  • 关于我

tag:

  • 刷题
  • pat
  • 牛客
  • 牛客网
  • vj
  • 项目
  • 紫书
  • 比赛

    缺失模块。
    1、请确保node版本大于6.2
    2、在博客根目录(注意不是yilia根目录)执行以下命令:
    npm i hexo-generator-json-content --save

    3、在根目录_config.yml里添加配置:

      jsonContent:
        meta: false
        pages: false
        posts:
          title: true
          date: true
          path: true
          text: false
          raw: false
          content: false
          slug: false
          updated: false
          comments: false
          link: false
          permalink: false
          excerpt: false
          categories: false
          tags: true
    

  • 景点地图

    2021-11-12

    #比赛

  • 补一下之前写的python合成大罗马

    2021-11-02

    #项目

  • python一个小游戏(抄的)

    2021-03-23

    #项目

  • 每日刷题(8)

    2021-03-20

    #刷题#牛客

  • 牛客网每日刷题(3)

    2021-01-30

    #刷题#紫书

  • 每日刷题(8)

    2021-01-26

    #刷题#vj

  • 牛客网每日刷题(7)

    2021-01-20

    #刷题#vj

  • 牛客网每日刷题(5)

    2021-01-19

    #刷题#牛客网

  • 牛客网每日刷题(5)

    2021-01-19

    #刷题#牛客网

  • 牛客网每日刷题(5)

    2021-01-08

    #刷题#牛客网

  • 牛客网每日刷题(4)

    2021-01-07

    #刷题#牛客网

  • 牛客网每日刷题(3)

    2021-01-07

    #刷题#牛客网

  • 牛客网每日刷题(2)

    2021-01-06

    #刷题#牛客网

  • 牛客网每日刷题(2)

    2021-01-06

    #刷题#牛客网

  • PAT-A-1068

    2020-12-13

    #刷题#牛客

  • 准备把自己的博客再装修装修

    2020-07-27

  • 1007 Maximum Subsequence Sum (25分)

    2020-07-27

    #刷题#pat

  • 1006 Sign In and Sign Out (25分)

    2020-07-27

    #刷题#pat

  • 1005 Spell It Right (20分)

    2020-07-27

    #刷题#pat

  • 1004 Counting Leaves (30分)

    2020-07-27

    #刷题#pat

  • 1003 Emergency (25分)

    2020-07-25

    #刷题#pat

  • 1002 A+B for Polynomials (25分)

    2020-07-25

    #刷题#pat

  • 1001 A+B Format (20分)

    2020-07-25

    #刷题#pat

  • 许久不更新

    2020-05-28

  • PAT-A-1068

    2020-04-26

    #刷题#pat

  • PAT-A-1064

    2020-04-25

    #刷题#pat

  • PAT-A-1060

    2020-04-20

    #刷题#pat

  • PAT A 1057

    2020-04-18

    #刷题#pat

  • 你好,这是LBLT的个人小站

    2020-04-17

  • Hello World

    2020-04-17

  • 友情链接1
计算机菜狗

什么都会一点
什么都不擅长