当前位置:网站首页>最大连续子段和(动态规划,递归,递推)
最大连续子段和(动态规划,递归,递推)
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);
}
边栏推荐
- Learning record of arouter principle
- The simple problem of leetcode: dismantling bombs
- Sprintf formatter abnormal exit problem
- [BMZCTF-pwn] 20-secret_ file
- Leetcode simple question: check whether the string is an array prefix
- C primre plus Chapter 10 question 6 inverted array
- stm32逆向入门
- Symbol of array element product of leetcode simple problem
- The consumption of Internet of things users is only 76 cents, and the price has become the biggest obstacle to the promotion of 5g industrial interconnection
- Number of uniform strings of leetcode simple problem
猜你喜欢
On typescript and grammar
UiPath实战(08) - 选取器(Selector)
Career planning of counter attacking College Students
Use Sqlalchemy module to obtain the table name and field name of the existing table in the database
论文阅读_ICD编码_MSMN
Thesis reading_ Tsinghua Ernie
[XSS bypass - protection strategy] understand the protection strategy and better bypass
Retirement plan fails, 64 year old programmer starts work again
[clock 223] [binary tree] [leetcode high frequency]: 102 Sequence traversal of binary tree
Learning practice: comprehensive application of cycle and branch structure (I)
随机推荐
First + only! Alibaba cloud's real-time computing version of Flink passed the stability test of big data products of the Institute of ICT
Wechat applet distance and map
带有注意力RPN和多关系检测器的小样本目标检测网络(提供源码和数据及下载)...
论文阅读_中文医疗模型_ eHealth
[luatos sensor] 1 light sensing bh1750
Thesis reading_ Chinese medical model_ eHealth
Current market situation and development prospect prediction of global direct energy deposition 3D printer industry in 2022
移动端——uniapp开发记录(公共请求request封装)
Truncated sentences of leetcode simple questions
Number of 1 in binary (simple difficulty)
General undergraduate college life pit avoidance Guide
[luatos sensor] 2 air pressure bmp180
The least operation of leetcode simple problem makes the array increment
Hire cashier (differential constraint)
[USACO 2009 Dec S]Music Notes
LVS load balancing cluster of efficient multi-purpose cluster (NAT mode)
Market status and development prospect prediction of the global autonomous hybrid underwater glider industry in 2022
The simple problem of leetcode: dismantling bombs
[research materials] annual report of China's pension market in 2021 - Download attached
[XSS bypass - protection strategy] understand the protection strategy and better bypass