Solution to a problem P2915 [USACO08NOV] mix Mixed Up Cows Cows

Solution to a problem P2915 [USACO08NOV] mix Mixed Up Cows Cows

Topic Link

16-20 are like the size of data compression

If you want every bit of the serial number, then press the space is definitely not enough, so each one can only be 0 or 1.1 expressed cow, 0 means no cow. Apparently selected for each location and on both sides of his cattle about, so we can define this state:

f [i] [j] i bovine indication set, wherein the last sequence number of a cow when the total number of program j. The answer from the f [(1 << n) - 1] [n] can be accumulated.

Transfer relatively miss, selected from i enumerated cow reciprocal second, sub-state as the last cow, attention boundary conditions.

Let's look at the code it, it is not hard:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define ll long long
#define N 17
using namespace std;
inline int read()
{
    int x = 0, f = 1;
    char c = getchar();
    while (c < '0' || c > '9') { if (c == '-') f = -1; c = getchar(); }
    while (c >= '0' && c <= '9') { x = x * 10 + c - '0'; c = getchar(); }
    return x * f;
}
int abs(int x) { return x > 0 ? x : -x; }
ll f[(1 << 16) + 5][18], ans;
int n, k, a[N], cnt[(1 << 16) + 5];
void init()
{
    for (int i = 1; i <= (1 << n); i++)
    {
        for (int j = 0; j < n; j++)
        {
            if (i & (1 << j)) cnt[i]++;
        }
    }
}//初始化每种集合的奶牛头数
int main()
{
    n = read(), k = read();
    for (int i = 1; i <= n; i++)
        a[i] = read();
    init();
    for (int i = 1; i < (1 << n); i++)
        for (int j = 1; j <= n; j++) //枚举这个集合的最后一头牛
            if (i & (1 << (j - 1)))
            {
                if (cnt[i] == 1) f[i][j] = 1; //边界情况
                else
                    for (int p = 1; p <= n; p++) //枚举倒数第二头牛
                        if (p != j && (i & (1 << (p - 1))) && abs(a[p] - a[j]) > k)
                            f[i][j] += f[i - (1 << (j - 1))][p];
            }
    for (int i = 1; i <= n; i++)
        ans += f[(1 << n) - 1][i];
    cout << ans;
    return 0;
}

19.09.05

Guess you like

Origin www.cnblogs.com/YuanqiQHFZ/p/11622352.html