51nod 1050 循环数组最大子段和
来源:互联网 发布:对外贸易数据 编辑:程序博客网 时间:2024/05/22 15:04
就是三种状态想出来就行了,开始想出来两种,看了一组测试数据想了一种。
1.正常的最大连续序列2.开始在尾,结束的首第一种正常求,第二种考虑答案的组成,开始的一段+到结尾的一段,那么中间为什么去掉呢,因为中间那段和为负数,只要求出负数最大的连续子序列去掉就行了max(ans1,sum+ans2);中间那段挖空了,所以首尾要接起来,总和减去那段负数的最大连续字段就行了,
#include<bits/stdc++.h>using namespace std;long long a[51000],maxn,now,first,min1=0,tmp,sum=0;int main(){int n,flag=0;tmp=0;ios::sync_with_stdio();first=maxn=now=0;cin>>n;for(int i=1;i<=n;i++){ cin>>a[i]; sum+=a[i]; now+=a[i]; tmp-=a[i]; min1=max(min1,tmp); if(tmp<0)tmp=0; maxn=max(maxn,now); if(now<0) { now=0; if(flag==0)first=maxn; flag=1; }}//cout<<maxn<<endl;first+=now;maxn=max(maxn,sum+min1);maxn=max(maxn,first);cout<<maxn<<endl; return 0;}
阅读全文
0 0
- 【循环数组最大子段和】51nod 1050 循环数组最大子段和
- 51nod 1050 循环数组最大子段和
- 51nod 1050 循环数组最大子段和 dp
- 51nod 1050 循环数组最大子段和
- 51nod 1050 循环数组最大子段和
- 51nod 1050 循环数组最大子段和
- 51nod 1050 循环数组最大子段和
- 51nod-1050 循环数组最大子段和
- 51nod 1050 循环数组最大子段和
- 51Nod 1050 循环数组最大子段和
- 51nod 1050 循环数组最大子段和
- 51Nod 1050 循环数组最大子段和
- 51nod 1050 循环数组最大子段和
- 51nod 1050 循环数组最大子段和
- 51nod 1050 循环数组最大子段和
- 51nod 1050 循环数组的最大子段和
- 51NOD 1050 循环数组最大子段和
- 51nod 1050 循环数组最大子段和
- html5新特性总结
- LeetCode笔记 -- Add Two Number
- LinuxStudyNote(40)-RPM包的命名规则
- 深度学习(3):优化神经网络(1)
- leetcode 344. Reverse String 反转字符串
- 51nod 1050 循环数组最大子段和
- Hadoop问题:hadoop集群NameNode成功启动,而datanode未启动
- Ubuntu 安装配置ftp
- python数与字符串
- Maven 管理SSM框架的pom.xml配置文件
- leetcode 345. Reverse Vowels of a String 反转字符串元音字符
- java递归实现选择排序
- Digits of Factorial
- python中的彩蛋