Hdu-1003-max Sum && poj-1050-to the Max (classic DP problem)

Source: Internet
Author: User
Tags cmath


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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.