zoukankan      html  css  js  c++  java
  • pat 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. 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 (≤). 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

    Topical:
    Typical and simple greedy algorithm. First, find the first positive number in the array, and assign this element to max, if traverse to the end of Array, print as the
    topic has said. For each step of traversing, you need sum the elements from s, the possiable maximum sequence's start position, to the position of this step, if the sum
    bigger than max, then renew the max, or if sum has alreay less than 0, assign 0 to sum and renew the possiable maximum sequence's start position to next step.

    Code:
    #include <iostream>
    using namespace std;
    int main()
    {
        int k;
        int st, ed, s, e;
        int a[10000];
        int i;
        cin >> k;
        for (i = 0; i < k; i++)
        {
            cin >> a[i];
        }
        int max, count;
        i = 0;
        while (i < k && a[i] < 0)
            i++;
        if (i == k)
        {
            cout << 0 << " " << a[0] << " " << a[k - 1];
            return 0;
        }
        count = max = a[i];
        s = st = ed = i;
        for (i = i + 1; i < k; i++)
        {
            count += a[i];
            e = i;
            if (count > max)
            {
                max = count;
                st = s;
                ed = e;
            }
            else if (count < 0)
            {
                count = 0;
                s = e = i + 1;
            }
        }
        cout << max << ' ' << a[st] << ' ' << a[ed];
        return 0;
    }

    Impression:

      First, the reason i use English to write this blog is that my English postgraduate was defeated in the composition, so this is one part of my plan to solve this problem. As you can see, maybe now the use of vocabulary and grammer is not satisfactory, but i trust one day i will be good at writing. Second, the 1007 is my first step of greedy algorithm learning.




  • 相关阅读:
    Spring 进行junit单元测试时,出现method ‘initializationError’ 错误
    反射注解
    Maven
    spring 和 spingmvc 和 mybatis 的集成应用
    SpringMVC:文件上传,文件下载,SpringMVC的拦截器,poi组件导出excel文件
    SpringMVC:SpringMVC执行流程和原理,RESTful风格支持,请求中文乱码问题, 响应传值方式, 转换JSON数据
    Spring和MyBatis的整合
    Spring的事务管理
    Spring:AOP, 面向切面编程,JDK的动态代理,CGLIB代理,Spring的AOP技术(底层就是JDK动态代理和CGLIB代理技术)
    Spring注解配置:@Component,@Controller,@Service,@Repository,@Scope,@Autowired,@Qualifier,@Resource@Value
  • 原文地址:https://www.cnblogs.com/stormax/p/10939855.html
Copyright © 2011-2022 走看看