当前位置:网站首页>最大连续子段和(动态规划,递归,递推)
最大连续子段和(动态规划,递归,递推)
2022-07-03 04:56:00 【疯疯癫癫才自由】
状态设计:dp[i]表示以a[i]结尾的最大序列和
状态转移方程: dp[i]=max(a[i],dp[i-1]+a[i]);
/**
状态设计:dp[i]表示以a[i]结尾的最大序列和
状态转移方程: dp[i]=max(a[i],dp[i-1]+a[i]);
*/
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int n;
cout << "输入整数的个数:\n";
cin >> n;
cout << "输入 " << n << " 个整数:\n";
int a[n];
for(int i=0;i<n;++i)
cin >> a[i];
int ans=a[0];
int dp[n]; //dp[i]表示以a[i]结尾的最大序列和
dp[0]=a[0];
for(int i=1;i<n;++i)
{
dp[i]=max(a[i],dp[i-1]+a[i]);
ans=max(ans,dp[i]);
}
cout << ans << endl;
return 0;
}求序列的最大连续子段和,如果全为负数,则最大连续最大字段和为0
并且还要输出最大子段和的开始和结束下标
/**
求序列的最大连续子段和,如果全为负数,则最大连续最大字段和为0
并且还要输出最大子段和的开始和结束下标
*/
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int n;
cout << "输入整数的个数:\n";
cin >> n;
cout << "输入 " << n << " 个整数:\n";
int a[n];
for(int i=0;i<n;++i)
cin >> a[i];
int ans=0,this_sum=0;
int sum_start=0,sum_end=0;
int start=0;
for(int i=0;i<n;++i)
{
this_sum+=a[i];
if(this_sum>ans)
{
ans=this_sum;
sum_start=start;
sum_end=i;
}
else if(this_sum<0)
{
start=i+1;
this_sum=0;
}
}
cout << "max_sum: " << ans << endl;
cout << "max_sum_start_index: " << sum_start << endl;
cout << "max_sum_end_index: " << sum_end << endl;
return 0;
}二分法求解,具有重叠子问题,用递归则要中间进行一些处理操作。
/**
二分法求解
*/
#include <iostream>
#include <cstdio>
using namespace std;
int max_sum(int *a,int l,int r);
int max_sum_3(int *a,int n);
int main()
{
int n;
printf("输入数据个数:\n");
cin >> n;
int a[n];
printf("输入%d个数:\n",n);
for(int i=0;i<n;++i)
{
cin >> a[i];
}
int sum=max_sum_3(a,n-1);
cout << sum << endl;
return 0;
}
//一直二分下去,直到只有一个数,才到终止条件
int max_sum(int *a,int l,int r)
{
if(l==r)
{
if(a[l]>0)
return a[l];
else
return 0;
}
int mid=l+(r-l)/2;
int lmax=max_sum(a,l,mid);
int rmax=max_sum(a,mid+1,r);
int ltempmax=0,tempsum=0;
for(int i=mid;i>=l;--i)
{
tempsum+=a[i];
if(tempsum>ltempmax)
ltempmax=tempsum;
}
tempsum=0;
int rtempmax=0;
for(int i=mid+1;i<=r;++i)
{
tempsum+=a[i];
if(tempsum>rtempmax)
rtempmax=tempsum;
}
int midmax=ltempmax+rtempmax;
if(lmax>rmax)
{
if(lmax>midmax)
return lmax;
}
else
{
if(rmax>midmax)
return rmax;
}
return midmax;
}
int max_sum_3(int *a,int n)
{
return max_sum(a,0,n-1);
}边栏推荐
- The current market situation and development prospect of the global gluten tolerance test kit industry in 2022
- Leetcode simple problem delete an element to strictly increment the array
- Leetcode simple question: check whether the string is an array prefix
- [research materials] 2021 China's game industry brand report - Download attached
- 1119 pre- and post order traversals (30 points)
- 论文阅读_清华ERNIE
- Shell script Basics - basic grammar knowledge
- MediaTek 2023 IC written examination approved in advance (topic)
- String matching: find a substring in a string
- Silent authorization login and registration of wechat applet
猜你喜欢

Valentine's day limited withdrawal guide: for one in 200 million of you

Prepare for 2022 and welcome the "golden three silver four". The "summary of Android intermediate and advanced interview questions in 2022" is fresh, so that your big factory interview can go smoothly

SSM framework integration

I stepped on a foundation pit today

Thesis reading_ ICD code_ MSMN

"Niuke brush Verilog" part II Verilog advanced challenge

MPM model and ab pressure test

STM32 reverse entry

并发操作-内存交互操作

The least operation of leetcode simple problem makes the array increment
随机推荐
Literature reading_ Research on the usefulness identification of tourism online comments based on semantic fusion of multimodal data (Chinese Literature)
Games101 Lesson 9 shading 3 Notes
论文阅读_清华ERNIE
Preparation for school and professional cognition
Thesis reading_ Chinese NLP_ ELECTRA
Day 51 - tree problem
Handling record of electric skateboard detained by traffic police
[XSS bypass - protection strategy] understand the protection strategy and better bypass
Huawei personally ended up developing 5g RF chips, breaking the monopoly of Japan and the United States
ZABBIX monitoring of lamp architecture (3): zabbix+mysql (to be continued)
Automatic voltage rise and fall 5-40v multi string super capacitor charging chip and solution
带有注意力RPN和多关系检测器的小样本目标检测网络(提供源码和数据及下载)...
ZABBIX monitoring of lamp architecture (2): ZABBIX basic operation
[set theory] binary relation (example of binary relation operation | example of inverse operation | example of composite operation | example of limiting operation | example of image operation)
[research materials] 2021 annual report on mergers and acquisitions in the property management industry - Download attached
并发操作-内存交互操作
sql语句模糊查询遇到的问题
Market status and development prospect prediction of global SoC Test Platform Industry in 2022
【SQL注入】联合查询(最简单的注入方法)
Messy change of mouse style in win system