emmmmm……还以为是数组复制一遍求最大子段和……然额……并不是……想不太到逆向思维的感觉……
循环数组的最大子段和转化成非循环数组可能有两种,一种是非循环数组的最大子段和(大概就是那种连续的,不跨越1和n那种),另一种是sum-最小子段和。
#include<iostream> #include<algorithm> #include<string> #include<string.h> typedef long long ll; using namespace std; ll n,res,a[50005],b[50005],crt; ll dp,dpp,sum; int main() { while (scanf("%lld", &n) != EOF) { sum = 0; crt = 0; ll minn = 0x3f3f3f3f,maxx=-0x3f3f3f3f; for (int i = 1; i <= n; i++) { scanf("%lld", &a[i]); sum += a[i]; b[i] = a[i]; if (a[i] < 0) crt++; } if (crt == n) break; for (int i = 1; i <= n; i++) { dp += a[i]; dpp+=b[i]; if (dp >maxx) maxx = dp; if(dpp<minn) minn=dpp; if (dp < 0) dp = 0; if(dpp>0) dpp=0; } printf("%lld ", max(sum - minn,maxx)); } return 0; }