51 nod 1050

和求最大的子段和有点像,但是是循环数组,首先我们知道如果不循环 那么就和求最大字段和一样,也就是说此子段的起点是小于终点的, 如果是循环数组那么我们可以知道此时可以是子段的起点大于重点,那么对于中间的一段显然是最小的子段和,

大体就是分情况讨论

第一种:子段的起点小于终点

第二种:子段的终点小于起点,此时起点和终点的部分就是最小的子段和

#include<cmath>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
typedef long long ll;
const int qq = 50000+10;
ll dp[qq];
int main(){
	int n;scanf("%d",&n);
	ll sum = 0;
	for(int i=0; i<n; ++i)	scanf("%lld",dp+i),sum+=dp[i];
	ll maxn = 0, tot = 0;
	for(int i=0; i<n; ++i){
		tot+=dp[i];
		maxn = max(maxn, tot);
		if(tot<0)	tot = 0;
	}
	tot = 0;
	ll minx = 1e9+10;
	for(int i=0; i<n; ++i){
		tot+=dp[i];
		minx = min(minx, tot);
		if(tot>0)	tot = 0;
	}
	printf("%lld\n", max(maxn, sum-minx));
	return 0;
}

如果题目分析后可以分情况讨论,自己要意识到

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值