hdu 5586(最大连续子序列和)

本文提供了一道来自HDU在线评测系统的编程题的解答思路及实现代码。通过转换问题为求最大连续子序列和,巧妙地解决了问题。代码使用C++编写,实现了快速求解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5586

官方题解:

A i =f(A i )A i   ,然后求一遍最大连续子序列和就能知道最多能增加的值。  

PS:感觉这题好巧妙啊。。很难想到去利用最大连续子序列和求增量。。

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;

typedef long long LL;
const int maxn = 1e5+5;
const int mod = 1e4+7;
int arr[maxn], data[maxn];

int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        LL sum = 0;
        for(int i=0; i<n; i++)
        {
            scanf("%d",&arr[i]);
            sum += arr[i];
            data[i] = ((arr[i]*1890+143)%mod) - arr[i];
        }
        LL Max = 0, tmp = 0;
        for(int i = 0; i < n; i++)
        {
            tmp += data[i];
            if(tmp > Max)
                Max = tmp;
            if(tmp < 0)
                tmp = 0;
        }
        LL ret = sum + Max;
        printf("%I64d\n",ret);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值