PAT(甲级)1007. Maximum Subsequence Sum (25)

Maximum Subsequence Sum (25)

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

题目大意:求最大连续子序列和 输出最大子序列和 以及子序列的首尾的值(不是序号。。。表问我为什么强调 宝宝委屈)然后还要序号最小 另外当所有元素都为负数时 默认最大和为0 同时输出第一位和最后一位的值
分析:最大连续子序列和 子序列一定是在某一位结束,所以记录以其前一位结尾的最大和 再更新以当前位结束的最大和 记录最大值与首尾值

/*
10
-10 2 7 -8 4 6 -23 3 7 -21
5
-2 -3 3 4 5
*/
#include <iostream>
#include<cstdio>
#include<cstring>
#define maxn 10010
using namespace std;

int main()
{
    int k;
    int num[maxn];
    int maxs,nsum;
    int s,e,t;
    scanf("%d",&k);
    for(int i=0;i<k;++i)
        scanf("%d",&num[i]);
    maxs=num[0],nsum=num[0];
    s=num[0],e=s,t=s;
    for(int i=1;i<k;++i){
        if(nsum>=0)//要求首尾序号最小
            nsum+=num[i];
        else{
            nsum=num[i];
            t=num[i];//记录开始位置的值
        }
        if(nsum>maxs){
            maxs=nsum;
            e=num[i];//记录最大子序列和的结束位置的值
            s=t;//开始位置的值
        }
    }
    if(maxs<0)//全为负数的情况
        printf("0 %d %d",num[0],num[k-1]);
    else
        printf("%d %d %d",maxs,s,e);
    return 0;
}

内容概要:本文介绍了如何使用Python识别图片扫描PDF中的文字。首先,文章讲解了使用Spire.OCR for Python库来识别图片中的文字,包括安装库、配置OCR模型路径语言设置、扫描图片以及保存识别后的文本。其次,详细描述了从图片中提取文字及其坐标位置的方法,使用户不仅能够获取文本内容,还能知道文本在图片中的具体位置。最后,文章还介绍了如何结合Spire.PDF for Python将PDF文件转换为图片格式,再通过OCR技术从中提取文字,适用于处理扫描版PDF文件。文中提供了完整的代码示例,帮助读者理解实践。 适合人群:对Python编程有一定基础,希望学习或提高光学字符识别(OCR)技术的应用开发者,尤其是需要处理大量图片或PDF文档中文字信息的工作人员。 使用场景及目标:① 开发者可以利用这些方法自动化处理图片或PDF文档中的文字信息,提高工作效率;② 实现从非结构化数据(如图片、扫描件)到结构化数据(如文本文件)的转换,便于后续的数据分析处理;③ 提供了一种解决纸质文档数字化的有效途径,特别是对于历史档案、书籍等资料的电子化保存。 其他说明:需要注意的是,OCR的准确性很大程度上取决于图片的质量,清晰度高、对比度好的图片可以获得更好的识别效果。此外,不同OCR库可能对特定语言或字体的支持程度不同,选择合适的库配置参数能显著提升识别精度。在实际应用中,建议先进行小规模测试,优化参数后再大规模应用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值