[最大子序列和]Hdu 5280 Senior's Array

题意:一个序列,在其中一个数必须替换成给定数字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;
	}
}

  

转载于:https://www.cnblogs.com/bruce27/p/4649435.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值