当前位置:网站首页>最大连续子段和(动态规划,递归,递推)
最大连续子段和(动态规划,递归,递推)
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);
}边栏推荐
- [research materials] the fourth quarter report of the survey of Chinese small and micro entrepreneurs in 2021 - Download attached
- sql语句模糊查询遇到的问题
- Market status and development prospect prediction of the global fire hose industry in 2022
- 文献阅读_基于多模态数据语义融合的旅游在线评论有用性识别研究(中文文献)
- [PHP vulnerability weak type] basic knowledge, PHP weak equality, error reporting and bypassing
- Realize file download through the tag of < a > and customize the file name
- MySQL winter vacation self-study 2022 12 (3)
- RT thread flow notes I startup, schedule, thread
- 关于开学的准备与专业认知
- [clock 223] [binary tree] [leetcode high frequency]: 102 Sequence traversal of binary tree
猜你喜欢

Truncated sentences of leetcode simple questions

Compile and decompile GCC common instructions

"Niuke brush Verilog" part II Verilog advanced challenge

【工具跑SQL盲注】

ZABBIX monitoring of lamp architecture (3): zabbix+mysql (to be continued)

stm32逆向入门

Handling record of electric skateboard detained by traffic police

《牛客刷verilog》Part II Verilog进阶挑战

Small sample target detection network with attention RPN and multi relationship detector (provide source code, data and download)

Web security - CSRF (token)
随机推荐
Review the old and know the new: Notes on Data Science
Thesis reading_ Chinese NLP_ ELECTRA
[luatos sensor] 1 light sensing bh1750
[clock 223] [binary tree] [leetcode high frequency]: 102 Sequence traversal of binary tree
MPM model and ab pressure test
Market status and development prospect prediction of global fermentation acid industry in 2022
第十九届浙江省 I. Barbecue
Wechat applet waterfall flow and pull up to the bottom
Leetcode simple question: check whether two string arrays are equal
Day 51 - tree problem
[research materials] the fourth quarter report of the survey of Chinese small and micro entrepreneurs in 2021 - Download attached
JDBC database operation
论文阅读_中文NLP_ELECTRA
[Yu Yue education] basic reference materials of interchangeability and measurement technology of Zhongyuan Institute of Technology
Career planning of counter attacking College Students
文献阅读_基于多模态数据语义融合的旅游在线评论有用性识别研究(中文文献)
【PHP漏洞-弱类型】基础知识、php弱相等、报错绕过
Current market situation and development prospect prediction of global direct energy deposition 3D printer industry in 2022
MC Layer Target
Automatic voltage rise and fall 5-40v multi string super capacitor charging chip and solution