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的最大子段和。加上原数列的和就好。
代码:
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; } }
相关文章推荐
- HDU 5586 Sum (最大连续子段和)
- HDU 1231 最大连续子序列(最大连续子段和)
- hdu1003最大连续子段和
- HDU-1003 Max Sum(最大连续子段和)
- hdu(1003)——Max Sum(最大连续子段和)
- HDU_5586_Sum(最大连续子段和)
- 【m段最大连续子段和的和】HDU - 1024 Max Sum Plus Plus
- HDU 1003 最大连续子段和
- HDU 1003 MAX SUM 最大连续子段和DP
- HDU 1422重温世界杯 (环形)最长连续子段 由最大连续子段和变形的到
- hdu 1422 重温世界杯(最大连续子段和变形)
- HDU - 1024 Max Sum Plus Plus(最大M段连续子段和,详细解释)
- HDU 2845 最大非连续子段和 二维dp
- HDU 1003 最大连续子段和
- hdu(5586)——Sum(最大连续子段和)
- HDU 1003 Max Sum(最大连续子段和)
- DP训练 hdu 1003 Max Sum 最大连续子段和
- [HDU] 1087 Super Jumping! Jumping! Jumping! -基本模型中的 最大递增非连续字段和
- hdu 1024 Max Sum Plus Plus (最大m子段和)(经典DP)(转)
- hdu Max Sum Plus Plus(最大m段子段和)