[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 ,每头奶牛的编号都不同.这些奶牛最近在闹脾气, 为表达不满的情绪,她们在排队的时候一定要排成混乱的队伍.如果一只队伍里所有位置相邻的奶牛 ...
随机推荐
- [RK3288][Android6.0] 调试笔记 --- 如何确认声卡是否注册成功【转】
本文转载自:http://blog.csdn.net/kris_fei/article/details/78399875 Platform: RK3288 OS: Android 6.0 Kernel ...
- docker 清理容器和镜像
在docker运行过程中,会不知不觉造出很多容器,很多都是不用的,需要清理. 下面就是一些清理办法,一个个清理肯定很低效,批量清理很有意思. 查看正在运行的容器 # docker ps -q 9b9f ...
- 5. extjs 中buttonAlign什么意思
转自:https://zhidao.baidu.com/question/1174901985976576339.html指定Panel中按钮的位置.可配置的值有'right', 'left' 和 ' ...
- sql2000数据库置疑造成的原因以及如何解决置疑
造成数据库置疑一般有以下几点: 1)电脑非法关机或者意外停电: 2)磁盘有坏道或者损坏: 3)数据库感染病毒,日志文件损坏: 4)非正常情况下移动数据库文件 5)系统,硬盘,经常强制性关机(如断电)类 ...
- VIDIOC_S_INPUT 作用 (转载)
转载:http://blog.csdn.net/kickxxx/article/details/7088658 G_INPUT和S_INPUT用来查询和选则当前的input 一个video设备节点可能 ...
- P2921 [USACO08DEC]在农场万圣节Trick or Treat on the Farm(Tarjan+记忆化)
P2921 [USACO08DEC]在农场万圣节Trick or Treat on the Farm 题意翻译 题目描述 每年,在威斯康星州,奶牛们都会穿上衣服,收集农夫约翰在N(1<=N< ...
- 在sql语句中使用关键字
背景 开发过程中遇到了遇到了一句sql语句一直报错,看了一下字段名和表名都对应上了,但是还是一直报错 sql语句如下: update table set using = ""hh ...
- (快速幂)51NOD 1046 A^B Mod C
给出3个正整数A B C,求A^B Mod C. 例如,3 5 8,3^5 Mod 8 = 3. Input 3个正整数A B C,中间用空格分隔.(1 <= A,B,C <= 10^ ...
- 修复mysql的表
数据损坏原因 MySQL表损坏一般是数据损坏,引起损坏的原因可能是由于磁盘损坏.系统崩溃或者MySQL服务器被崩溃等外部原因.例如有人使用kill -9终止进程,导致MySQL进程未能正常关闭,那么就 ...
- AGC16E Poor Turkeys
输入样例: 10 10 8 9 2 8 4 6 4 9 7 8 2 8 1 8 3 4 3 4 2 7 输出样例#6: 5 话说这题虽然不是很OI但是确实挺锻炼思维的 一开始以为是用并查集之类的东西维 ...