HDU 5586 Sum (最大连续子段和)

There is a number sequence A 1 ,A 2 ….A n ,you can select a interval l,r or not,all the numbers A i (l≤i≤r) f(x)=(1890x+143)mod10007 After that,the sum of n numbers should be as much as possible.What is the maximum sum? InputThere are multiple test cases.
First line of each case contains a single integer n.(1≤n≤10 5 )
(1≤n≤105)

Next line contains n integers A 1 ,A 2 ….A n
A1,A2….An
.(0≤A i ≤10 4 )
(0≤Ai≤104)

It’s guaranteed that ∑n≤10 6
∑n≤106
.
OutputFor each test case,output the answer in a line.
Sample Input2
10000 9999
5
1 9999 1 9999 1
Sample Output19999
22033

题意:
一个数列ai,你可以选择任意长度的子区间(可以为0)将ai变成ai=ai*890+143)%10007,求n个数的最大和值。
题解:
求出差值bi,求出bi的最大子段和。加上原数列的和就好。
代码:

#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <iostream>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <string.h>
#define bababaa printf("!!!!!!!\n")
#define ll long long
using namespace std;
ll a[100006],b[100006],dp[100006];
int main()
{
    int n;
    while(cin>>n)
    {
        ll sum=0;
        int mx=-100000000;
        for(int i=1;i<=n;i++)
        {
            cin>>a[i];
            sum+=a[i];
            b[i]=(1890*a[i]+143)%10007-a[i];
        }
        dp[1]=b[1];
        for(int i=2;i<=n;i++)
        {
            dp[i]=max(dp[i-1]+b[i],b[i]);
            if(dp[i]>mx)
                mx=dp[i];
        }
        if(mx>0)
            cout<<sum+mx<<endl;
        else
            cout<<sum<<endl;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值