P2915 [USACO08NOV]奶牛混合起来Mixed Up Cows

——————————————————————————————————————————————————————————

我真睿智

重叠变量名查了十多分钟的错

很好的状压题,无论是隐形的表示还有计数

————————————————————————————

#include<bits/stdc++.h>
using namespace std;
long long int n,kk,ans,a[17],f[17][1<<17],cows[17];
int main()
{
    cin>>n>>kk;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];;
        f[i][cows[i]=1<<(n-i)]=1;
    }
    for(int i=1;i<=(1<<n)-1;i++)
    for(int j=1;j<=n;j++)
    if(i&cows[j])
    for(int k=1;k<=n;k++)
    if((j!=k)&&(abs(a[k]-a[j])>kk))f[j][i]+=f[k][i^cows[j]];
    for(int i=1;i<=n;i++)ans+=f[i][(1<<n)-1];
    cout<<ans;
}

原文地址:https://www.cnblogs.com/SFWR-YOU/p/11279760.html

时间: 2024-10-29 05:15:56

P2915 [USACO08NOV]奶牛混合起来Mixed Up Cows的相关文章

洛谷 P2915 [USACO08NOV]奶牛混合起来Mixed Up Cows

P2915 [USACO08NOV]奶牛混合起来Mixed Up Cows 题目描述 Each of Farmer John's N (4 <= N <= 16) cows has a unique serial number S_i (1 <= S_i <= 25,000). The cows are so proud of it that each one now wears her number in a gangsta manner engraved in large le

题解 P2915 [USACO08NOV]奶牛混合起来Mixed Up Cows

题解 P2915 [USACO08NOV]奶牛混合起来Mixed Up Cows 题目链接 数据规模16-20的都是状压 如果要每一位都压序号的话空间肯定是不够的,所以每一位只能是0或1.1表示有这头牛,0表示没有这头牛.显然每个位置的选择和他两边的牛有关,所以我们就可以定义这样的状态: f[i][j]表示使用集合i的牛,其中最后一头牛的序号为j时的总方案数.答案从f[(1 << n) - 1][n]累加即可. 转移也比较好想,从i中枚举选出倒数第二头牛,作为子状态的最后一头牛,注意边界情况.

洛谷 P2915 【[USACO08NOV]奶牛混合起来Mixed Up Cows】

类似于n皇后的思想,只要把dfs表示放置情况的数字压缩成一个整数,就能实现记忆化搜索了. 一些有关集合的操作: {i}在集合S内:S&(1<<i)==1: 将{i}加入集合S:S=S|(1<<i): 集合S内包含了{0,1,2,...,n-2,n-1}:S==(1<<n)-1: 1 #include <cstdio> 2 #include <cstring> 3 #include <cmath> 4 using namespa

[USACO08NOV]奶牛混合起来Mixed Up Cows

题目描述 Each of Farmer John's N (4 <= N <= 16) cows has a unique serial number S_i (1 <= S_i <= 25,000). The cows are so proud of it that each one now wears her number in a gangsta manner engraved in large letters on a gold plate hung around her

[luoguP2915] [USACO08NOV]奶牛混合起来Mixed Up Cows(DP)

传送门 f[i][S] 表示当前集合为 S,最后一个数为 i 的最优解 f[i][S] += f[j][S - i] (j, i ∈ S && j != i && abs(a[i] - a[j]) > k) ——代码 1 #include <cstdio> 2 #include <iostream> 3 #define LL long long 4 5 int a[17]; 6 int n, m, k; 7 LL ans, f[17][1 &l

Luogu2917_ [USACO08NOV]奶牛混合起来Mixed Up Cows_KEY

题目传送门 看到数据范围就果断装压. 设f[i][j]表示i状态下最后一个数字为a[j]. code: #include <cstdio> using namespace std; int N,K,a[17]; long long f[1<<16][17]; inline int abs(int x){return x>0?x:-x;} int main() { scanf("%d%d",&N,&K); register int i,j,k

AC日记——[USACO06FEB]奶牛零食Treats for the Cows 洛谷 P2858

[USACO06FEB]奶牛零食Treats for the Cows 思路: 区间DP: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 2005 #define ll long long ll n,ai[maxn],dp[maxn][maxn],sum[maxn]; inline void in(ll &now) { char Cget=getchar();now=0; while(Cget>'9'

P2858 [USACO06FEB]奶牛零食Treats for the Cows

题目描述 FJ has purchased N (1 <= N <= 2000) yummy treats for the cows who get money for giving vast amounts of milk. FJ sells one treat per day and wants to maximize the money he receives over a given period time. The treats are interesting for many re

洛谷 P2858 [USACO06FEB]奶牛零食Treats for the Cows

题目描述 FJ has purchased N (1 <= N <= 2000) yummy treats for the cows who get money for giving vast amounts of milk. FJ sells one treat per day and wants to maximize the money he receives over a given period time. The treats are interesting for many re