dp (questions summary)

   Long time no write summed up, accumulated a lot of problems, the kinds of questions did classification summarize, here is what I encountered these kinds of questions in hdu acm step inside the type of questions, re-sort out ideas.

Questions one:

http://acm.hdu.edu.cn/showproblem.php?pid=1003

hdu1003 idea some time ago already described, and the focus here talk about the starting point of this type of exercise,

 A look at this type of exercise is part of the largest sub-sequences and demand, the starting point is to find the start and end, that is the start position and end position. At first, when I came across this question, but also learn a number of ways online, but with the deepening of learning, gradually with the point clue, in fact, the idea is basically the same and the Internet, but this is only for reference and learning, and not really achieve mastery, it will take time to it. I hope in the future of learning to join some of their own ideas. Specific ideas look at my blog now!

Questions II:

http://acm.hdu.edu.cn/showproblem.php?pid=1159

Similar to 1003, but different, to solve the maximum length common subsequence. Is also equivalent to the one deformed, that is, the legendary LCS (just learning to feel when God, Oh), this problem also have template, as follows:

15103737_Eie5.gif 15103737_xZQz.gif View Code

    
void LCS( char a[], char b[], int x, int y)
{
int i,j;
for (i = 0 ;i <= x;i ++ )
c[i][
0 ] = 0 ;
for (j = 0 ;j <= y;j ++ )
c[
0 ][j] = 0 ;

for (i = 1 ;i <= x;i ++ )
{
for (j = 1 ;j <= y;j ++ )
{
if (a[i - 1 ] == b[j - 1 ]) {c[i][j] = c[i - 1 ][j - 1 ] + 1 ;}
else { c[i][j] = Max( c[i][j - 1 ], c[i - 1 ][j]);}
}
}
}

This is the way hdu courseware inside, a good method, also used to learn, this way of thinking is still challenging for the inertia of thinking, I was to understand this problem, I took an afternoon. To finally know how the story.

   Wherein, C [i] [j] represents a character in the i-th and b j-th character in the comparison, a result of comparing two strings as before i-th character in front of the front and b j-th common character length, C [I] [J] = C [-I. 1] [j-1] + 1'd, represents a [i-1] and the [j-1] is equal to the case where b, to be equal to the character plus the number 1, i.e., c [i-1] [j -1] +1, otherwise, c [i] [j] = Max (c [i] [j-1], c [i-1] [j ]), interpreted as a in the i-th and b j-th unequal time, then it is necessary first from the front a in the i-th and b of the first j-1 th values and a in i-1 th time, and compare to find a maximum assigned c [i] [j], so that the convenience of the j-th b.

   There is there is highly dynamic, i.e., the maximum transfer of the sub-sequence length, a two-dimensional array, the original intermittent linear relationship.

 for (I = 0 ; I <= X; I ++ )
        C [I] [
0 ] = 0 ;
   
for (J = 0 ; J <= Y; J ++ )
        C [
0 ] [J] = 0 ;
this is no comparison before, the result is zero.

Questions III:

http://acm.hdu.edu.cn/showproblem.php?pid=1087

The problem, I just started to do with dfs results timeout, pruning is not so. Later This is perhaps dp, based on the idea of ​​a dfs think, and later also read the report online, indeed!

This is what I have just started using dfs do, in fact, dp and dfs idea of ​​a kind, just a lot faster than dfs from time.

15103737_Eie5.gifdfs

    
#include " iostream "
using namespace std;
int a[ 1010 ];
int n,sum;
int p = 0 ;
void dfs( int x, int y)
{
while (y <= x && x < n) y ++ ;

if (a[x] < a[y] && y < n && x < n)
{
sum
+= a[y];
x
= y;
if (x < n - 1 && y < n - 1 ) dfs(x,y);
}
else if (y < n - 1 ){ y ++ ; if (y < n - 1 ) dfs(x,y);}
}
int main()
{
int i,j;
while (cin >> n,n)
{
for (i = 0 ;i < n;i ++ ) cin >> a[i];
int Max =- 1 ;
sum
= 0 ;
for (i = 0 ;i < n;i ++ )
{
sum
= a[i];
if (Max < sum) Max = sum;
for (j = i + 1 ; j < n; j ++ )
{
p
= 0 ;
if (a[i] < a[j])
{
sum
+= a[j];
dfs(j,p);
}
if (sum > Max) Max = sum;
sum
= a[i];
}
}
cout
<< Max << endl;
}
return 0 ;
}

Later, the report looked at the online problem-solving, and soon there will be a train of thought.

State transition equation b [i] = max (b [i], b [j] + a [i]), the key point, more than dfs save a lot of unnecessary computation step, the core code:

15103737_Eie5.gif 15103737_xZQz.gif View Code

    
for (i = 1 ;i < n;i ++ )
{
b[i]
= a[i];
for (j = 0 ;j < i;j ++ )
if (a[j] < a[i] && b[i] < b[j] + a[i])
b[i]
= b[j] + a[i];
if (b[i] > Max) Max = b[i];
}


This can be readily understood, a [j] <a [i] is determined whether the pieces are in ascending order taking, as b [i] <[j] + a [i] b, was determined before the i line and and j and the front section is coupled with the i-th comparator, to find a maximum.

Finally Max and compare to find the maximum.

Questions Four:

http://acm.hdu.edu.cn/showproblem.php?pid=1160

Good morning! Continued!

   This question is a simple idea, beginning with the search results has to do w, I do not know why, look at the amount of data was not great, not big ah 1000, with the most time complexity is O (n ^ 2), in fact, really run short of this time, it is reckoned dp, and later learn the online method, instead dp, also passed. Core code:

15103737_Eie5.gif 15103737_xZQz.gif View Code

    
for (i = 1 ;i < k;i ++ )
{
a[i]
= 1 ; b[i] =- 1 ;
for (j = i - 1 ;j >= 1 ;j -- )
{
if (s[i].w > s[j].w && s[i].v < s[j].v && a[j] + 1 > a[i])
{
a[i]
= a[j] + 1 ;
b[i]
= j;
}
}

if (a[i] > Max)
{
Max
= a[i];
flag
= i;
}
}

State transition equation:

A [I] = A [J] + 1'd;
      B [I] = J; actually, b [i] is used to mark the path. As with the previous number, a [i] is the longest record incremental sequence. Conditions s [i] .w> s [ j] .w && s [i] .v <s [j] .v && a [j] +1> a [i], denoted as strictly increasing in quality, speed strictly decreasing case, comparing a [i] and a [j] +1.

Questions five:

http://acm.hdu.edu.cn/showproblem.php?pid=2571

   So this is a series of questions, very interesting, are interested can do a do, Website: http://acm.hdu.edu.cn/search.php?field=problem&key=ACM . I have done some time ago a type of question on the POJ, http://poj.org/problem?id=2704  but this problem which has been given of the state transition equation, dp [i] [j] , dp [i + 1] [ y], dp [i] [ k * y] (k> 1), so that the problem of a lot of simple. The core code is as follows,

15103737_Eie5.gif 15103737_xZQz.gif View Code

    
for (i = 1 ;i <= n;i ++ )
{
for (j = 1 ;j <= m;j ++ )
{
if (j != 1 ) dp[i][j] = Max(dp[i][j], dp[i][j - 1 ] + a[i][j]);
if (i != 1 ) dp[i][j] = Max(dp[i][j] , dp[i - 1 ][j] + a[i][j]);

for ( int p = 2 ;p <= m; p ++ )
{
if (j * p > m) break ;
dp[i][j
* p] = Max(dp[i][j] + a[i][j * p] , dp[i][j * p] );
}
}
}

Questions 6:

http://acm.hdu.edu.cn/showproblem.php?pid=1176

 At first nothing thinking, this road is dp problem, but could not find a breakthrough point, then check the Internet, very surprisingly, the number is actually always the tower model. I had to learn, and now no other ideas. Think about it slowly.

code show as below,

15103737_Eie5.gif 15103737_xZQz.gif View Code

    
for (i = 1 ;i <= n;i ++ )
{
cin
>> a >> b;
dp[b][a]
++ ;
if (Max < b) Max = b;
}

for (i = Max ; i >= 1 ; i -- )
{
for (j = 0 ;j <= 10 ; j ++ )
{
if (j == 0 )
dp[i
- 1 ][j] += MAX(dp[i][j], dp[i][j + 1 ]);
else if (j == 10 )
dp[i
- 1 ][j] += MAX(dp[i][j - 1 ],dp[i][j]);
else
dp[i
- 1 ][j] += MAX(MAX(dp[i][j - 1 ], dp[i][j]),dp[i][j + 1 ]);
}
}
cout
<< dp[ 0 ][ 5 ] << endl;

Specific ideas still thinking process. . . . . .

     

 

Reproduced in: https: //my.oschina.net/garyun/blog/602866

Guess you like

Origin blog.csdn.net/weixin_34174322/article/details/91774629