给你n个数字的数组 然后还有一个 x,你可以选择一段区间乘上 x,输出最大子段和。用一个二维dp来做就行了
AC代码:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<stdlib.h>
#include<queue>
#include<map>
#include<set>
#include<iomanip>
#include<math.h>
using namespace std;
typedef long long ll;
typedef double ld;
const int INF = 0x3f3f3f3f;
const int N = 300005;
int i,j,k;
int cnt,temp,pos;
int n,m;
ll x;
ll dp[N][3],a[N];
int main()
{
scanf("%d%lld",&n,&x);
for(i=1; i<=n; ++i)
scanf("%lld",&a[i]);
ll ans=0;
for(i=1; i<=n; ++i)
{
dp[i][0]=max(dp[i-1][0]+1ll*a[i],a[i]);//最大子段和
dp[i][1]=max(max(dp[i-1][1],dp[i-1][0])+1ll*a[i]*x,1ll*x*a[i]);//当前数乘x
dp[i][2]=max(max(dp[i-1][2],dp[i-1][1])+a[i],a[i]);//当前数不乘x
ans=max(ans,dp[i][0]);
ans=max(ans,dp[i][1]);
ans=max(ans,dp[i][2]);
}
printf("%lld\n",ans);
return 0;
}
标签:Beautiful,int,max,ll,Codeforces,ans,Array,include,dp From: https://blog.51cto.com/u_15952369/6035770