题意:一个序列,在其中一个数必须替换成给定数字p的条件下,求最大连续子序列之和。
依次把每一个数替换成p,求每次的最大连续和,找出最大值。O(n^2)。
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
typedef long long ll;
using namespace std;
const int MAXN=1000+5;
const int INF=0x3f3f3f3f;
int t;
ll n,p,a[MAXN];
ll solve () {
ll tmax = -INF, sum = 0;
for (int i = 0; i < n; i++) {
if (sum < 0) sum = a[i];
else sum += a[i];
tmax = max (tmax, sum);
}
return tmax;
}
int main(){
scanf("%d",&t);
while(t--){
ll ans=-INF;
cin>>n>>p;
for(int i=0;i<n;++i)
cin>>a[i];
for(int i=0;i<n;++i){
int t=a[i];
a[i]=p;
ans=max(ans,solve());
a[i]=t;
}
cout<<ans<<endl;
}
}