文章标题

原创 2015年11月18日 00:24:58

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.

Sample Input:

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

Sample Output:

10 1 4

#include <stdio.h>
int main(){
    int a[10000],i=0,top=0,sum=0,h,r,t,z=1,l,x=1;
    int k,k1;
    scanf("%d",&k);
    k1=k;
    while(k1){
        scanf("%d",&a[i]);
        i++;
        k1--;
     }
    h=r=t=0;
    l=a[0];
    for(i=0;i<k;i++){
    if(a[i]>0) {
        z=0;
    }
//  else if(i>0){
//      if(a[i]>l){
//          l=a[i];
//      }
//  }
    if(a[i]==0) x=0;
        sum+=a[i];
        if(sum<0){
            sum=0;
            t=i+1;      
        }

        if(sum>top) {
            top=sum; 
            r=i;
            if(t!=h){
                h=t;
            }
        }
    } 
    if(z){
        switch(x){
            case 1:
                top=0;
                h=0;
                r=k-1;
                break;
            case 0:
                top=0;
                a[h]=0;
                a[r]=0;
        }
    }
    printf("%d %d %d",top,a[h],a[r]); 
    return 0;
} 

一开始没考虑到如果全为负数怎么处理
于是加入z=1用于判断是否有大于零的数

    if(a[i]>0) {
        z=0;
    }else if(i>0){
        if(a[i]>l){
            l=a[i];
        }

结果读题不仔细,全为0时的输出要求完全不一样,l的数据记录也没有意义。
x是再后来加的,用于0和负数混合输入的情况

版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

文章标题

AsyncTask连续启动多次时出现的bug

文章标题自动标注程序

  • 2015-06-25 17:04
  • 6.93MB
  • 下载

文章标题

测试编辑器的效果这里写链接内容

文章标题

题目如下:建议使用QMutex QThread 3、生产者消费者:有一个生产者对象,还有一个消费者对象生产者用来向篮子对象中放苹果,每次1个。消费者从篮子中取走吃掉1个苹果,生产苹果时不能吃...

文章标题

android 的进程基本知识当应用程序第一次启动的时候,Android会启动一个Linux 进程和一个主线程,默认情况下,所有组件都会在该进程和线程中运行,Android会为每一个应用程序分配一个单...

Cmake编译安装MySQL完整版

cmake编译安装MySQL

文章标题

## 类的常识 ## struct和class唯一的不同为在struct中未被标明为public,protected,private的默认为public,而class中默认为private,其余完全相...

文章标题

// // ViewController.m // UI-NO-26-多线程-GCD // // Created by 杨政涛 on 15/8/28. // Copyright (c...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)