Topic Transfer: HDU-1003
Train of thought: maximal sub-sequences and
dp[i]= A[i] (dp[i-1]<0)
dp[i]= Dp[i-1]+a[i] (dp[i-1]>=0)
AC Code:
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include < cmath> #include <queue> #include <stack> #include <vector> #include <map> #include <set > #include <deque> #include <cctype> #define LL long long#define INF 0x7fffffffusing namespace Std;int n;int Main () {int t;int cas = 1;scanf ("%d", &t), while (T--) {scanf ("%d", &n); int sum = 0;int ans =-inf;int from, To;int Qi = 1, zhong = 1;for (int i = 1; I <= n; i + +) {int t;scanf ("%d", &t); sum + = T;zhong = i;if (Sum > ans) {ans = s Um from = Qi; to = Zhong; }if (Sum < 0) {Qi = i + 1; sum = 0;}} printf ("Case%d:\n%d%d%d\n", CAs + +, ans, from, to); if (T! = 0) printf ("\ n");} return 0;}
Topic Transfer: POJ-1050
Idea: the maximal sub-matrix and, the principle and the above question, is the i~j row of the column on the number of rows to go, and then calculate the maximum sub-sequence of the row and can (0<=i<=j <n)
AC Code:
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include < cmath> #include <queue> #include <stack> #include <vector> #include <map> #include <set > #include <deque> #include <cctype> #define LL long long#define INF 0x7fffffffusing namespace Std;int a[105 ][105];int tmp[105];int n;int Fun () {int max =-1, sum = 0;for (int i = 0; i < n; i + +) {sum + = tmp[i];if (Sum > Max) max = sum;if (sum < 0) sum = 0;} return Max;} int main () {while (scanf ("%d", &n)! = EOF) {for (int i = 0; i < n; i + +) {for (int j = 0; J < N; j + +) {scanf ("%d", &A[I][J]);}} int ans = -1;for (int i = 0; i < n; i + +) {memset (tmp, 0, sizeof (TMP)), for (int j = i; J < N; j + +) {for (int k = 0; k < n; K + +) {Tmp[k] + = A[j][k];} int t = fun (); if (ans < t) ans = t;}} cout << ans << endl;} return 0;}
Hdu-1003-max Sum && poj-1050-to the Max (classic DP problem)