当前位置:网站首页>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);
}边栏推荐
- Career planning of counter attacking College Students
- cookie session jwt
- Shell script -- condition judgment
- LVS load balancing cluster of efficient multi-purpose cluster (NAT mode)
- [SQL injection point] location and judgment of the injection point
- Literature reading_ Research on the usefulness identification of tourism online comments based on semantic fusion of multimodal data (Chinese Literature)
- Thesis reading_ Chinese medical model_ eHealth
- [research materials] 2022q1 game preferred casual game distribution circular - Download attached
- 2022-02-11 daily clock in: problem fine brush
- Current market situation and development prospect prediction of global direct energy deposition 3D printer industry in 2022
猜你喜欢

论文阅读_清华ERNIE
![[research materials] 2022q1 game preferred casual game distribution circular - Download attached](/img/13/5a67c5d08131745759fdc70a71cf0f.jpg)
[research materials] 2022q1 game preferred casual game distribution circular - Download attached

The reason why the entity class in the database is changed into hump naming
![[clock 223] [binary tree] [leetcode high frequency]: 102 Sequence traversal of binary tree](/img/0f/bc8c44aee7a2c9dccac050b1060017.jpg)
[clock 223] [binary tree] [leetcode high frequency]: 102 Sequence traversal of binary tree

Handling record of electric skateboard detained by traffic police

Flutter monitors volume to realize waveform visualization of audio

Automatic voltage rise and fall 5-40v multi string super capacitor charging chip and solution

Network security textual research recommendation

UiPath实战(08) - 选取器(Selector)

RT thread flow notes I startup, schedule, thread
随机推荐
【XSS绕过-防护策略】理解防护策略,更好的绕过
Market status and development prospect prediction of the global fire alarm sensor industry in 2022
Wechat applet distance and map
Messy change of mouse style in win system
Analysis of proxy usage of ES6 new feature
Notes | numpy-09 Broadcast
50 practical applications of R language (36) - data visualization from basic to advanced
Keepalived热备与HAProxy
论文阅读_中文NLP_ELECTRA
STM32 reverse entry
1107 social clusters (30 points)
The reason why the entity class in the database is changed into hump naming
General undergraduate college life pit avoidance Guide
Market status and development prospect prediction of the global fire hose industry in 2022
The current market situation and development prospect of the global gluten tolerance test kit industry in 2022
[USACO 2009 Dec S]Music Notes
Basic use of Metasploit penetration testing framework
Day 51 - tree problem
MC Layer Target
雇佣收银员(差分约束)