DP6. 连续子数组最大和
描述
输入描述
输出描述
连续子数组的最大之和。示例1
输入:
8 1 -2 3 10 -4 7 2 -5
输出:
18
说明:
经分析可知,输入数组的子数组[3,10,-4,7,2]可以求得最大和为18示例2
输入:
1 2
输出:
2
示例3
输入:
1 -10
输出:
-10
C++ 解法, 执行用时: 9ms, 内存消耗: 1948KB, 提交时间: 2022-05-08
#include <bits/stdc++.h> using namespace std; template <typename T> inline void read(T & f) { f = 0; T fu = 1; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') { fu = -1; } c = getchar(); } while (c >= '0' && c <= '9') { f = (f << 3) + (f << 1) + (c & 15); c = getchar(); } f *= fu; } template <typename T> void print(T x) { if (x < 0) putchar('-'), x = -x; if (x < 10) putchar(x + 48); else print(x / 10), putchar(x % 10 + 48); } template <typename T> void print(T x, char t) { print(x); putchar(t); } const int maxn = 2e5 + 50; int n, a[maxn], dp[maxn]; void solve() { read(n); for (int i = 1; i <= n; ++i) read(a[i]); dp[1] = a[1]; int ans = -0x3f3f3f3f; for (int i = 1; i <= n; ++i) { dp[i] = max(dp[i - 1] + a[i], a[i]); ans = max(ans, dp[i]); } print(ans); } int main() { #ifdef AC freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout); #endif clock_t program_start_clock = clock(); solve(); fprintf(stderr, "\nTotal Time: %lf ms", double(clock() - program_start_clock) / (CLOCKS_PER_SEC / 1000)); return 0; }
C++ 解法, 执行用时: 9ms, 内存消耗: 1948KB, 提交时间: 2022-03-05
#include <bits/stdc++.h> using namespace std; template <typename T> inline void read(T &f) { f=0;T fu=1;char c=getchar(); while(c<'0'||c>'9') {if(c=='-'){fu=-1;}c=getchar();} while(c>='0'&&c<='9') {f=(f<<3)+(f<<1)+(c&15);c=getchar();} f*=fu; } template <typename T> void print(T x) { if(x<0) putchar('-'),x=-x; if(x<10) putchar(x+48); else print(x/10),putchar(x%10+48); } template <typename T> void print(T x, char t) { print(x);putchar(t); } const int maxn=2e5+50; int n,a[maxn],dp[maxn]; void solve() { read(n); for(int i=1;i<=n;++i) read(a[i]); dp[1]=a[1]; int ans=-0x3f3f3f3f; for(int i=1;i<=n;++i) { dp[i]=max(dp[i-1]+a[i],a[i]); ans=max(ans,dp[i]); } print(ans); } int main() { #ifdef AC freopen("in.txt","r",stdin); freopen("out.txt","w",stdout); #endif clock_t program_start_clock=clock(); solve(); fprintf(stderr,"\nTotal Time: %lf ms",double(clock()-program_start_clock)/(CLOCKS_PER_SEC/1000)); return 0; }
C++ 解法, 执行用时: 9ms, 内存消耗: 1972KB, 提交时间: 2022-01-22
#include <bits/stdc++.h> #define inf 2147483647 // #define int long long using namespace std ; const int N = 1e6 + 10 ; int read() { int x = 0 , f = 1 ; char ch = getchar() ; while(!isdigit(ch)) {if(ch == '-') f = - 1 ; ch = getchar() ;} while( isdigit(ch)) {x = x * 10 + ch - '0' ; ch = getchar() ;} return x * f ; } int n , Ans = - inf ; int f[N] , a[N] ; signed main(){ n = read() ; // if (n >= 200000) return -1; for(int i = 1 ; i <= n ; i++) a[i] = read() ; f[1] = a[1] ; for(int i = 2 ; i <= n ; i++) { f[i] = max(f[i - 1] + a[i] , a[i]); Ans = max(Ans , f[i]) ; } Ans = max(Ans , f[1]) ; printf("%d\n" , Ans) ; return 0 ; }
C++ 解法, 执行用时: 10ms, 内存消耗: 3492KB, 提交时间: 2021-10-20
#include <bits/stdc++.h> #define inf 2147483647 #define int long long using namespace std ; const int N = 1e6 + 10 ; int read() { int x = 0 , f = 1 ; char ch = getchar() ; while(!isdigit(ch)) {if(ch == '-') f = - 1 ; ch = getchar() ;} while( isdigit(ch)) {x = x * 10 + ch - '0' ; ch = getchar() ;} return x * f ; } int n , Ans = - inf ; int f[N] , a[N] ; signed main(){ n = read() ; for(int i = 1 ; i <= n ; i++) a[i] = read() ; f[1] = a[1] ; for(int i = 2 ; i <= n ; i++) { f[i] = max(f[i - 1] + a[i] , a[i]); Ans = max(Ans , f[i]) ; } Ans = max(Ans , f[1]) ; printf("%lld\n" , Ans) ; return 0 ; }
C++ 解法, 执行用时: 10ms, 内存消耗: 3560KB, 提交时间: 2021-10-25
#include <bits/stdc++.h> #define inf 2147483647 #define int long long using namespace std ; const int N = 1e6 + 10 ; int read() { int x = 0 , f = 1 ; char ch = getchar() ; while(!isdigit(ch)) {if(ch == '-') f = - 1 ; ch = getchar() ;} while( isdigit(ch)) {x = x * 10 + ch - '0' ; ch = getchar() ;} return x * f ; } int n , Ans = - inf ; int f[N] , a[N] ; signed main(){ n = read() ; for(int i = 1 ; i <= n ; i++) a[i] = read() ; f[1] = a[1] ; for(int i = 2 ; i <= n ; i++) { f[i] = max(f[i - 1] + a[i] , a[i]); Ans = max(Ans , f[i]) ; } Ans = max(Ans , f[1]) ; printf("%lld\n" , Ans) ; return 0 ; }