题目描述

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。请数一数,有多少种队形是混乱的呢?

输入输出格式

输入格式:

 

  • Line 1: Two space-separated integers: N and K

  • Lines 2..N+1: Line i+1 contains a single integer that is the serial number of cow i: S_i

 

输出格式:

 

  • Line 1: A single integer that is the number of ways that N cows can be 'Mixed Up'. The answer is guaranteed to fit in a 64 bit integer.

 

输入输出样例

输入样例#1:

4 1
3
4
2
1
输出样例#1:

2

说明

The 2 possible Mixed Up arrangements are:

3 1 4 2

2 4 1 3

思路:

看到n的范围这么小就能猜到是状态压缩的动态规划。

设f[i][j]为j在二进制表示的那些牛中以第i只牛为尾合法的队形总数,则我们就可以得到动态转移方程f[i][j | (1 << p-1)]  += f[i][j] ,其中f[i][j | (1 << p-1)] 为状态j加上(1 << p-1)这只牛后的状态。其中 1 <= p <= n。

该动态转移方程必须符合以下条件:

状态j中没有包括第p只牛,且abs(s[p]-s[i]) > k。

所以,我们枚举每一个状态,并且在每一个状态中枚举第每一只牛,看这只牛是否在该状态中,如果在,我们则在此情况下进一步枚举,看那一只牛不在当前状态里,找到不在当前状态中的牛之后就进行累加,即为:

                     f[没在当前状态中的牛的输入顺序编号][当前状态加上不在状态中的牛后的新状态]  += f[当前状态的结尾牛输入顺序编号][当前状态];

初始化应该为f[i][1 << i] = 1, 因为,当队列中只有一只牛时最后一只牛的标号就是它本身且此情况上的子问题答案是1。

另外,这题因为答案大,所以要使用longlong,这里用到了条件编译,使得在任何平台下,该程序将不受longlon影响。

下面贴代码,有问题留言。

#include<cstdio>
#define N 1 << 17
#define S 20
using namespace std; long long f[S][N];
int s[]; #ifdef WIN32            //条件编译,省去longlong给程序带来的影响
#define LL "%I64d\n"
#else
#define LL "%lld\n"
#endif int main(){
int n,k;
scanf("%d%d",&n,&k);
for(int i = ; i <= n; i++)scanf("%d",&s[i]);
int mxx = ( << n)-;
for(int i = ; i <= n; i++)f[i][ << (i-)] = ;
for(int i = ; i <= mxx+;i++){                //枚举每一个状态
for(int j = ; j <= n; j++){
if(i & ( << j-)){                //第j只牛是否在状态i中
for(int p = ; p <= n; p++)          //进一步枚举没有在状态i中的牛
if(!(i & ( << p-)) && (s[j]-s[p] > k || s[p]-s[j] > k)){ //如果k不在队列中且差值大于k
f[p][i | ( << p-)] += f[j][i];
}
}
}
}
long long ans = ;
for(int i = ; i <= n; i++)ans += f[i][mxx];
printf(LL,ans);
return ;
}

[USACO08NOV]奶牛混合起来Mixed Up Cows(状态压缩DP)的更多相关文章

  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. 【题解】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 & ...

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

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

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

    类似于n皇后的思想,只要把dfs表示放置情况的数字压缩成一个整数,就能实现记忆化搜索了. 一些有关集合的操作: {i}在集合S内:S&(1<<i)==1: 将{i}加入集合S:S= ...

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

    首先我们能够一眼看到4 <= N <= 16,那么就是它了,我们要压缩的状态就是它了 那么之后能我们用这个状态表示什么呢,我们要表示的显然是每只奶牛是否在队伍中 比如说10吧,转成二进制后 ...

随机推荐

  1. 0316 【案例】MySQL count操作优化案例一则

      转自http://blog.itpub.net/22664653/viewspace-1791124/ 一 背景 某业务的数据库定期报 thread_runing 飙高,通定位发现一个慢查询sql ...

  2. bzoj3931【CQOI2015】网络吞吐量

    3931: [CQOI2015]网络吞吐量 Time Limit: 10 Sec  Memory Limit: 512 MB Submit: 853  Solved: 381 [Submit][id= ...

  3. Leetcode45:Intersection of Two Linked Lists

    Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...

  4. 新建maven web工程报错

    问题: 检查本地仓库: 检查1.0跟release的文件夹: 试试:http://www.ithao123.cn/content-8028507.html 然后选择maven catalog下的:(这 ...

  5. Mule ESB-3.Build a webservice proxy

    自从引入ESB后,系统之间不再直接依赖.我负责的这块,主要是解决Webservice的问题.使系统A不再直接依赖系统B的Webservice. 我们选择的产品是Mule ESB.所以自然要使用Mule ...

  6. SQL SERVER读书笔记:内存

    系统先操作地址空间,真正要用的时候才申请物理内存,进行使用. Reserved Memory  保留内存,虚拟内存 Commited Memory 提交内存,物理内存 [如何判断SQL SERVER ...

  7. POJ2228 Naptime 环形DP

    题目大意:牛在第i个小时睡觉能够恢复U[i]点体力.睡觉时第一小时不恢复体力.一天的N小时连着下一天的1小时.求能够恢复体力的和的最大值. 定义DP[i][j][0]为前i个小时休息了j个小时,i小时 ...

  8. spring的bean管理(注解和配置文件混合使用)

    1.建三个类,在一个类中引用其他两个类 import javax.annotation.Resource; import org.springframework.beans.factory.annot ...

  9. TF-IDF算法--关键词句和文本集中每篇文章相关度计算

    关键词句和文本集每篇文章相关度计算:假设语料库中有几万篇文章,每篇文章的长度不一,你任意输入关键词或句子,通过代码以tf-idf值为准检索出来相似度高的文章. 1.TF-IDF概述 TF-IDF是一种 ...

  10. ORA-03137 - ORA-12592 TNS:BAD PACKET OR ORA-3137 故障处理

    环境 操作系统:CentOS release 6.8 数据库:oracle 11.2.0.4.190115 说明:数据库psu 为19年1月份的补丁,可不间断运行,但是开发提示在执行一些批处理的时候, ...