"SGU" SGU daily Practice 1 · Little Shop of Flowers "DP"

Source: Internet
Author: User

Main topic:

Give you the n*m rectangle (M >= N)

Each node Mp[i][j] has a weight, from the first walk to the last row, each row will be selected only one number and for I row, the number of columns of the selected number is strictly greater than the number of columns selected I-1 row

Ask you what the maximum power value is, and output the number of n columns selected

Ideas:

DP equation very good to think: dp[i][j] = max (dp[i][j-1], dp[i-1][j-1] + mp[i][j]);

If you look for a path, you can go from i+1 to M per line, or you can start looking directly from I-1.

can also be in the DP inside Mark, state transfer when this point record!

But you can't think too much, you know.

Attached code;

#include <iostream>#include<stdio.h>#include<algorithm>#include<string.h>#include<string>#include<math.h>#include<stack>#include<queue>#include<vector>#include<map>#include<Set>#pragmaWarning (disable:4996)#defineZero (a) memset (a, 0, sizeof (a))#defineNeg (a) memset (a,-1, sizeof (a))#defineAll (a) A.begin (), A.end ()#definePB push_back#defineREPF (I,A,B) for (int i = a;i <= b; i++)#defineRepff (I,A,B) for (int i = A; I < b; ++i)#defineLson l,m,rt<<1#defineRson m+1,r,rt<<1|1#defineRoot 1,n,1#defineLD RT << 1#defineRd RT << 1 | 1#definell Long Long#defineMAXN 105#defineINF 0x3f3f3f3f#defineMoD 10007using namespaceStd;vector<int>go;intN, M, DP[MAXN][MAXN], GG[MAXN][MAXN], MP[MAXN][MAXN];voidinit () {memset (GG,0,sizeof(GG));  for(inti =1; I <= N; ++i) for(intj =1; J <= M; ++j) {scanf ("%d", &Mp[i][j]); if(I! =0) Dp[i][j] =-INF; ElseDP[I][J] =0; }     for(inti =0; I <= N; ++i) for(intj =0; J <= M; ++j) {            if(I! =0) Dp[i][j] =-INF; ElseDP[I][J] =0; } go.clear ();}intMain () { while(~SCANF ("%d%d", &n, &m))        {init ();  for(inti =1; I <= N; ++i) for(intj =1; J <= M; ++j) {                if(Dp[i-1][j-1] + mp[i][j] > Dp[i][j-1]) {Gg[i][j]=1; //cout << "Go" << Endl;DP[I][J] = dp[i-1][j-1] +Mp[i][j]; }                Else{Dp[i][j]= Dp[i][j-1]; }} printf ("%d\n", Dp[n][m]); intNOWX = N,nowy =m;  while(Nowx &&Nowy) {            if(Gg[nowx][nowy] = =1) {go.push_back (Nowy); Nowx--; Nowy--; }            Else{Nowy--; }        }        intLen =go.size ();  for(inti = len-1; I >=0; --i) {            if(I! = Len-1) printf (" "); printf ("%d", Go[i]); } puts (""); }    return 0;}

"SGU" SGU daily Practice 1 · Little Shop of Flowers "DP"

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.