#include <stdio.h> #define maxn 100000 + 2 int arr[maxn]; int main(){ int t, n, maxLeft, maxRight, maxSum, id = 1; int thisSum; scanf("%d", &t); while(t--){ scanf("%d", &n); for(int i = 0; i < n; ++i) scanf("%d", &arr[i]); maxSum = arr[0]; maxLeft = maxRight = 0; /*maxSubsequenceSum------O(N^3)*/ for(int i = 0; i < n; ++i){ for(int j = i; j < n; ++j){ thisSum = 0; for(int k = i; k <= j; ++k){ thisSum += arr[k]; } if(thisSum > maxSum){ maxSum = thisSum; maxLeft = i; maxRight = j; } } } printf("Case %d:\n%d %d %d\n", id++, maxSum, maxLeft + 1, maxRight + 1); if(t) printf("\n"); } return 0; }
#include <stdio.h> #define maxn 100000 + 2 int arr[maxn]; int main(){ int t, n, maxLeft, maxRight, maxSum, id = 1; int thisSum; scanf("%d", &t); while(t--){ scanf("%d", &n); for(int i = 0; i < n; ++i) scanf("%d", &arr[i]); maxSum = arr[0]; maxLeft = maxRight = 0; /*maxSubsequenceSum------O(N^2)*/ for(int i = 0; i < n; ++i){ thisSum = 0; for(int j = i; j < n; ++j){ thisSum += arr[j]; if(thisSum > maxSum){ maxSum = thisSum; maxLeft = i; maxRight = j; } } } printf("Case %d:\n%d %d %d\n", id++, maxSum, maxLeft + 1, maxRight + 1); if(t) printf("\n"); } return 0; }
#include <stdio.h> #define maxn 100000 + 2 int arr[maxn]; int t, n, maxLeft, maxRight, maxSum, id = 1; int max3(int a, int b, int c){ if(a >= b && a >= c) return 1; if(b >= a && b >= c) return 2; if(c >= a && c >= b) return 3; } int maxSubsequenceSum(int left, int right, int *l, int *r){ int thisLeft, thisRight; int leftSum, rightSum, midSum, mid; int leftBorderSum, maxLeftBorderSum; int rightBorderSum, maxRightBorderSum; int ll, lr, rl, rr, ml, mr; if(left == right){ *l = *r = left; return arr[left]; } mid = (left + right) / 2; leftSum = maxSubsequenceSum(left, mid, &ll, &lr); rightSum = maxSubsequenceSum(mid + 1, right, &rl, &rr); leftBorderSum = 0; thisLeft = mid; maxLeftBorderSum = arr[mid]; for(int i = mid; i >= left; --i){ leftBorderSum += arr[i]; if(leftBorderSum >= maxLeftBorderSum){ maxLeftBorderSum = leftBorderSum; thisLeft = i; } } rightBorderSum = 0; thisRight = mid + 1; maxRightBorderSum = arr[mid + 1]; for(int i = mid + 1; i <= right; ++i){ rightBorderSum += arr[i]; if(rightBorderSum > maxRightBorderSum){ maxRightBorderSum = rightBorderSum; thisRight = i; } } midSum = maxLeftBorderSum + maxRightBorderSum; int sign = max3(leftSum, midSum, rightSum); if(sign == 1){ maxSum = leftSum; *l = ll; *r = lr; }else if(sign == 2){ maxSum = midSum; *l = thisLeft; *r = thisRight; }else{ maxSum = rightSum; *l = rl; *r = rr; } return maxSum; } int main(){ scanf("%d", &t); while(t--){ scanf("%d", &n); for(int i = 0; i < n; ++i) scanf("%d", &arr[i]); maxSum = arr[0]; maxLeft = maxRight = 0; maxSubsequenceSum(0, n - 1, &maxLeft, &maxRight); printf("Case %d:\n%d %d %d\n", id++, maxSum, maxLeft + 1, maxRight + 1); if(t) printf("\n"); } return 0; }
#include <stdio.h> int main(){ int t, n, maxLeft, maxRight, maxSum, temp; int thisLeft, thisSum; scanf("%d", &t); for(int id = 1; id <= t; ++id){ scanf("%d", &n); scanf("%d", &maxSum); thisLeft = maxLeft = maxRight = 0; thisSum = maxSum; if(thisSum < 0){ thisSum = 0; thisLeft = 1; } for(int i = 1; i < n; ++i){ scanf("%d", &temp); thisSum += temp; if(thisSum > maxSum){ maxSum = thisSum; maxLeft = thisLeft; maxRight = i; } if(thisSum < 0){ thisLeft = i + 1; thisSum = 0; } } printf("Case %d:\n%d %d %d\n", id, maxSum, maxLeft + 1, maxRight + 1); if(id != t) printf("\n"); } return 0; }
HDU1003 Max Sum 最大子序列和的问题【四种算法分析+实现】,布布扣,bubuko.com
HDU1003 Max Sum 最大子序列和的问题【四种算法分析+实现】
原文:http://blog.csdn.net/chang_mu/article/details/26157759