Joj water DP Summary

Source: Internet
Author: User

Take some time to sort out the previous water issues, consolidate the foundation, and hope to improve the detailed handling.

The problems that can be solved using dynamic planning must meet the demands of No-aftereffect (DAG) and optimal sub-structure.

 

[Note]

/*************************************** **************************************** ****/

2526: Medic medicine collection problem, classic 01 backpack, no need to fill

1-dimensional space, better understanding with the idea of Generating Functions of combined mathematics

#include <cstdio>#include <string.h>const int maxn=102;int w[maxn],v[maxn],ans[1005];int main (){    int t,m,i,j;    while (scanf("%d%d",&t,&m)==2)    {        for (i=0 ; i<m ; ++i)         scanf("%d%d",w+i,v+i);        memset (ans,0,sizeof(ans));        for (i=0 ; i<m ; i++)//0...N         for (j=t ; j>=w[i] ; --j)//V...cost         {                ans[j]>?=ans[j-w[i]]+v[i];         }         //for (i=0 ; i<=t ; i++)          printf("%d\n",ans[t]);    }    return 0;}

/*************************************** **************************************** ****/

1424 1583 2201 1176 can be regarded as quality 1, full backpack counting problem, pay attention to initialization, it is better to understand the generation function

# Include <stdio. h> # include <memory. h> using namespace STD; const int maxn = 7500; int C [5] = {, 50}; int V; int f [maxn]; int main () {memset (F, 0, sizeof (f); F [0] = 1; for (INT I = 0; I <5; I ++) for (Int J = C [I]; j <= maxn; j ++) {f [J] + = f [J-C [I]; // important} while (scanf ("% d", & V) = 1) {printf ("% d \ n", F [v]);} return 0 ;}

/*************************************** **************************************** ****/

 

Poj 2392 multi-backpack Template

#include <cstdio>#include <cstring>#include <algorithm>#define max(a,b) (a>b?a:b)using namespace std;const int maxn=500;int dp[5][40050];struct Item {    int h,a,c;}blo[maxn];bool cmp(const Item & a , const Item & b){    return a.a<b.a;}void O1Pack(int f[] , int c , int w , int V){    for (int v=V ; v>=c ; --v)        f[v]=max(f[v-c]+w , f[v]);}void ComPack(int f[] , int c , int w , int V){    for (int v=c ; v<=V ; ++v)        f[v]=max(f[v-c]+w , f[v]);}void MulPack(int f[] , int c , int w , int m , int V){    if(c*m>=V)    {        ComPack(f , c , w , V);        return ;    }    int k=1;    while (k<m)    {        O1Pack(f , k*c , k*w , V);        m-=k;        k*=2;    }    O1Pack(f , c*m , w*m , V);}int main (){    int c,w,m;    while (~scanf("%d",&m))    {        memset (dp , 0 , sizeof(dp));        for (int i=0 ; i<m ; ++i)        {            scanf("%d%d%d",&blo[i].h , &blo[i].a , &blo[i].c);        }        sort(blo , blo+m , cmp);        for (int i=0 ; i<m ; ++i)        {            MulPack(dp[0] , blo[i].h , blo[i].h , blo[i].c , blo[i].a);        }        int ans=0;        for (int i=0 ; i<=blo[m-1].a ; ++i)        ans=max(dp[0][i],ans);        printf("%d\n",ans);    }    return 0;}

/*************************************** **************************************** ****/

HDU 1561 generalized backpack

/** O (N * V) general knapsack algorithm from the paper [several categories of knapsack questions] 4686406 2011-10-01 14:23:10 accepted 1561 0 Ms 372 K 1776 B G ++ geners **/# include <cstdio> # include <cstring> # define max (, b) (A> B? A: B) const int maxn = 205; struct edge {int V, next;} edge [maxn]; int head [maxn], CNT; void addedge (INT U, int v) {edge [CNT]. V = V; edge [CNT]. next = head [u]; head [u] = CNT ++;} int W [maxn], DP [maxn] [maxn], node; int n, m; void DFS (int u, int c) {for (int p = head [u]; ~ P; P = edge [p]. next) {Int & V = edge [p]. v; For (Int J = 0; j <C; ++ J) DP [v] [J] = DP [u] [J] + W [v]; /// When retrieving a son, the father must take it. Therefore, the father is directly put into the son, DFS (v, C-1); // update the son for (Int J = 1; j <= C; ++ J) // calculate the sum of two generalized items with an intersection: DP [u] [J] = max (DP [u] [J], DP [v] [J-1]); // After updating son, then assign the value to the father} // For the leaf node of the tree, we can process it into 01 pack} int main () {While (scanf ("% d ", & N, & M), (N | M) {memset (Head,-1, sizeof (head); CNT = 0; For (INT I = 1; I <= N; ++ I) {scanf ("% d", & node, W + I); addedge (node, I );} W [0] = 0; memset (DP, 0, sizeof (DP); DFS (0, m); printf ("% d \ n ", DP [0] [m]);} return 0 ;}

 

 

/*************************************** **************************************** ****/

 

 

2529 chorus longest monotonic sequence Deformation

If opt1 is used for storage increase and opt2 is used for storage decrease, the maximum size of the person I is opt1 [I] + OPT [I]-1 time n ^ 2 space N

Because each number contains itself, the initialization is all 1.

/*************************************** **************************************** ****/

2511 classic data tower problems, search from bottom up

OPT [I] [J] = num [I] [J];

For I m .. 0

For J I. 0

OPT [I] [J] + = OPT [I + 1] [J]>? OPT [I + 1] [J + 1];

 

/*************************************** **************************************** ****/

1826 triangle partitioning with formulas can be pushed

F [2] = 1; F [3] = 1;
F [I] = 4 * f [I-1]-6 * f [I-1]/(I-1 );

/*************************************** **************************************** ****/

1968 Longest Common subsequence time n ^ 2 space N ^ 2

The stage is not very obvious. Opt [I] [J] indicates the longest common subsequence with str1 length being I and str2 length being J starting from the start point.

#include <stdio.h>#include <memory.h>#include <string.h>const int maxn=200;char str1[maxn],str2[maxn];int opt[maxn][maxn];int same(int i,int j){    if(str1[i]==str2[j])     return 1;    return 0;}int main (){    int i,j;    while (scanf("%s%s",str1,str2)!=EOF)    {        memset (opt,0,sizeof(opt));        opt[1][1]=same(0,0);        int len1=strlen(str1);        int len2=strlen(str2);        int ans=0;        for (i=1 ; i<=len1 ; i++)         for (j=1 ; j<=len2 ; j++)         {             opt[i][j]=opt[i][j-1];             if(opt[i][j]<opt[i-1][j])opt[i][j]=opt[i-1][j];              if(opt[i][j]<(opt[i-1][j-1]+same(i-1,j-1)))               opt[i][j]=(opt[i-1][j-1]+same(i-1,j-1));         }        printf("%d\n",opt[len1][len2]);    }    return 0;}

 

/*************************************** **************************************** ****/

1995 maximum sub-segment and time n

Store the sum with the sum prefix. When sum is less than 0, it is cleared. At the same time, the maximum value of sum and the maximum value of the value must be recorded during the scanning process, because all values may be negative, however, the maximum value of sum is 0 by default.

2058. There are more conditional record intervals or something.

/*************************************** **************************************** ****/

 

 

 

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.