[USACO] 奶牛混合起来 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。请数一数,有多少种队形是混乱的呢?
题目解析
本来想打状压搜索的,结果分析了一下发现会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的更多相关文章
- 洛谷 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 & ...
- 【题解】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 & ...
- P2915 [USACO08NOV]奶牛混合起来Mixed Up Cows
题目描述 约翰家有N头奶牛,第i头奶牛的编号是Si,每头奶牛的编号都是唯一的.这些奶牛最近 在闹脾气,为表达不满的情绪,她们在挤奶的时候一定要排成混乱的队伍.在一只混乱的队 伍中,相邻奶牛的编号之差均 ...
- 【USACO08NOV】奶牛混合起来Mixed Up Cows
题目描述 约翰有 N 头奶牛,第 i 头奶牛的编号是 S i ,每头奶牛的编号都不同.这些奶牛最近在闹脾气, 为表达不满的情绪,她们在排队的时候一定要排成混乱的队伍.如果一只队伍里所有位置相邻的奶牛 ...
随机推荐
- 关于spring配置文件中编辑时没有提示信息的问题
spring配置文件头部信息主要是提供一个xml的编写规范作用. 新创建的配置文件引入头部信息后,编辑时没有提示信息,重启elipse即可解决.
- SICP习题练习
练习1.6 new-if的三个参数会先被执行,这样就会无限循环下去 练习1.7 (define (sqrt-iter last-guess guess x) (if (good-enough? las ...
- Android编译详解之lunch命令 【转】
本文转载自: Android编译详解之lunch命令 (2012-10-08 10:27:55) 转载▼ 标签: it 分类: android内核剖析 Android的优势就在于其开源,手机和 ...
- ExecuteNonQuery()的用法
ExecuteNonQuery()的用法 下面我们将详细讲解如何在Page_Load()中对数据库的增加.删除.修改,最后我们再来总结一下ExecuteNonQuery(),ExecuteScalar ...
- bzoj1898 [Zjoi2005]Swamp 沼泽鳄鱼——矩阵快速幂
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1898 求到达方案数...还是矩阵快速幂: 能够到达就把邻接矩阵那里赋成1,有鳄鱼的地方从各处 ...
- bzoj3884
http://www.lydsy.com/JudgeOnline/problem.php?id=3884 拓展欧拉定理 http://blog.csdn.net/Pedro_Lee/article/d ...
- WP8 中使用HTML Agility Pack与友盟分享SDK遇到的 System.Xml.XPath加载问题
今晚在尝试使用友盟最新的社交分享SDK时,按照官方Demo,并未做多少多少改动,就是去除了对微信.脸书和推特的分享.然后运行之后就一直报错 : {System.IO.FileLoadException ...
- 31. ExtJs4回车事件监听
转自:https://710542316.iteye.com/blog/2148542 监听表单字段事件: Ext使得对用户某个动作的监听特别简单,诸如单击某个元素或者按下某个键盘上的键. 一个经常性 ...
- JS判断字符串中是否存在某个字符
用String类中的indexOf函数,例如:String str="we find out sth";if(str.indexOf("o")==-1){ // ...
- JS 九宫格算法 用原生js实现
九宫格算法核心: 利用控件索引index计算出控件所在的行数和列数: 利用控件计算出left距离: 利用控件计算出top距离: 写特效时需要用到定位 公式: 行 row=parseInt(i/cols ...