当前位置:网站首页>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);
}
边栏推荐
- Notes | numpy-07 Slice and index
- Thesis reading_ Tsinghua Ernie
- What is UUID
- Market status and development prospect prediction of the global fire hose industry in 2022
- Day 51 - tree problem
- Notes | numpy-10 Iterative array
- Silent authorization login and registration of wechat applet
- [backtrader source code analysis 4] use Python to rewrite the first function of backtrader: time2num, which improves the efficiency by 2.2 times
- Basic use of Metasploit penetration testing framework
- [PHP vulnerability weak type] basic knowledge, PHP weak equality, error reporting and bypassing
猜你喜欢
Coordinatorlayout appbarrayout recyclerview item exposure buried point misalignment analysis
Esp32-c3 learning and testing WiFi (II. Wi Fi distribution - smart_config mode and BlueIf mode)
cookie session jwt
Thesis reading_ Tsinghua Ernie
[research materials] the fourth quarter report of the survey of Chinese small and micro entrepreneurs in 2021 - Download attached
[set theory] relational representation (relational matrix | examples of relational matrix | properties of relational matrix | operations of relational matrix | relational graph | examples of relationa
Sdl2 + OpenGL glsl practice (Continued)
Cross platform plug-in flutter for displaying local notifications_ local_ notifications
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
SSM framework integration
随机推荐
Market status and development prospect prediction of the near infrared sensor industry of the global Internet of things in 2022
Distinguish between releases and snapshots in nexus private library
The programmer resigned and was sentenced to 10 months for deleting the code. JD came home and said that it took 30000 to restore the database. Netizen: This is really a revenge
Thesis reading_ Chinese medical model_ eHealth
移动端——uniapp开发记录(公共请求request封装)
Retirement plan fails, 64 year old programmer starts work again
String matching: find a substring in a string
The current market situation and development prospect of the global gluten tolerance test kit industry in 2022
[PHP vulnerability weak type] basic knowledge, PHP weak equality, error reporting and bypassing
论文阅读_中文NLP_ELECTRA
On typescript and grammar
The least operation of leetcode simple problem makes the array increment
First + only! Alibaba cloud's real-time computing version of Flink passed the stability test of big data products of the Institute of ICT
cookie session jwt
Market status and development prospect prediction of the global forward fluorescent microscope industry in 2022
[USACO 2009 Dec S]Music Notes
I've seen a piece of code in the past. I don't know what I'm doing. I can review it when I have time
第十九届浙江省 I. Barbecue
STM32 reverse entry
Market status and development prospect prediction of the global fire extinguisher industry in 2022