标签:std scan 最大子段和 span div i++ while lan col
AC_Code
1 #include <iostream> 2 #include <cstdio> 3 #include <string> 4 #include <cstring> 5 #include <string> 6 #include <cmath> 7 #include <cstdlib> 8 #include <algorithm> 9 using namespace std; 10 typedef long long ll; 11 const int maxn = 50010; 12 const ll inf=0x3f3f3f3f; 13 14 int a[maxn]; 15 int leftt[maxn],rightt[maxn], leftmax[maxn],rightmax[maxn]; 16 17 int main() 18 { 19 int t; 20 scanf("%d",&t); 21 while( t-- ){ 22 int n; 23 scanf("%d",&n); 24 for(int i=1;i<=n;i++){ 25 scanf("%d",&a[i]); 26 } 27 leftt[1]=a[1]; 28 leftmax[1]=a[1]; 29 rightt[n]=a[n]; 30 rightmax[n]=a[n]; 31 32 for(int i=2;i<=n;i++){ 33 leftt[i]=max(leftt[i-1]+a[i], a[i]); 34 } 35 for(int i=1;i<=n;i++){ 36 leftmax[i]=max(leftmax[i-1],leftt[i]); 37 } 38 for(int i=n-1;i>=1;i--){ 39 rightt[i]=max(rightt[i+1]+a[i],a[i]); 40 } 41 for(int i=n-1;i>=1;i--){ 42 rightmax[i]=max(rightt[i],rightmax[i+1]); 43 } 44 45 int ans=-inf; 46 for(int i=2;i<=n;i++){ 47 ans=max(ans,leftmax[i-1]+rightmax[i]); 48 } 49 printf("%d\n",ans); 50 } 51 return 0; 52 }
标签:std scan 最大子段和 span div i++ while lan col
原文地址:https://www.cnblogs.com/wsy107316/p/12244808.html