zoukankan      html  css  js  c++  java
  • UVA 10564 计数DP

    也是经典的计数DP题,想练练手,故意不写记忆化搜索,改成递推,还是成功了嘞。。。不过很遗憾一开始WA了,原来是因为判断结束条件写个 n或s为0,应该要一起为0的,搞的我以为自己递推写挫了,又改了一下,其实递推没问题,就是写出来不好看

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #define LL long long
    using namespace std;
    const int N=80;
    LL dp[N][N][1000];
    int mat[N][N];
    int p[N][N][1000];
    int n,s;
    void print(int d,int loc,int s)
    {
        if (d==2*n-1) return;
        int val=mat[d][loc];
        if (d<n){
          if (dp[d+1][loc-1][s-val] && loc>1){
            putchar('L');
            print(d+1,loc-1,s-val);
            return;
          }
          else {
              putchar('R');
              print(d+1,loc,s-val);
          }
        }
        else{
            if (dp[d+1][loc][s-val]){
                putchar('L');
                print(d+1,loc,s-mat[d][loc]);
                return;
            }
            else{
                putchar('R');
                print(d+1,loc+1,s-mat[d][loc]);
            }
        }
    }
    int main()
    {
        while (scanf("%d%d",&n,&s)!=EOF)
        {
            if (n==0 &&s==0) break;
            memset(mat,0,sizeof mat);
            for (int i=1;i<=2*n-1;i++){
                int k;
                if (i<=n) k=n-i+1;
                else k=i-n+1;
                for (int j=1;j<=k;j++){
                    scanf("%d",&mat[i][j]);
                }
            }
            memset(dp,0,sizeof dp);
            for (int i=1;i<=n;i++){
                dp[2*n-1][i][mat[2*n-1][i]]=1;
            }
            for (int i=2*n-2;i>=1;i--)
            {
                int k;
                if (i>=n) k=i-n+1;
                else k=n-i+1;
                if (i>=n){
                    for (int j=1;j<=k;j++){
                        for (int q=mat[i][j];q<=s;q++){
                            dp[i][j][q]+=dp[i+1][j][q-mat[i][j]];
                            dp[i][j][q]+=dp[i+1][j+1][q-mat[i][j]];
                        }
                    }
                }
                else{
                    for (int j=1;j<=k;j++){
                        for (int q=mat[i][j];q<=s;q++){
                            dp[i][j][q]+=dp[i+1][j][q-mat[i][j]];
                            dp[i][j][q]+=dp[i+1][j-1][q-mat[i][j]];
                        }
                    }
                }
            }
            LL ans=0;
            int loc=-1;
            for (int i=1;i<=n;i++){
                ans+=dp[1][i][s];
                if (dp[1][i][s] && loc==-1){
                    loc=i;
                }
            }
            printf("%lld
    ",ans);
            if (ans>0)
            {
                printf("%d ",loc-1);
                print(1,loc,s);
            }
            puts("");
        }
        return 0;
    }
    

      

  • 相关阅读:
    Python3 基本数据类型
    C语言使用以及其它语言经常出的错误
    Deleting array elements in JavaScript
    postgresql 导出建表语句的方法-类似describe table
    什么是分布式系统(通俗易懂的说法)
    什么是分布式系统,分布式系统例子?
    【转】Linux cp -a用法
    [转]Go基础之锁的初识
    fabricJs使用系列(一)
    chrome 的onbeforeunload事件没有触发
  • 原文地址:https://www.cnblogs.com/kkrisen/p/3902760.html
Copyright © 2011-2022 走看看