您的位置:首页 > 其它

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

2017-01-09 22:32 411 查看
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;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: