A - Max Sum Plus Plus
HDU - 1024
Topic links: https://vjudge.net/contest/68966#problem/A
topic:
Now I think you got a AC on "Max Sum" Ignatius.L the problem. To be a brave ACMer, we always challenge their own problems more difficult. Now you're faced with a more difficult problem.
A given sequence of consecutive numbers S 1, S 2, S 3 , S 4 ... S x, ... S n (1≤x≤n≤1,000,000, -32768≤Sx≤32767). We define the function sum (i, j) = S i + ... + S j (1≤i≤j≤n).
Now given an integer m (m> 0), the task is to find you for m i and j, and so that they (i 1, j 1) + sum (i 2, j 2) + sum (i 3, j 3 ) + ... + sum (im, jm) maximal ( or do not allow ix≤iy≤jx ix≤jy≤jx).
But I'm lazy, I do not want to write a special judging module, so you do not have to output m i and j, and the maximum output only the sum of (ix, jx) (1≤x ≤m ) opposite. ^ _ ^
Input
each test will start two integers m and n, followed by n integers S 1, S 2, S 3 ... S n.
Treatment to the end of the file.
Yield
outputs the sum of the maximum in a row.
Sample input
. 1 2. 3. 3. 1
2. 6. 3 -2 -2 -1. 4. 3
sample output
. 6
. 8
Ideas: borrow two pictures of other blog:
Such as: dp [2] [4] = max (max (-1,4,2,), 2) + num [4] = 7;
// // Created by hy on 2019/8/4. // #include <algorithm> #include <iostream> #include <cstdio> #include <cstring> #include <queue> #include <set> #include<math.h> using namespace std; typedef long long ll; const int maxn=1e6+10; int dp[maxn],frontmax[maxn],num[maxn]; #define MAX 0x3f3f3f3f int main() { int n,m; while(~scanf("%d%d",&m,&n)) { for(int i=1;i<=n;i++) scanf("%d",&num[i]); memset(dp,0,sizeof(dp)); memset(frontmax,0,sizeof(frontmax)); int temp; for(int i=1;i<=m;i++) { temp=MAX*(-1); for(int j = I; j <= n-; j ++ ) { DP [j] = max (frontmax [J- . 1 ], DP [J- . 1 ;]) + NUM [j] // maximum before DP [j] j memory and, frontmax j-1 previously stored maximum value frontmax [J- . 1 ] = TEMP; TEMP = max (TEMP, DP [J]); // TEMP updated value is maximum } } the printf ( " % D \ n- " , TEMP); } return 0 ; }