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

PAT 1007 Maximum Subsequence Sum

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

自己写的代码:一开始只写了13分,没有考虑到只有一个整数的情况,没有更新sum,max,min的值,但是现在还是没有满分,以后慢慢琢磨 (知道错哪里了)

例子:

2 

0 -1 

正确答案应该是 0 0 0  因为0是非负数,所以0在这里算是最大值

而不是这个代码打印出来的 0 0 -1

解决办法:sum一开始的赋值应该为-1,让当a[i]=0的情况也能更新 l, r 

 代码:

#include <iostream>
using namespace std;

const int N=10010;
int f[N];
int a[N];
int n;

int main(){
    cin >> n;
    int min=0,max=0,sum=0,l=0,r=0;
    int sign=0;
    
    for(int i=1;i<=n;i++){
        scanf("%d",&a[i]);
        if(a[i]>=0)  sign=1;//此处大于等于0才算是非负数
    }
    
    if(!sign)   printf("0 %d %d\n",a[1],a[n]);
    else{
        for(int i=1;i<=n;i++){
            //目前想到的当f[i-1]<=0时,就可以直接放弃前面的数了
            //或者和当前一个负数相加<=0,也可以直接放弃
            //还有情况是一个数组中只有一个负数时,-1 -2 3 -5,这时也需要改变sum,l,r的值
            if(f[i-1]<=0){
                f[i]=a[i];
                l=i;
                if(f[i]>sum){
                    sum=f[i];
                    min=l;
                    max=l;//这里要注意的是因为当前r还未更新,所以r<=i,只能手动给max赋值
                }
            }
            else{
                f[i]=f[i-1]+a[i];
                r=i;
                if(f[i]>sum){
                    sum=f[i];
                    min=l;
                    max=r;
                }
            }
        }
        printf("%d %d %d\n",sum,a[min],a[max]);
    }
    
    return 0;
}

依照惯例,还是看看大佬的代码:

思路简单明了,清晰易懂

#include <iostream>
#include <vector>
using namespace std;
int main() {
    int n;
    scanf("%d", &n);
    vector<int> v(n);
    int leftindex = 0, rightindex = n - 1, sum = -1, temp = 0, tempindex = 0;
    for (int i = 0; i < n; i++) {
        scanf("%d", &v[i]);
        temp = temp + v[i];
        if (temp < 0) {
            temp = 0;
            tempindex = i + 1;
        } else if (temp > sum) {
            sum = temp;
            leftindex = tempindex;
            rightindex = i;
        }
    }
    if (sum < 0) sum = 0;
    printf("%d %d %d", sum, v[leftindex], v[rightindex]);
    return 0;
}

只需要存前面是大于0的状态,小于0的直接舍弃,果断一点

好好学习,天天向上!

我要考研!

相关文章:

  • go中的slice
  • 什么是完全的静态分析?
  • 如何在ios手机上使用动态代理?
  • 搭建zookeeper集群
  • React生命周期详解
  • 大数据项目中数据倾斜
  • Kafka Consumer源码讲解
  • svg中 path标签的d属性
  • 什么样的数字IC后端工程师能拿到高薪Offer?
  • 计算机组成原理_DRAM和SRAM
  • 两个有序序列的中位数
  • 浅析Android UI——View 的绘制
  • 一文看懂25个神经网络模型,神经网络神经元模型
  • 神经网络模型训练过程,神经网络模型应用实例
  • NR PDCCH(二) SearchSpace
  • 【跃迁之路】【733天】程序员高效学习方法论探索系列(实验阶段490-2019.2.23)...
  • ComponentOne 2017 V2版本正式发布
  • GitUp, 你不可错过的秀外慧中的git工具
  • Java,console输出实时的转向GUI textbox
  • JavaScript创建对象的四种方式
  • Java基本数据类型之Number
  • java中的hashCode
  • Lsb图片隐写
  • Perseus-BERT——业内性能极致优化的BERT训练方案
  • php中curl和soap方式请求服务超时问题
  • Shell编程
  • 从setTimeout-setInterval看JS线程
  • 大快搜索数据爬虫技术实例安装教学篇
  • 给自己的博客网站加上酷炫的初音未来音乐游戏?
  • 将 Measurements 和 Units 应用到物理学
  • 强力优化Rancher k8s中国区的使用体验
  • 如何使用 JavaScript 解析 URL
  • 我的zsh配置, 2019最新方案
  • 详解NodeJs流之一
  • 小程序01:wepy框架整合iview webapp UI
  • raise 与 raise ... from 的区别
  • 阿里云服务器购买完整流程
  • 进程与线程(三)——进程/线程间通信
  • #我与Java虚拟机的故事#连载16:打开Java世界大门的钥匙
  • (NO.00004)iOS实现打砖块游戏(十二):伸缩自如,我是如意金箍棒(上)!
  • (zt)基于Facebook和Flash平台的应用架构解析
  • (笔试题)分解质因式
  • (附源码)spring boot基于Java的电影院售票与管理系统毕业设计 011449
  • (七)理解angular中的module和injector,即依赖注入
  • (三十五)大数据实战——Superset可视化平台搭建
  • (一)基于IDEA的JAVA基础10
  • (转)EXC_BREAKPOINT僵尸错误
  • (转)fock函数详解
  • (转)树状数组
  • .NET C# 使用 SetWindowsHookEx 监听鼠标或键盘消息以及此方法的坑
  • .NET Core实战项目之CMS 第十二章 开发篇-Dapper封装CURD及仓储代码生成器实现
  • .net 发送邮件
  • .NET 中 GetHashCode 的哈希值有多大概率会相同(哈希碰撞)
  • .Net接口调试与案例
  • @converter 只能用mysql吗_python-MySQLConverter对象没有mysql-connector属性’...