# (Interval accurately the DP +) matrix should take Los Valley P1005 (increase + / Province selected from -)

Author:  Jack_Homes_Huang  Updated: 2018-07-24 11:19   view Ta's blog Report 162   


Subject to the effect

A n-\ Times m n- × m matrix, for the first I I row, each value of the edge removal A_ {I, J} A I , J , to increase the score of the line X X(See self-rule title), Seeking n maximum sum score line n.

Analyze

  • Seeking the n- the n-biggest score line and each line access without affecting the other line, so just make sure the maximum score for each row, manage their own children on the line. (This is called optimal substructure moving in regulation)
  • Every time access is on the edge to take, then each fetch complete the remaining elements must be in a complete one section, is the optimal solution, interval DP came into being.

DP Process

(DP only for each of T T line)

status

  • We F_ {I, J} F I , J representing the interval becomes [I, J] [ I , J ], the maximum score obtained.

Metastasis

  • When the interval becomes [I, J] [ I , J ], the last access time interval must be [. 1-I, J] [ I - . 1 , J ] or [I, J +. 1] [ I , J + . 1 ], can be from two state transition. In the m-I-J. 1 + m - J + I - . 1 views (this simulated your own) removed the A_ {-I. 1, J} A I - . 1 , J , or A_ {i, j + 1} A I , J + . 1 namely:f_{i,j}=max\{f_{i-1,j}+A_{i-1,j} \cdot 2^{m-j+i-1},f_{i,j+1}+A_{i,j+1} \cdot 2^{m-j+i-1}\}fi,j=max{fi1,j+Ai1,j2mj+i1,fi,j+1+Ai,j+12mj+i1}

Final value (answer)

  • The final value of ah super annoying, is not clear if the state really have no idea.
  • Since said to take complete title, but empty section DP is not out, then each have a length of an analog manually 1 section 1. That is: Ans = max_ {I \ Leq m} \ {F_ {I, I + A_ {I}, m I ^ 2} \} A n- S = m A X I m { F I , I + A I , I 2 m }

    some(Super annoying)thing

  • I will not say why use a high-precision
  • Ah precision Hao Fan
  • Go bother bother what can I do, I will not int128

To summarize all use of high-precision

  1. High precision + + high precision

  2. High precision \ Times × Intracytoplasmic Sperm

  3. max \ { m A X {high precision , high precision \} } (manual mischievous)

Well, I do not care you want the board to stick sticky board it \ cdots

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>

using namespace std;

const int MAXN = 85, Mod = 10000; // four compression Gong is good high-precision
int n-, m;
int Ar [MAXN];

the HP {struct
int P [505], len;
the HP () {
Memset (P, 0, the sizeof P);
len = 0;
} // This is the constructor function for creating a direct high-precision variable
void print () {
the printf ( "% D", P [len]);
for (int I = len -. 1; I> 0; i--) {
IF (P [I] == 0) {
the printf ( "0000");
Continue ;
}
for (int = 10 K; K * P [I] <Mod; K * = 10)
the printf ( "0");
the printf ( "% D", P [I]);
}
} // four compression output
} f [MAXN] [MAXN] , base [MAXN], ans;

HP operator + (const HP &a, const HP &b) {
HP c; c.len = max(a.len, b.len); int x = 0;
for (int i = 1; i <= c.len; i++) {
c.p[i] = a.p[i] + b.p[i] + x;
x = c.p[i] / Mod;
c.p[i] %= Mod;
}
if (x > 0)
c.p[++c.len] = x;
return c;
} //高精+高精

HP operator * (const HP &a, const int &b) {
HP c; c.len = a.len; int x = 0;
for (int i = 1; i <= c.len; i++) {
c.p[i] = a.p[i] * b + x;
x = c.p[i] / Mod;
c.p[i] %= Mod;
}
while (x > 0)
c.p[++c.len] = x % Mod, x /= Mod;
return c;
} //高精*单精

HP max(const HP &a, const HP &b) {
if (a.len > b.len)
return a;
else if (a.len < b.len)
return b;
for (int i = a.len; i > 0; i--)
if (a.p[i] > b.p[i])
return a;
else if (a.p[i] < b.p[i])
return b;
return a;
} //比较取最大值

BaseTwo void () {
Base [0] .p [. 1] =. 1, Base [0] = .LEN. 1;
for (int I =. 1; I <= m + 2; I ++) {// This is m m! !! I m n tune TM written in n ...
Base [I] Base = [I -. 1] * 2;
}
} // a power of 2 pretreatment

int main (void) {
Scanf ( "% D% D", & n-, & m);
BaseTwo ();
the while (N--) {
Memset (F, 0, the sizeof F);
for (int I =. 1; I < = m; I ++)
Scanf ( "% D", & Ar [I]);
for (int I =. 1; I <= m; I ++)
for (int J = m; J> = I; J,) {/ / because the final value is the inter-cell, DP naturally from large section start
f [i] [j] = max (f [i] [j], f [i - 1] [j] + base [m - j + i -. 1] * Ar [I -. 1]);
F [I] [J] = max (F [I] [J], F [I] [J +. 1] + Base [m - J + I -. 1] Ar * [J +. 1]);
} // to write operator-bearing structural weight more natural
the HP max;
for (int I =. 1; I <= m; I ++)
max = max (max, F [I] [I] + Base [m] Ar * [I]);
ANS = Max + ANS; // record the total answer
}
ans.print (); // output
return 0;
}


Code Jun

Guess you like

Origin www.cnblogs.com/little-cute-hjr/p/11428720.html