hdu-5586 Sum(dp)

时间:2021-02-04 07:59:08

题目链接:

Sum

Time Limit: 2000/1000 MS (Java/Others)  

  Memory Limit: 65536/65536 K (Java/Others)

Problem Description
 
There is a number sequence A1,A2....An,you can select a interval [l,r] or not,all the numbers Ai(l≤i≤r) will become f(Ai).f(x)=(1890x+143)mod10007.After that,the sum of n numbers should be as much as possible.What is the maximum sum?
 
Input
 
There are multiple test cases.
First line of each case contains a single integer n.(1≤n≤10^5)
Next line contains n integers A1,A2....An.(0≤Ai≤10^4)
It's guaranteed that ∑n≤10^6.
 
Output
 
For each test case,output the answer in a line.
 
Sample Input
2
10000 9999
5
1 9999 1 9999 1
 
Sample Output
19999
22033
 
 
题意:
 
给一个数组,选一个区间[l,r]把a[i]变成f(a[i]),也可以不选,问最后得到的这个数组的和最大是多少;
 
思路:
 
p[i]=(1890*a[i]+143)%10007;
然后就是在p[i]中找一个连续喝最大的字串,然后就好了;水题;
 
AC代码:
//#include <bits/stdc++.h>
#include <vector>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <cstring>
#include <algorithm>
#include <cstdio> using namespace std;
#define Riep(n) for(int i=1;i<=n;i++)
#define Riop(n) for(int i=0;i<n;i++)
#define Rjep(n) for(int j=1;j<=n;j++)
#define Rjop(n) for(int j=0;j<n;j++)
#define mst(ss,b) memset(ss,b,sizeof(ss));
typedef long long LL;
template<class T> void read(T&num) {
char CH; bool F=false;
for(CH=getchar();CH<''||CH>'';F= CH=='-',CH=getchar());
for(num=;CH>=''&&CH<='';num=num*+CH-'',CH=getchar());
F && (num=-num);
}
int stk[], tp;
template<class T> inline void print(T p) {
if(!p) { puts(""); return; }
while(p) stk[++ tp] = p%, p/=;
while(tp) putchar(stk[tp--] + '');
putchar('\n');
} const LL mod=1e9+;
const double PI=acos(-1.0);
const LL inf=1e14;
const int N=1e4+;
const int maxn=; int p[*N],a[*N];
LL dp[*N];
int main()
{
int n;
while(cin>>n)
{
LL sum=,ans=;
Riep(n)read(a[i]),p[i]=(*a[i]+)%-a[i],sum=sum+a[i];
dp[]=;
for(int i=;i<=n;i++)
{
if(dp[i-]+p[i]<)dp[i]=;
else dp[i]=dp[i-]+p[i];
ans=max(ans,dp[i]);
}
cout<<sum+ans<<"\n";
} return ;
}