当前位置:网站首页>Maximum continuous sub segment sum (dynamic programming, recursive, recursive)
Maximum continuous sub segment sum (dynamic programming, recursive, recursive)
2022-07-03 04:59:00 【Madness makes freedom】
State Design :dp[i] Said to a[i] Maximum sequence sum at the end
State transition equation : dp[i]=max(a[i],dp[i-1]+a[i]);
/**
State Design :dp[i] Said to a[i] Maximum sequence sum at the end
State transition equation : dp[i]=max(a[i],dp[i-1]+a[i]);
*/
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int n;
cout << " Enter the number of integers :\n";
cin >> n;
cout << " Input " << n << " It's an integer :\n";
int a[n];
for(int i=0;i<n;++i)
cin >> a[i];
int ans=a[0];
int dp[n]; //dp[i] Said to a[i] Maximum sequence sum at the end
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;
} Find the maximum continuous subsegment sum of the sequence , If all are negative , Then the maximum continuous maximum field sum is 0
And also output the start and end subscripts of the maximum sub segment sum
/**
Find the maximum continuous subsegment sum of the sequence , If all are negative , Then the maximum continuous maximum field sum is 0
And also output the start and end subscripts of the maximum sub segment sum
*/
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int n;
cout << " Enter the number of integers :\n";
cin >> n;
cout << " Input " << n << " It's an integer :\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;
}The dichotomy solves , With overlapping subproblems , Using recursion requires some processing operations in the middle .
/**
The dichotomy solves
*/
#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(" Enter the number of data :\n");
cin >> n;
int a[n];
printf(" Input %d Number :\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;
}
// Keep going in two , Until there's only one number , Until the termination condition
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);
}边栏推荐
- 1110 complete binary tree (25 points)
- Interface frequency limit access
- 2022-02-11 daily clock in: problem fine brush
- Hire cashier (differential constraint)
- Network security textual research recommendation
- 【工具跑SQL盲注】
- Market status and development prospect prediction of the global fire hose industry in 2022
- 论文阅读_清华ERNIE
- Messy change of mouse style in win system
- 1107 social clusters (30 points)
猜你喜欢

Thesis reading_ Tsinghua Ernie
![[research materials] 2021 China's game industry brand report - Download attached](/img/b7/a377b0b7c742078e2feb28ebfbca62.jpg)
[research materials] 2021 China's game industry brand report - Download attached

Introduction to message queuing (MQ)

【批处理DOS-CMD命令-汇总和小结】-CMD窗口的设置与操作命令-关闭cmd窗口、退出cmd环境(exit、exit /b、goto :eof)

Shuttle + Alluxio 加速内存Shuffle起飞

Review the configuration of vscode to develop golang
![[USACO 2009 Dec S]Music Notes](/img/e6/282a8820becdd24d63dcff1b81fcaf.jpg)
[USACO 2009 Dec S]Music Notes

Mobile terminal - uniapp development record (public request encapsulation)

Interface frequency limit access

UiPath实战(08) - 选取器(Selector)
随机推荐
C language self-made Games: Sanzi (tic tac toe chess) intelligent chess supplement
[research materials] the fourth quarter report of the survey of Chinese small and micro entrepreneurs in 2021 - Download attached
Market status and development prospects of the global IOT active infrared sensor industry in 2022
《牛客刷verilog》Part II Verilog进阶挑战
sql语句模糊查询遇到的问题
The current market situation and development prospect of the global gluten tolerance test kit industry in 2022
Current market situation and development prospect forecast of global UV sensitive resin 3D printer industry in 2022
The reason why the entity class in the database is changed into hump naming
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
Literature reading_ Research on the usefulness identification of tourism online comments based on semantic fusion of multimodal data (Chinese Literature)
Interface frequency limit access
@RequestMapping
Network security textual research recommendation
LVS load balancing cluster of efficient multi-purpose cluster (NAT mode)
[SQL injection] joint query (the simplest injection method)
1087 all roads lead to Rome (30 points)
Hire cashier (differential constraint)
论文阅读_清华ERNIE
document. The problem of missing parameters of referer is solved
Compile and decompile GCC common instructions