zoukankan      html  css  js  c++  java
  • DP录 (更新)

    补补弱项 全面发展。。

    从最基础来

    sdut1299最长上升子序

     1 #include <iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<algorithm>
     5 #include<stdlib.h>
     6 using namespace std;
     7 int dp[1100];
     8 int a[1100];
     9 int main()
    10 {
    11     int i,j,k,n;
    12     cin>>n;
    13     for(i =1; i <= n ; i++)
    14     {
    15         cin>>a[i];
    16         dp[i] = 1;
    17     }
    18     for(i = 1; i <= n ;i++)
    19     for(j = 1 ; j < i ; j++)
    20     if(a[i]>a[j])
    21     {
    22         dp[i] = max(dp[j]+1,dp[i]);
    23     }
    24     int maxz = 0;
    25     for(i = 1; i <= n ;i++)
    26     maxz = max(dp[i],maxz);
    27     cout<<maxz<<endl;
    28     return 0;
    29 }
    30  
    31 
    32 
    33 
    34 /**************************************
    35     Problem id    : SDUT OJ 1299 
    36     User name    : 尚雨 
    37     Result        : Accepted 
    38     Take Memory    : 476K 
    39     Take Time    : 0MS 
    40     Submit Time    : 2013-08-05 09:26:48  
    41 **************************************/
    View Code

    hdu2084数塔

     1 #include <iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<algorithm>
     5 #include<stdlib.h>
     6 using namespace std;
     7 int dp[110][110];
     8 int a[110][110];
     9 int main()
    10 {
    11     int i,j,k,n,t;
    12     cin>>t;
    13     while(t--)
    14     {
    15         cin>>n;
    16         memset(dp,0,sizeof(dp));
    17         for(i = 1; i <= n ;i++)
    18         for(j = 1; j <= i ;j++)
    19         cin>>a[i][j];
    20         for(j = 1; j <= n ; j++)
    21         dp[n][j] = a[n][j];
    22         for(i = n-1 ; i >= 1 ;i--)
    23         for(j = 1; j <= i ; j++)
    24         {
    25             dp[i][j]=a[i][j]+max(dp[i+1][j],dp[i+1][j+1]);
    26         }
    27         cout<<dp[1][1]<<endl;
    28     }
    29     return 0;
    30 }
    View Code

    hdu1159最长公共子序列

     1 #include <iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<algorithm>
     5 #include<stdlib.h>
     6 using namespace std;
     7 int dp[1100][1100];
     8 char s1[1100],s2[1100];
     9 int main()
    10 {
    11     int i,j,k1,k2;
    12     while(cin>>s1)
    13     {
    14         k1 = strlen(s1);
    15         cin>>s2;
    16         k2 = strlen(s2);
    17         memset(dp,0,sizeof(dp));
    18         for(i = 1 ; i <= k1 ; i++)
    19             for(j = 1 ; j <= k2 ; j++)
    20             {
    21                 if(s1[i-1]==s2[j-1])
    22                 dp[i][j] = dp[i-1][j-1]+1;
    23                 else
    24                 dp[i][j] = max(dp[i-1][j],dp[i][j-1]);
    25             }
    26         cout<<dp[k1][k2]<<endl;
    27     }
    28     return 0;
    29 }
    View Code

    hdu1003最大子段和

     1 #include <iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<algorithm>
     5 #include<stdlib.h>
     6 using namespace std;
     7 #define INF 0xfffffff
     8 int dp[110000];
     9 int a[100010];
    10 int main()
    11 {
    12     int i,j,n,t,kk=0;
    13     cin>>t;
    14     while(t--)
    15     {
    16         cin>>n;kk++;
    17         for(i = 1; i <= n ;i++)
    18         cin>>a[i];
    19         int maxz = -INF,tmaxz=0;
    20         int x=1,y,tx=1;
    21         for(i =  1; i <= n ;i++)
    22         {
    23             tmaxz+=a[i];
    24             if(tmaxz>maxz)
    25             {
    26                 maxz = tmaxz;
    27                 x = tx;
    28                 y = i;
    29             }
    30             if(tmaxz<0)
    31             {
    32                 tmaxz=0;
    33                 tx = i+1;
    34             }
    35         }
    36         printf("Case %d:
    ",kk);
    37         cout<<maxz<<" "<<x<<" "<<y<<endl;
    38         if(t!=0)
    39         puts("");
    40     }
    41     return 0;
    42 }
    View Code

     sdut1225http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=1225

     1 #include <iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<algorithm>
     5 #include<stdlib.h>
     6 using namespace std;
     7 char s1[1050],s2[1050];
     8 int dp[1050][1050];
     9 int main()
    10 {
    11     int i,j,k1,k2;
    12     while(cin>>s1)
    13     {
    14         cin>>s2;
    15         memset(dp,0,sizeof(dp));
    16         k1 = strlen(s1);
    17         k2 = strlen(s2);
    18         for(i = 0 ; i <= k1 ; i++)
    19         dp[i][0] = i;
    20         for(i = 0 ; i <= k2 ; i++)
    21         dp[0][i] = i;
    22         for(i = 1 ; i <= k1 ; i++)
    23             for(j = 1 ; j <= k2 ; j++)
    24             {
    25                 if(s1[i-1]==s2[j-1])
    26                 dp[i][j] = dp[i-1][j-1];
    27                 else
    28                 dp[i][j] = dp[i-1][j-1]+1;
    29                 dp[i][j] = min(dp[i][j],min(dp[i-1][j]+1,dp[i][j-1]+1));
    30             }
    31         cout<<dp[k1][k2]<<endl;
    32     }
    33     return 0;
    34 }
    View Code

     hdu1058 预处理出所有的数 预处理的技巧挺好

     1 #include <iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<stdlib.h>
     5 #include<algorithm>
     6 using namespace std;
     7 int dp[6010];
     8 void init()
     9 {
    10     int i,j,a=2,b=3,c=5,d=7,ii,jj,g,o;
    11     ii=jj=g=o=1;
    12     dp[1] = 1;
    13     for(i = 2; i <= 5842 ;i++)
    14     {
    15         int x = dp[ii]*a;
    16         int y = dp[jj]*b;
    17         int z = dp[g]*c;
    18         int w = dp[o]*d;
    19         dp[i] = min(min(x,y),min(z,w));
    20         if(dp[i]==x)
    21         ii++;
    22         if(dp[i]==y)
    23         jj++;
    24         if(dp[i]==z)
    25         g++;
    26         if(dp[i]==w)
    27         o++;
    28     }
    29 }
    30 int main()
    31 {
    32     int i,j,k,n;
    33     init();
    34     while(cin>>n)
    35     {
    36         if(n==0)
    37         break;
    38         if(n%10==1&&n%100!=11)
    39         printf("The %dst humble number is %d.
    ",n,dp[n]);
    40         else if(n%10==2&&n%100!=12)
    41         printf("The %dnd humble number is %d.
    ",n,dp[n]);
    42         else if(n%10==3&&n%100!=13)
    43         printf("The %drd humble number is %d.
    ",n,dp[n]);
    44         else
    45         printf("The %dth humble number is %d.
    ",n,dp[n]);
    46     }
    47     return 0;
    48 }
    View Code

    hdu2571 类似数塔

     1 #include <iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<algorithm>
     5 #define INF 0xfffffff
     6 using namespace std;
     7 int dp[30][1010];
     8 int v[30][1010];
     9 int main()
    10 {
    11     int i,j,n,m,t,g;
    12     cin>>t;
    13     while(t--)
    14     {
    15         cin>>n>>m;
    16         for(i = 1; i <= n ;i++)
    17             for(j = 1; j <= m ; j++)
    18             {
    19                cin>>v[i][j];
    20                dp[i][j] = -INF;
    21             }
    22         dp[1][1] = v[1][1];
    23         for(i = 1;i <= n ;i++)
    24             for(j = 1; j <= m ; j++)
    25             {
    26                 if(i<n)
    27                 dp[i+1][j] = max(dp[i][j]+v[i+1][j],dp[i+1][j]);
    28                 if(j<m)
    29                 dp[i][j+1] = max(dp[i][j]+v[i][j+1],dp[i][j+1]);
    30                 for(g = 2;;g++)
    31                 {
    32                     if(g*j>m)
    33                     break;
    34                     dp[i][g*j] = max(dp[i][j]+v[i][g*j],dp[i][g*j]);
    35                 }
    36             }
    37         cout<<dp[n][m]<<endl;
    38     }
    39     return 0;
    40 }
    View Code

     嵌套矩形 类似最长上升子序 因没排序WA了几次

     1 #include <iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<algorithm>
     5 #include<stdlib.h>
     6 using namespace std;
     7 struct node
     8 {
     9     int a,b;
    10 }q[1010];
    11 int dp[1010];
    12 bool cmp(node a,node b)
    13 {
    14     if(a.a==b.a)
    15     return a.b<b.b;
    16     return a.a<b.a;
    17 }
    18 int main()
    19 {
    20     int i,j,t,n,tt;
    21     cin>>t;
    22     while(t--)
    23     {
    24         cin>>n;
    25         memset(dp,0,sizeof(dp));
    26         for(i = 1; i <= n ;i++)
    27         {
    28             cin>>q[i].a>>q[i].b;
    29             if(q[i].a>q[i].b)
    30             {
    31                 tt = q[i].a;
    32                 q[i].a = q[i].b;
    33                 q[i].b = tt;
    34             }
    35             dp[i] = 1;
    36         }
    37         sort(q+1,q+n+1,cmp);
    38         for(i = 1; i <= n ; i++)
    39             for(j = 1; j  < i ; j++)
    40             {
    41                 if(((q[i].a>q[j].a)&&(q[i].b>q[j].b))||(q[i].b>q[j].a&&q[i].a>q[j].b))
    42                 dp[i] = max(dp[i],dp[j]+1);
    43             }
    44         int maxz=0;
    45         for(i = 1; i <= n ;i++)
    46         maxz = max(maxz,dp[i]);
    47         cout<<maxz<<endl;
    48     }
    49     return 0;
    50 }
    View Code

        Vijos 1493 传纸条 多进程DP

    四维

     1 #include <iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<algorithm>
     5 #include<stdlib.h>
     6 using namespace std;
     7 int dp[51][51][51][51];
     8 int a[51][51];
     9 int main()
    10 {
    11     int i,j,n,m,g,o;
    12     while(cin>>n>>m)
    13     {
    14         memset(dp,0,sizeof(dp));
    15         memset(a,0,sizeof(a));
    16         for(i = 1; i <= n ; i++)
    17             for(j = 1; j <= m ; j++)
    18             scanf("%d",&a[i][j]);
    19         for(i = 1; i <= n ;i++)
    20             for(j = 1 ; j <= m ; j++)
    21                 for(g = 1; g <= n ; g++)
    22                     for(o = 1 ;o <= m ;o++)
    23                     {
    24                         dp[i][j][g][o] = max(max(dp[i-1][j][g-1][o],dp[i-1][j][g][o-1]),max(dp[i][j-1][g-1][o],dp[i][j-1][g][o-1]));
    25                         if(i==g&&j==o)
    26                         dp[i][j][g][o]+=a[i][j];
    27                         else
    28                         dp[i][j][g][o]+=a[i][j]+a[g][o];
    29                     }
    30         printf("%d
    ",dp[n][m][n][m]);
    31     }
    32     return 0;
    33 }
    View Code

    因为它只能从两个方向走过来 很巧妙的可以转换为3维的 

     1 #include <iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<algorithm>
     5 #include<stdlib.h>
     6 using namespace std;
     7 int dp[101][51][51];
     8 int a[51][51],d[101][51];
     9 int main()
    10 {
    11     int i,j,n,m,g,o,x,y,z,p;
    12     while(cin>>n>>m)
    13     {
    14         memset(dp,0,sizeof(dp));
    15         memset(d,0,sizeof(d));
    16         for(i = 1; i <= n ; i++)
    17             for(j = 1; j <= m ; j++)
    18             {
    19                 scanf("%d",&a[i][j]);
    20                 if(i+j>n)
    21                 d[i+j-1][n-i+1] = a[i][j];
    22                 else
    23                 d[i+j-1][j] = a[i][j];
    24             }
    25         for(i = 1; i < n+m ; i++)
    26             for(j = 1 ;j <= m ; j++)
    27                 for(g = 1; g <= m ;g++)
    28                 {
    29                     if(i <= n)
    30                     {
    31                         x = dp[i-1][j][g-1];
    32                         y = dp[i-1][j-1][g-1];
    33                         z = dp[i-1][j][g];
    34                         p = dp[i-1][j-1][g];
    35                     }
    36                     else
    37                     {
    38                         x = dp[i-1][j][g];
    39                         y = dp[i-1][j+1][g+1];
    40                         z = dp[i-1][j+1][g];
    41                         p = dp[i-1][j][g+1];
    42                     }
    43                     dp[i][j][g] = max(max(x,y),max(z,p));
    44                     if(j==g)
    45                     dp[i][j][g]+=d[i][j];
    46                     else
    47                     dp[i][j][g]+=d[i][j]+d[i][g];
    48                 }
    49         cout<<dp[n+m-1][1][1]<<endl;
    50 
    51     }
    52     return 0;
    53 }
    View Code

     hdu1059http://acm.hdu.edu.cn/showproblem.php?pid=1059

    多重背包 优化一下 时间卡的很紧

     1 #include <iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<cstring>
     5 #include<algorithm>
     6 #include<stdlib.h>
     7 using namespace std;
     8 int a[10],b[20010],d[33];
     9 int dp[1000010];
    10 int main()
    11 {
    12     int i,j,k,s=0,g,n=0,kk=0;
    13     while(scanf("%d",&a[1])!=EOF)
    14     {
    15         s=0;
    16         s+=a[1];g=0;kk++;
    17         memset(b,0,sizeof(b));
    18         for(i = 2; i <= 6 ; i++)
    19         {
    20             scanf("%d",&a[i]);
    21             s+=a[i]*i;
    22             n+=a[i];
    23         }
    24         if(s==0) break;
    25         printf("Collection #%d:
    ",kk);
    26         if(s%2!=0)
    27         {
    28             printf("Can't be divided.
    
    ");
    29             continue;
    30         }
    31         else
    32         s = s/2;
    33         d[1] = 1;
    34         for(i = 0 ; i <= s ; i++)
    35         dp[i] = 0;
    36         for(i = 2; i  < 30 ; i++)
    37         d[i] = d[i-1]<<1;
    38         j=0;
    39         for(i = 1; i <= 6 ; i++)
    40         {
    41             k = 1;
    42             while(a[i]>d[k])
    43             {
    44                 a[i]-=d[k];
    45                 b[++j] = d[k]*i;
    46                 k++;
    47             }
    48             if(a[i])
    49             {
    50                 b[++j] = a[i]*i;
    51             }
    52         }
    53         k = j;
    54         for(i = 1; i <= k ; i++)
    55         for(j = s ; j >= b[i] ; j--)
    56         {
    57             dp[j] = max(dp[j-b[i]]+b[i],dp[j]);
    58         }
    59         if(dp[s]==s)
    60         printf("Can be divided.
    ");
    61         else
    62         printf("Can't be divided.
    ");
    63         puts("");
    64     }
    65     return 0;
    66 }
    View Code

     hdu1176免费馅饼 http://acm.hdu.edu.cn/showproblem.php?pid=1176

    将每秒的状态合起来类似数塔 倒推回去就行

     1 #include <iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<algorithm>
     5 #include<stdlib.h>
     6 using namespace std;
     7 int dp[100010][15];
     8 int main()
     9 {
    10     int i,j,k,n,x,t;
    11     while(scanf("%d",&n)&&n)
    12     {
    13         memset(dp,0,sizeof(dp));
    14         int maxz=0;
    15         for(i = 1 ;i <= n ; i++)
    16         {
    17             scanf("%d%d",&x,&t);
    18             dp[t][x+1]++;
    19             maxz = max(maxz,t);
    20         }
    21         for(i = maxz-1 ; i >= 0; i--)
    22            for(j = 1 ; j <= 11 ; j++)
    23            {
    24                dp[i][j]+=max(max(dp[i+1][j-1],dp[i+1][j+1]),dp[i+1][j]);
    25            }
    26         cout<<dp[0][6]<<endl;
    27     }
    28     return 0;
    29 }
    View Code

     hdu1069http://acm.hdu.edu.cn/showproblem.php?pid=1069 每个矩形算六个 排序后类似最长上升子序

     1 #include <iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<algorithm>
     5 #include<stdlib.h>
     6 #define N 40
     7 using namespace std;
     8 struct node
     9 {
    10     int x,y,z;
    11 }rc[N*6];
    12 int g,dp[310];
    13 void init(int a,int b,int c)
    14 {
    15     rc[++g].x = a;
    16     rc[g].y = b;
    17     rc[g].z = c;
    18     rc[++g].x = b;
    19     rc[g].y = a;
    20     rc[g].z = c;
    21     rc[++g].x = a;
    22     rc[g].y = c;
    23     rc[g].z = b;
    24     rc[++g].x = b;
    25     rc[g].y = c;
    26     rc[g].z = a;
    27     rc[++g].x = c;
    28     rc[g].y = b;
    29     rc[g].z = a;
    30     rc[++g].x = c;
    31     rc[g].y = a;
    32     rc[g].z = b;
    33 }
    34 bool cmp(node a,node b)
    35 {
    36     if(a.x==b.x)
    37     return a.y>b.y;
    38     return a.x>b.x;
    39 }
    40 int main()
    41 {
    42     int i,j,n,a,b,c,kk=0;
    43     while(cin>>n)
    44     {
    45         g=0;kk++;
    46         if(n==0) break;
    47         memset(dp,0,sizeof(dp));
    48         for(i = 1;i <= n ; i++)
    49         {
    50             scanf("%d%d%d",&a,&b,&c);
    51             init(a,b,c);
    52         }
    53         sort(rc+1,rc+g+1,cmp);
    54         for(i = 1; i <= g ; i++)
    55         dp[i] = rc[i].z;
    56         for(i = 1; i <= g ; i++)
    57             for(j = 1; j < i ; j++)
    58             {
    59                 if(rc[j].x>rc[i].x&&rc[j].y>rc[i].y)
    60                 dp[i] = max(dp[i],dp[j]+rc[i].z);
    61             }
    62         int maxz=0;
    63         printf("Case %d: maximum height = ",kk);
    64         for(i = 1; i <= g ; i++)
    65         maxz = max(dp[i],maxz);
    66         cout<<maxz<<endl;
    67     }
    68     return 0;
    69 }
    View Code

     hdu1081http://acm.hdu.edu.cn/showproblem.php?pid=1081 记录从i到j行g列的和 转换为一维的最大子段和 

     1 #include <iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<stdlib.h>
     5 #include<algorithm>
     6 using namespace std;
     7 #define INF 0xfffffff
     8 int a[110][110],s[105][105][105],c[105][105][105];
     9 int main()
    10 {
    11     int i,j,k,n,g;
    12     while(scanf("%d",&n)!=EOF)
    13     {
    14         memset(s,0,sizeof(s));
    15         memset(c,0,sizeof(c));
    16         for(i = 1; i <= n ; i++)
    17             for(j = 1 ;j <= n ;j++)
    18             scanf("%d",&a[i][j]);
    19         for(i = 1; i <= n ; i++)
    20             for(j = 1 ;j <= n ; j++)
    21             {
    22                 s[j][j][i] = a[j][i];
    23                 for(g = j+1; g <= n ; g++)
    24                 s[j][g][i]=a[g][i]+s[j][g-1][i];
    25             }
    26         int maxz = -INF,tmaxz=0;
    27         for(i = 1; i <= n ; i++)
    28             for(j = 1; j <= n ; j++)
    29             {
    30                 tmaxz=0;
    31                 for(g = 1 ;g <= n ;g++)
    32                 {
    33                     tmaxz+=s[i][j][g];
    34                     if(tmaxz>maxz)
    35                     maxz=tmaxz;
    36                     if(tmaxz<0)
    37                     tmaxz = 0;
    38                 }
    39             }
    40           cout<<maxz<<endl;
    41 
    42     }
    43     return 0;
    44 }
    View Code

     hdu1087 水题

     1 #include <iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<algorithm>
     5 #include<stdlib.h>
     6 using namespace std;
     7 #define LL long long
     8 int a[1010];
     9 LL dp[1010];
    10 int main()
    11 {
    12     int i,j,k,n;
    13     while(cin>>n)
    14     {
    15         if(!n) break;
    16         memset(dp,0,sizeof(dp));
    17         for(i = 1; i <= n ; i++)
    18         {
    19             cin>>a[i];
    20             dp[i] = a[i];
    21         }
    22         for(i = 1 ;i <= n ; i++)
    23             for(j = 1; j < i ; j++)
    24             if(a[i]>a[j])
    25             dp[i] = max(dp[i],dp[j]+a[i]);
    26         LL maxz=dp[1];
    27         for(i = 2 ;i <= n ;i++)
    28         if(dp[i]>maxz)
    29         maxz = dp[i];
    30         cout<<maxz<<endl;
    31     }
    32     return 0;
    33 }
    View Code

    hdu1158http://acm.hdu.edu.cn/showproblem.php?pid=1158二维DP 三重循环 以前居然做过 好扯 居然一点印象没有 好认真的推了半张纸。。

     1 #include <iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<algorithm>
     5 #include<stdlib.h>
     6 using namespace std;
     7 #define INF 0xfffffff
     8 int dp[20][1010],num[20];
     9 int main()
    10 {
    11     int i,j,k,n,m,a,b,c,g;
    12     while(cin>>n)
    13     {
    14         if(!n) break;
    15         cin>>a>>b>>c;
    16         memset(dp,0,sizeof(dp));
    17         memset(num,0,sizeof(num));
    18         int maxz=0,minz;
    19         for(i = 1; i <= n ;i++)
    20         {
    21             cin>>num[i];
    22             if(num[i]>maxz)
    23             maxz = num[i];
    24             if(num[i]<minz)
    25             minz = num[i];
    26         }
    27         for(i = 1;i <= n ;i++)
    28             for(j = 0 ; j <= maxz ; j++)
    29             dp[i][j] = INF;
    30         for(i = 0 ; i <= maxz ; i++)
    31         dp[1][i] = i*(a+b);
    32         for(i = 2; i <= n ; i++)
    33             for(j = num[i-1] ; j <= maxz ; j++)
    34                 for(g = num[i] ; g <= maxz ; g++)
    35                 {
    36                     if(g>j)
    37                     dp[i][g] = min(dp[i][g],dp[i-1][j]+(g-j)*a+g*b);
    38                     else if(g<j)
    39                     dp[i][g] = min(dp[i][g],dp[i-1][j]+(j-g)*c+g*b);
    40                     else
    41                     dp[i][g] = min(dp[i][g],dp[i-1][j]+g*b);
    42                 }
    43         int maxzz = INF;
    44         for(i = num[n] ;i <= maxz ;i++)
    45         if(dp[n][i]<maxzz)
    46           maxzz = dp[n][i];
    47         cout<<maxzz<<endl;
    48     }
    49     return 0;
    50 }
    View Code

     hdu1165http://acm.hdu.edu.cn/showproblem.php?pid=1165 这题确实不能算DP  我还傻乎乎的写了一递归  M比较小 直接推就好 了

     1 #include <iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<algorithm>
     5 #include<stdlib.h>
     6 using namespace std;
     7 #define LL long long
     8 #define N 1000010
     9 int n,m;
    10 LL dp[4][N+10];
    11 int main()
    12 {
    13     int i,j;
    14     for(i = 0 ; i <= N ; i++)
    15     dp[0][i] = i+1;
    16     dp[1][0] = 2;
    17     dp[2][0] = 3;
    18     dp[3][0] = 5;
    19     for(i = 1 ; i <= N ; i++)
    20     dp[3][i] = dp[1][0]*dp[3][i-1]+3;
    21     while(scanf("%d%d",&m,&n)!=EOF)
    22     {
    23         if(m==1)
    24         dp[m][n] = dp[1][0]+n;
    25         else if(m==2)
    26         dp[m][n] = dp[1][0]*n+dp[2][0];
    27         cout<<dp[m][n]<<endl;
    28     }
    29     return 0;
    30 }
    View Code

     hdu1506 http://acm.hdu.edu.cn/status.php 题不错 记录i左边及右边第一个比它小的位置 用来求以它为高的最大面积 求位置的时候 dp一下下吧 

    注意。。杭电上long long会无限WA。。用——int64

     1 #include <iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<algorithm>
     5 #include<stdlib.h>
     6 using namespace std;
     7 #define N 100100
     8 __int64 a[N],dp1[N],dp2[N];
     9 __int64 s;
    10 int main()
    11 {
    12     int i,n;
    13     while(cin>>n)
    14     {
    15         if(!n) break;
    16         for(i = 1 ;i <= n ;i++)
    17         scanf("%I64d",&a[i]);
    18         dp1[1] = 0;
    19         a[0] = -1;
    20         for(i = 2; i <= n ;i++)
    21         {
    22             if(a[i]>a[i-1])
    23             dp1[i] = i-1;
    24             else
    25             {
    26                 int x = i-1;
    27                 while(x>=1&&a[x]>=a[i])
    28                 {
    29                     x = dp1[x];
    30                 }
    31                 dp1[i] = x;
    32             }
    33         }
    34         dp2[n] = n+1;
    35         a[n+1] = -1;
    36         for(i = n-1; i >= 1 ;i--)
    37         if(a[i]>a[i+1])
    38         dp2[i] = i+1;
    39         else
    40         {
    41             int x = i+1;
    42             while(x<=n&&a[x]>=a[i])
    43             {
    44                 x = dp2[x];
    45             }
    46             dp2[i] = x;
    47         }
    48         long long maxz=0;
    49         for(i = 1 ;i <= n ; i++)
    50         {
    51             s = (dp2[i]-dp1[i]-1)*a[i];
    52             maxz = max(s,maxz);
    53         }
    54         printf("%I64d
    ",maxz);
    55     }
    56     return 0;
    57 }
    View Code

     hdu1712http://acm.hdu.edu.cn/showproblem.php?pid=1712 简单二维DP

     1 #include <iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<algorithm>
     5 #include<stdlib.h>
     6 using namespace std;
     7 int dp[110][110];
     8 int a[110][110];
     9 int main()
    10 {
    11     int i,j,g,n,m;
    12     while(cin>>n>>m)
    13     {
    14         if(!n&&!m) break;
    15         memset(a,0,sizeof(a));
    16         memset(dp,0,sizeof(dp));
    17         for(i = 1;i <= n ; i++)
    18             for(j = 1 ;j <= m ; j++)
    19             scanf("%d",&a[i][j]);
    20         for(i = 1;i <= n ; i++)
    21             for(j = 1 ; j <= m ; j++)
    22             for(g = 0; g<= j ; g++)
    23             dp[i][j] = max(dp[i-1][g]+a[i][j-g],dp[i][j]);
    24         int maxz = 0;
    25         for(i = 0; i <= m ;i++)
    26         maxz = max(maxz,dp[n][i]);
    27         cout<<maxz<<endl;
    28     }
    29     return 0;
    30 }
    View Code

     hdu2372http://acm.hdu.edu.cn/showproblem.php?pid=2372 又一水DP

     1 #include <iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<algorithm>
     5 #include<stdlib.h>
     6 using namespace std;
     7 #define LL __int64
     8 LL dp[110][110];
     9 int a[110];
    10 int main()
    11 {
    12     int i,j,g,n,k;
    13     while(cin>>n>>k)
    14     {
    15         if(!n&&!k) break;
    16         memset(dp,0,sizeof(dp));
    17         for(i =1 ;i <= n ; i++)
    18         {
    19             scanf("%d",&a[i]);
    20             dp[i][1] = 1;
    21         }
    22         for(i = 1; i <= n ;i++)
    23             for(j = 1;j<=i&&j <= k ; j++)
    24             for(g = 1 ; g < i ; g++)
    25             if(a[i]>a[g])
    26             dp[i][j] += dp[g][j-1];
    27         LL maxz=0;
    28         for(i = k ; i <= n ; i++)
    29             maxz+=dp[i][k];
    30         cout<<maxz<<endl;
    31     }
    32     return 0;
    33 }
    View Code

     hdu4223http://acm.hdu.edu.cn/showproblem.php?pid=4223 这题不能算DP吧 预处理一下

     1 #include <iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<algorithm>
     5 #include<stdlib.h>
     6 #include<cmath>
     7 using namespace std;
     8 #define LL __int64
     9 #define INF 0xfffffff
    10 int s[1010];
    11 int a[1010];
    12 int main()
    13 {
    14     int i,j,k,n,kk=0,t;
    15     cin>>t;
    16     while(t--)
    17     {
    18         cin>>n;kk++;
    19         memset(s,0,sizeof(s));
    20         for(i = 1; i <= n ; i++)
    21         {
    22             scanf("%d",&a[i]);
    23             s[i] = s[i-1]+a[i];
    24         }
    25         int minz=INF;
    26         for(i = 1; i <= n ; i++)
    27             for(j = 0; j < i ; j++)
    28             {
    29                 int x = abs(s[i]-s[j]);
    30                 minz = min(minz,x);
    31             }
    32         printf("Case %d: ",kk);
    33         cout<<minz<<endl;
    34     }
    35     return 0;
    36 }
    View Code

     hdu4159http://acm.hdu.edu.cn/showproblem.php?pid=4159

    一直想推概率 结果怎么也推不出来 后来想到可以推可能的种数  因为太大WA了 又去想推概率 还是没退出 最后除了个很大的数 过了

     1 #include <iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<stdlib.h>
     5 #include<algorithm>
     6 using namespace std;
     7 #define MM 1000000000000000.0
     8 double dp[55][55];
     9 int main()
    10 {
    11     int n,s,i,j;
    12     while(cin>>n>>s)
    13     {
    14         memset(dp,0,sizeof(dp));
    15         if(s==0)
    16         {
    17             printf("0.00000
    ");
    18             continue;
    19         }
    20         dp[1][s] = 2;
    21         dp[1][s-1] = 1;
    22         for(i = 2; i <= n ;i++)
    23             for(j = 0 ; j <= s ; j++)
    24             {
    25                 dp[i][j] = dp[i-1][j]*2+dp[i-1][j+1];
    26             }
    27         double ss=0;
    28         for(i = 0; i <= s ; i++)
    29         {
    30             ss+=dp[n][i]/MM;
    31         }
    32         dp[n][0] = dp[n][0]/MM;
    33         double x = 1-1.0*dp[n][0]/ss;
    34         printf("%.5lf
    ",x*100);
    35     }
    36     return 0;
    37 }
    View Code
  • 相关阅读:
    学习两个Python不常用的语法
    使用uwsgi在centos7上部署Flask在线Web服务
    redis server学习002
    上传至gitlab—— 本地git出现fatal: The current branch develop has no upstream branch. To push the current branch and set the remote as upstream, use git push --set-upstream origin develop 的问题
    数据库脏数据——数据库中的并发操作带来的一系列问题
    error: failed to push some refs to 'git@github.com:Sirxy/flask_jwt_demo.git'
    redis server学习001
    【解决方案】麒麟v10,更新yum源
    【解决方案】github图片加载不出来
    linux c程序高cpu,排查记录
  • 原文地址:https://www.cnblogs.com/shangyu/p/3237752.html
Copyright © 2011-2022 走看看