当前位置:网站首页>最大连续子段和(动态规划,递归,递推)
最大连续子段和(动态规划,递归,递推)
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);
}边栏推荐
- [XSS bypass - protection strategy] understand the protection strategy and better bypass
- Leetcode simple problem delete an element to strictly increment the array
- Realize file download through the tag of < a > and customize the file name
- Leetcode simple question: check whether two string arrays are equal
- Shell script -- condition judgment
- Unity tool Luban learning notes 1
- [research materials] 2022q1 game preferred casual game distribution circular - Download attached
- C primre plus Chapter 10 question 6 inverted array
- Notes | numpy-07 Slice and index
- [PCL self study: filtering] introduction and use of various filters in PCL (continuously updated)
猜你喜欢

Apache MPM model and ab stress test

Interface frequency limit access

Compile and decompile GCC common instructions

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_ ICD code_ MSMN

Use Sqlalchemy module to obtain the table name and field name of the existing table in the database

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

ZABBIX monitoring of lamp architecture (2): ZABBIX basic operation

论文阅读_清华ERNIE

RT thread flow notes I startup, schedule, thread
随机推荐
[research materials] annual report of China's pension market in 2021 - Download attached
Market status and development prospect prediction of the global fire alarm sensor industry in 2022
Coordinatorlayout appbarrayout recyclerview item exposure buried point misalignment analysis
Market status and development prospects of the global automatic tea picker industry in 2022
[PCL self study: filtering] introduction and use of various filters in PCL (continuously updated)
[research materials] 2021 annual report on mergers and acquisitions in the property management industry - Download attached
Market status and development prospect prediction of global colorimetric cup cover industry in 2022
Kept hot standby and haproxy
Mobile terminal - uniapp development record (public request encapsulation)
移动端——uniapp开发记录(公共请求request封装)
Source insight garbled code solution
Hire cashier (differential constraint)
Shuttle + alluxio accelerated memory shuffle take-off
Market status and development prospect prediction of the global forward fluorescent microscope industry in 2022
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
Leetcode simple question: check whether the string is an array prefix
并发操作-内存交互操作
Silent authorization login and registration of wechat applet
Wechat applet distance and map
Learning practice: comprehensive application of cycle and branch structure (I)