HDU 4272(LianLianKan 连连看?消消乐?误)状压dp

题面链接

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

题面

在这里插入图片描述

题意

一个栈,每次可以选择和栈顶一样的数字,并且和栈顶距离小于 6 6 ,然后同时消去他们,问能不能把所有的数消去。

思路

每个数字最远能消去和他相距 9 9 的数,因为至多中间 4 4 个可以被他上面的消去。然后还要判断栈顶有没有被消去,所以开 10 10 d p dp d p [ i ] [ j ] dp[i][j] 表示第 i i 个栈顶状态为 j j 是否存在。那么直接状压 D P DP ,假如被消去了就 d p [ i + 1 ] [ j > > 1 ] = 1 dp[i + 1][j >> 1] = 1
!注意给定的是正确的进栈顺序。

参考代码

/* CF was purple years ago!
 * Thanks cf-tool!
 * Author: nuoyanli
 * Time: 2020-02-29 21:05:05
 **/

#include <bits/stdc++.h>
using namespace std;
#define IOS ios::sync_with_stdio(false), cin.tie(0)
#define endl '\n'
#define PB push_back
#define FI first
#define SE second
#define m_p(a, b) make_pair(a, b)
#define lson l, mid, root << 1
#define rson mid + 1, r, root << 1 | 1
#define T(t) while (t--)
#define inf 0x3f3f3f3f
typedef long long LL;
const double pi = acos(-1.0);
const double eps = 1e-9;
const int N = 2e6 + 10;
const int M = 1e5 + 10;
const int mod = 1e9 + 7;
const int st = (1 << 10);
int dp[st][st], a[st]; // dp[i][j]表示第i个栈顶状态为j是否存在
void solve() {
  IOS;
  int n;
  while (cin >> n) {
    for (int i = n; i >= 1; i--) {
      cin >> a[i];
    }
    if (n & 1) { //显然奇数必不可消完
      cout << 0 << endl;
    } else {
      int limt = 0;
      fill(dp[0], dp[0] + st * st, 0); // fill 二维数组初始化
      dp[1][0] = 1;
      for (int i = 1; i <= n; i++) {
        limt = min(10, n - i + 1); //控制上限可以省时间23333
        for (int j = 0; j < (1 << limt); j++) {
          if (!dp[i][j]) { //如果状态不存在
            continue;
          }
          if (j & 1) { //消去了
            dp[i + 1][j >> 1] = 1;
          } else {
            int num = 0; //至多往下5位
            for (int k = 1; k < limt; k++) {
              if (!((1 << k) & j)) {
                num++;
                if (a[i] == a[i + k] && num < 6) { //满足即消去
                  dp[i + 1][(j | (1 << k)) >> 1] = 1;
                }
              }
            }
          }
        }
      }
      cout << dp[n][1] << endl;
    }
  }
}
signed main() {
  solve();
  return 0;
}
发布了322 篇原创文章 · 获赞 247 · 访问量 19万+

猜你喜欢

转载自blog.csdn.net/nuoyanli/article/details/104585439
今日推荐