zoukankan      html  css  js  c++  java
  • 【BZOJ4565】 [Haoi2016]字符合并

    Description

    有一个长度为 n 的 01 串,你可以每次将相邻的 k 个字符合并,得到一个新的字符并获得一定分数。得到的新字
    符和分数由这 k 个字符确定。你需要求出你能获得的最大分数。

    Input

    第一行两个整数n,k。接下来一行长度为n的01串,表示初始串。接下来2k行,每行一个字符ci和一个整数wi,ci
    表示长度为k的01串连成二进制后按从小到大顺序得到的第i种合并方案得到的新字符,wi表示对应的第i种方案对应
    获得的分数。1<=n<=300,0<=ci<=1,wi>=1,k<=8

    Output

    输出一个整数表示答案

    Sample Input

    3 2
    101
    1 10
    1 10
    0 20
    1 30

    Sample Output

    40
    //第3行到第6行表示长度为2的4种01串合并方案。00->1,得10分,01->1得10分,10->0得20分,11->1得30分。

    Solution

    考虑区间DP。记f[i][j][k]表示把[i,j]区间内的数都合并成k的最大价值。因为k<=8,所以可以用一个二进制数来把状态压起来。

    考虑如何转移,注意到只有长度是k的线段才可以合并成一个,所以,长度在模k-1意义下余1的线段一定只会被合并成1个数,所以,我们每次转移的时候只要枚举长度在模k-1意义下为1的前(后)缀,然后再枚举2^k的状态即可。时间复杂度O(n^3*2^k/k^2)。

    Code

     1 #include <cstdio>
     2 #include <cstring>
     3 #include <algorithm>
     4 #include <cmath>
     5 
     6 #ifdef WIN32
     7     #define LL "%I64d"
     8 #else
     9     #define LL "%lld"
    10 #endif
    11 
    12 #ifdef CT
    13     #define debug(...) printf(__VA_ARGS__)
    14     #define setfile() 
    15 #else
    16     #define debug(...)
    17     #define filename ""
    18     #define setfile() freopen(filename".in", "r", stdin); freopen(filename".out", "w", stdout)
    19 #endif
    20 
    21 #define R register
    22 #define getc() (S == T && (T = (S = B) + fread(B, 1, 1 << 15, stdin), S == T) ? EOF : *S++)
    23 #define dmax(_a, _b) ((_a) > (_b) ? (_a) : (_b))
    24 #define dmin(_a, _b) ((_a) < (_b) ? (_a) : (_b))
    25 #define cmax(_a, _b) (_a < (_b) ? _a = (_b) : 0)
    26 #define cmin(_a, _b) (_a > (_b) ? _a = (_b) : 0)
    27 #define cabs(_x) ((_x) < 0 ? (- (_x)) : (_x))
    28 char B[1 << 15], *S = B, *T = B;
    29 inline int F()
    30 {
    31     R char ch; R int cnt = 0; R bool minus = 0;
    32     while (ch = getc(), (ch < '0' || ch > '9') && ch != '-') ;
    33     ch == '-' ? minus = 1 : cnt = ch - '0';
    34     while (ch = getc(), ch >= '0' && ch <= '9') cnt = cnt * 10 + ch - '0';
    35     return minus ? -cnt : cnt;
    36 }
    37 #define maxn 666
    38 int str[maxn], to[maxn];
    39 long long f[310][310][256], val[maxn];
    40 int main()
    41 {
    42 //    setfile();
    43     R int n = F(), k = F();
    44     for (R int i = 1; i <= n; ++i)
    45     {
    46         R char ch = getc();
    47         while (ch < '0' || ch > '1') ch = getc();
    48         str[i] = ch - '0';
    49     }
    50     for (R int i = 0; i < 1 << k; ++i)
    51     {
    52         to[i] = F();
    53         val[i] = F();
    54     }
    55     memset(f, -63, sizeof (f)); R long long inf = f[0][0][0];
    56     for (R int i = 1; i <= n; ++i) f[i][i][str[i]] = 0;
    57     for (R int len = 2; len <= n; ++len)
    58     {
    59         R int l = len - 1; while (l >= k) l -= k - 1;
    60         for (R int i = 1, j = len; j <= n; ++i, ++j)
    61         {
    62             R long long *tmp = f[i][j];
    63             for (R int mid = j; mid > i; mid -= k - 1)
    64                 for (R int s = 0; s < (1 << l); ++s)
    65                 {
    66                     cmax(tmp[s << 1], f[i][mid - 1][s] + f[mid][j][0]);
    67                     cmax(tmp[s << 1 | 1], f[i][mid - 1][s] + f[mid][j][1]);
    68                 }
    69             if (l == k - 1)
    70             {
    71                 R long long g[2]; memset(g, -63, sizeof (g));
    72                 for (R int s = 0; s < (1 << k); ++s)
    73                     cmax(g[to[s]], tmp[s] + val[s]);
    74                 tmp[0] = g[0]; tmp[1] = g[1];
    75             }
    76 //            if (i == 1 && j == 2) printf("%lld
    ", f[i][j][0] );
    77         }
    78     }
    79 /*    for (R int i = 1; i <= n; ++i)
    80         for (R int j = i; j <= n; ++j)
    81             for (R int s = 0; s < 1 << k; ++s)
    82                 printf("f[%d][%d][%d] = %lld
    ", i, j, s, f[i][j][s] );*/
    83     R long long ans = 0;
    84     for (R int i = 0; i < (1 << k); ++i)
    85         cmax(ans, f[1][n][i]);
    86     printf("%lld
    ", ans );
    87     return 0;
    88 }
    89 /*
    90 3 2
    91 101
    92 1 10
    93 1 10
    94 0 20
    95 1 30
    96 */
  • 相关阅读:
    【数据库学习笔记】什么是DBO?
    【计算机网络学习笔记】端口号
    【计算机网络学习笔记】什么是cookie以及cookie劫持的基本概念
    【python学习笔记】pthon3.x中的文件读写操作
    【Java学习笔记】JFrame类的pack()与validate()方法的区别(待补充)
    【python学习笔记】python中的模块,类,函数,对象,实例(与JAVA的比较)
    virtual hust 2013.6.23 数学杂题基础题目 M
    virtual hust 2013.6.23 数学杂题基础题目 F
    virtual hust 2013.6.23 数学杂题基础题目 E
    virtual hust 2013.6.23 数学杂题基础题目 D
  • 原文地址:https://www.cnblogs.com/cocottt/p/6685127.html
Copyright © 2011-2022 走看看