【题解】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 <= 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
思路
- 用二进制表示牛的状态state
- 设$f[state][last]$ 表示牛的排列状态为state,最后一头牛为last时的合法队列数
- 当且仅当状态j中没有包括第i只牛,且$abs(s[i]-s[last]) > k$ 时,第i只牛可以加入队列中
- 此时可以有转移方程$dp[State|(1<<i)][i]+=dp[State][last]$
注意 边界条件为 $dp[i][1<<i]=1$
- 最终答案
$$answer= \sum_{i=0}^{n-1}dp[(1<<n)-1][i]$$
代码
#include<cmath>
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#define re register int
#define ll long long
using namespace std;
inline int read(){
int x=0,w=1;
char ch=getchar();
while(ch!='-'&&(ch<'0'||ch>'9')) ch=getchar();
if(ch=='-') w=-1,ch=getchar();
while(ch>='0'&&ch<='9') x=(x<<1)+(x<<3)+ch-48,ch=getchar();
return x*w;
}
const int maxs=(1<<16)+5,maxn=16+5;
int n,k;
ll ans;
ll dp[maxs][maxn],num[maxn];
int main() {
n=read(),k=read();
for(re i=0;i<n;++i) num[i]=read(),dp[1<<i][i]=1;
for(re S=0;S<(1<<n);++S) for(re i=0;i<n;++i) //枚举每一个状态
if(S&(1<<i)) for(re j=0;j<n;++j) //第j只牛是否在状态i中,进一步枚举没有在状态i中的牛
if(!(S&(1<<j))&&abs(num[j]-num[i])>k) //如果k不在队列中且差值大于k
dp[S|(1<<j)][j]+=dp[S][i];
for(re i=0;i<n;++i) ans+=dp[(1<<n)-1][i];
printf("%lld\n",ans);
return 0;
}
【题解】Luogu2915 [USACO08NOV]奶牛混合起来Mixed Up Cows的更多相关文章
- Luogu2915 [USACO08NOV]奶牛混合起来Mixed Up Cows (状压DP)
枚举末位状态 #include <iostream> #include <cstdio> #include <cstring> #include <algor ...
- 洛谷 P2915 [USACO08NOV]奶牛混合起来Mixed Up Cows 解题报告
P2915 [USACO08NOV]奶牛混合起来Mixed Up Cows 题意: 给定一个长\(N\)的序列,求满足任意两个相邻元素之间的绝对值之差不超过\(K\)的这个序列的排列有多少个? 范围: ...
- 洛谷P2915 [USACO08NOV]奶牛混合起来Mixed Up Cows
P2915 [USACO08NOV]奶牛混合起来Mixed Up Cows 题目描述 Each of Farmer John's N (4 <= N <= 16) cows has a u ...
- 洛谷 P2915 [USACO08NOV]奶牛混合起来Mixed Up Cows
P2915 [USACO08NOV]奶牛混合起来Mixed Up Cows 题目描述 Each of Farmer John's N (4 <= N <= 16) cows has a u ...
- [USACO08NOV]奶牛混合起来Mixed Up Cows
题目描述 Each of Farmer John's N (4 <= N <= 16) cows has a unique serial number S_i (1 <= S_i & ...
- 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 & ...
- [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 & ...
- P2915 [USACO08NOV]奶牛混合起来Mixed Up Cows
题目描述 约翰家有N头奶牛,第i头奶牛的编号是Si,每头奶牛的编号都是唯一的.这些奶牛最近 在闹脾气,为表达不满的情绪,她们在挤奶的时候一定要排成混乱的队伍.在一只混乱的队 伍中,相邻奶牛的编号之差均 ...
- 洛谷 P2915 【[USACO08NOV]奶牛混合起来Mixed Up Cows】
类似于n皇后的思想,只要把dfs表示放置情况的数字压缩成一个整数,就能实现记忆化搜索了. 一些有关集合的操作: {i}在集合S内:S&(1<<i)==1: 将{i}加入集合S:S= ...
随机推荐
- ZwQuerySystemInformation枚举内核模块
在内核中通过调用此函数来枚举windows系统中已经加载的内核模块. NTSTATUS ZwQuerySystemInformation ( SYSTEM_INFORMATION_CLASS Syst ...
- Django(15)外键和表关系
外键删除操作 如果一个模型使用了外键.那么在对方那个模型被删掉后,该进行什么样的操作.可以通过on_delete来指定.可以指定的类型如下: CASCADE:级联操作.如果外键对应的那条数据被删除了, ...
- CodeForces - 879
A 题意:就是一共有n个医生,每个医生上班的时间是第Si天,之后每隔d天去上班,问最少多少天能够访问完这n名医生 思路:直接进攻模拟就可以 代码: 1 #include<iostream> ...
- CAS的理解
CAS(CompareAndSweep)工作方式 CAS是乐观锁技术,当多个线程尝试使用CAS同时更新同一个变量时,只有其中一个线程能更新变量的值,而其它线程都失败,失败的线程并不会被挂起,而是被 ...
- (Dubbo架构)基于MDC+Filter的跨应用分布式日志追踪解决方案
在单体应用中,日志追踪通常的解决方案是给日志添加 tranID(追踪ID),生成规则因系统而异,大致效果如下: 查询时只要使用 grep 命令进行追踪id筛选即可查到此次调用链中所有日志,但是在 du ...
- WIKI和JIRA-安装与使用
1.Wiki介绍1.1 Wiki(多人协作的写作系统)是一种超文本系统,这种超文本系统支持面向社群的协作式写作,即人人可编辑.在公司的项目管理中,可以把它当作文档管理和信息组织(Portlet)系统来 ...
- 【海通国际】Joe Lowry(Mr. Lithium)谈全球电池原材料供应危机
[海通国际]Joe Lowry(Mr. Lithium)谈全球电池原材料供应危机 环球锂业公司(Global Lithium)总裁Joe Lowry日前接受了欧洲锰业Euro Manganese的邀请 ...
- iozone测试方法-20191008
iozone 一.简介 磁盘设备之上是文件系统,测试磁盘的工具往往就是调用驱动块设备驱动的接口进行读写测试.而文件系统的测试软件就是,针对文件系统层提供的功能进行测试,包括文件的打开关闭速度以及顺 ...
- Ansible_编写Playbook文件
一.Playbook的实施 1.Ansible playbook与临时命令概述: 临时命令可以作为一次性命令对一组目标主机运行一项简单的任务 play是针对清单中选定的主机运行的一组有序任务.play ...
- MyBatis 高级查询之一对一查询(九)
高级查询之一对一查询 查询条件:根据游戏角色ID,查询账号信息 我们在之前创建的映射器接口 GameMapper.java 中添加接口方法,如下: /** * 根据角色ID查询账号信息 * @para ...