解题报告 『[USACO08NOV]Mixed Up Cows(状压动规)』

原题地址

观察数据范围:4 ≤ N ≤ 16

很明显,这是一道状压DP。

 

定义:dp[i][j]表示队尾为奶牛i,当前含奶牛的状态为j,共有多少组符合条件的队伍。

 

代码实现如下:

#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define rep(i, a, b) for (register int i = (a); i <= (b); i++)

const int maxn = 20;

int n, m;
LL ans = 0;
int a[maxn];
LL dp[maxn][2 << maxn];

int read(){
    int x = 0, flag = 0;
    char ch = ' ';
    while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar();
    if (ch == '-') {
        flag = 1;
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9') {
        x = (x << 1) + (x << 3) + ch - '0';
        ch = getchar();
    }
    return flag ? -x : x;
}

void write(LL x) {
    if (x < 0) {
        putchar('-');
        x = -x;
    }
    if (x > 9) write(x / 10);
    putchar(x % 10 + '0');
}

int main() {
    n = read(), m = read();
    rep(i, 1, n) a[i] = read();
    rep(i, 1, n) dp[i][1 << (i - 1)] = 1;
    rep(i, 1, (1 << n) - 1) {
        rep(j, 1, n) {
            if (i & (1 << (j - 1)))
            rep(k, 1, n) {
                if (!(i & (1 << (k - 1))) && abs(a[j] - a[k]) > m) dp[k][i | (1 << (k - 1))] += dp[j][i];
            }
        }
    }
    rep(i, 1, n) ans += dp[i][(1 << n) - 1];
    write(ans);
    return 0;
}
View Code

转载于:https://www.cnblogs.com/Kirisame-Marisa/p/10382159.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值