题目描述

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 ample bovine neck.

Gangsta cows are rebellious and line up to be milked in an order called 'Mixed Up'. A cow order is 'Mixed Up' if the sequence of serial numbers formed by their milking line is such that the serial numbers of every pair of consecutive cows in line differs by more than K (1 <= K <= 3400). For example, if N = 6 and K = 1 then 1, 3, 5, 2, 6, 4 is a 'Mixed Up' lineup but 1, 3, 6, 5, 2, 4 is not (since the consecutive numbers 5 and 6 differ by 1).

How many different ways can N cows be Mixed Up?

For your first 10 submissions, you will be provided with the results of running your program on a part of the actual test data.

POINTS: 200

约翰家有N头奶牛,第i头奶牛的编号是Si,每头奶牛的编号都是唯一的。这些奶牛最近 在闹脾气,为表达不满的情绪,她们在挤奶的时候一定要排成混乱的队伍。在一只混乱的队 伍中,相邻奶牛的编号之差均超过K。比如当K = 1时,1, 3, 5, 2, 6, 4就是一支混乱的队伍, 而1, 3, 6, 5, 2, 4不是,因为6和5只差1。请数一数,有多少种队形是混乱的呢?

题目解析

本来想打状压搜索的,结果分析了一下发现会T。

看到一个特别好的思路,记在这里

相比于考虑用几个奶牛,再枚举两重状态的三循环来说,我们可以考虑先枚举状态,再枚举状态里用过的牛哪个在队伍最后。这样只要两重循环就可以了,效率比原来高多了。

dp[i][j]表示用了i个牛,状态是j(01串)

注意开longlong,这题很坑

Code

#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std; int n,m;
long long ans;
int s[];
long long dp[][(<<)]; void init() {
for(int i = ; i <= n; i++) {
dp[i][<<(n-i)] = ;
}
return;
} bool judge(int j,int k) {
if(k == j) return false;
if(abs(s[j] - s[k]) > m) return true;
else return false;
} int main() {
scanf("%d%d",&n,&m);
for(int i = ; i <= n; i++) {
scanf("%d",&s[i]);
}
init();
int tmp;
for(int i = ; i < ( << n); i++) {
for(int j = ; j <= n; j++) {
if(dp[j][i]) continue;
if(i & ( << (n - j))) {
tmp = i ^ ( << (n - j));
for(int k = ; k <= n; k++) {
if(judge(k,j)) dp[j][i] += dp[k][tmp];
}
}
}
}
for(int i = ;i <= n;i++) {
ans += dp[i][(<<n)-];
}
printf("%lld",ans);
return ;
}

[USACO] 奶牛混合起来 Mixed Up Cows的更多相关文章

  1. 洛谷 P2915 [USACO08NOV]奶牛混合起来Mixed Up Cows 解题报告

    P2915 [USACO08NOV]奶牛混合起来Mixed Up Cows 题意: 给定一个长\(N\)的序列,求满足任意两个相邻元素之间的绝对值之差不超过\(K\)的这个序列的排列有多少个? 范围: ...

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

    P2915 [USACO08NOV]奶牛混合起来Mixed Up Cows 题目描述 Each of Farmer John's N (4 <= N <= 16) cows has a u ...

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

    P2915 [USACO08NOV]奶牛混合起来Mixed Up Cows 题目描述 Each of Farmer John's N (4 <= N <= 16) cows has a u ...

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

    题目描述 Each of Farmer John's N (4 <= N <= 16) cows has a unique serial number S_i (1 <= S_i & ...

  5. luogu 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 & ...

  6. [USACO08NOV]奶牛混合起来Mixed Up Cows(状态压缩DP)

    题目描述 Each of Farmer John's N (4 <= N <= 16) cows has a unique serial number S_i (1 <= S_i & ...

  7. 【题解】Luogu2915 [USACO08NOV]奶牛混合起来Mixed Up Cows

    题目描述 Each of Farmer John's N (4 <= N <= 16) cows has a unique serial number S_i (1 <= S_i & ...

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

    题目描述 约翰家有N头奶牛,第i头奶牛的编号是Si,每头奶牛的编号都是唯一的.这些奶牛最近 在闹脾气,为表达不满的情绪,她们在挤奶的时候一定要排成混乱的队伍.在一只混乱的队 伍中,相邻奶牛的编号之差均 ...

  9. 【USACO08NOV】奶牛混合起来Mixed Up Cows

    题目描述 约翰有 N 头奶牛,第 i 头奶牛的编号是 S i ,每头奶牛的编号都不同.这些奶牛最近在闹脾气, 为表达不满的情绪,她们在排队的时候一定要排成混乱的队伍.如果一只队伍里所有位置相邻的奶牛 ...

随机推荐

  1. A Go library implementing an FST (finite state transducer)——mark下

    https://github.com/couchbaselabs/vellum Building an FST To build an FST, create a new builder using ...

  2. [JSOI 2016] 最佳团体

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=4753 [算法] 很明显的分数规划 可以用树形动态规划(树形背包)检验答案 时间复杂度 ...

  3. 【HDU 4547】 CD操作

    [题目链接] 点击打开链接 [算法] 分四种情况讨论 : 1. 当前目录和目标目录是同一目录,不需要变换,答案为0 2. 当前目录是目标目录的祖先,答案为当前目录的深度 - 目标目录的深度 3. 当前 ...

  4. jquery autocomplete自动补全

    简单用法: $(function(){ var data = "the People's Republic of China".split(" "); $(&q ...

  5. debian下使用dpkg来安装/卸载deb包 (转载)

    转自:http://blog.csdn.net/zhou_2008/article/details/6076900 在debian下,你可以使用dpkg(Debian package system)来 ...

  6. Tomcat的jvm配置

    Tomcat本身不能直接在计算机上运行,需要依赖于操作系统和一个JAVA虚拟机.Tomcat的内存溢出本质就是JVM内存溢出,JAVA程序启动时JVM会分配一个初始内存和最大内存给程序.当程序需要的内 ...

  7. bzoj 1697: [Usaco2007 Feb]Cow Sorting牛排序【置换群】

    至今都不知道置换群是个什么东西--题解说什么就是什么.jpg 以下来自hzwer:http://hzwer.com/3905.html #include<iostream> #includ ...

  8. 洛谷P5398 [Ynoi2018]GOSICK(二次离线莫队)

    题面 传送门 题解 维包一生推 首先请确保您会二次离线莫队 那么我们现在的问题就是怎么转移了,对于\(i\)和前缀\([1,r]\)的贡献,我们拆成\(b_i\)和\(c_i\)两部分,其中\(b_i ...

  9. 获取Sprite上某一个点的透明度

    转载[ http://www.cnblogs.com/Androider123/p/3795050.html] 本篇文章主要讲一下怎么做一个不规则的按钮,比如如下图的八卦,点击绿色和点击红色部分,需要 ...

  10. MySQL客户端导入数据库脚本,字段值出现乱码解决方法

    解决方法1:在MySql安装目录下找到my.ini,将[mysql]下的default-character-set=latin1改为default-character-set=utf8,保存,然后重启 ...