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 <= 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

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#define maxn 20
using namespace std;
int n,k,a[maxn],ans;
bool vis[maxn];
void dfs(int cnt,int pre){
if(cnt==n){ans++;return;}
for(int i=;i<=n;i++){
if(!vis[i]&&abs(a[i]-pre)>k){
vis[i]=;
dfs(cnt+,a[i]);
vis[i]=;
}
}
}
int main(){
freopen("Cola.txt","r",stdin);
scanf("%d%d",&n,&k);
for(int i=;i<=n;i++)scanf("%d",&a[i]);
dfs(,-);
printf("%d",ans);
}

70分 暴力

/*
记忆化搜索,f[sta][i]表示选了的牛的集合为sta,且最后一个牛是i的混乱序列方案数
*/
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<cstdlib>
#define maxn 20
#ifdef WIM32
#define LL "%I64d"
#else
#define LL "%lld"
#endif
using namespace std;
int n,k,a[maxn],ans;
bool vis[maxn];
long long f[<<][];
long long dfs(int sta,int cnt,int pre){
if(cnt==n){return f[sta][pre]=;}
if(f[sta][pre]!=-)return f[sta][pre];
long long res=;
for(int i=;i<=n;i++){
if(!vis[i]&&abs(a[i]-a[pre])>k){
vis[i]=;
res+=dfs(sta+(<<(i-)),cnt+,i);
vis[i]=;
}
}
f[sta][pre]=res;
return res;
}
int main(){
memset(f,-,sizeof(f));
scanf("%d%d",&n,&k);
for(int i=;i<=n;i++)scanf("%d",&a[i]);
a[]=-;
dfs(,,);
printf(LL,f[][]);
}

100分 记忆化搜索

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#ifdef WIN32
#define LL "%I64d"
#else
#define LL "%lld"
#endif
using namespace std;
int n,k,a[];
long long f[<<][];
int main(){
scanf("%d%d",&n,&k);
for(int i=;i<=n;i++)scanf("%d",&a[i]);
for(int i=;i<=n;i++)f[(<<(i-))][i]=;
for(int sta=;sta<(<<n);sta++){
for(int i=;i<=n;i++){
if(sta&(<<(i-))){
for(int j=;j<=n;j++){
if(abs(a[j]-a[i])>k)f[sta][i]+=f[sta^(<<(i-))][j];
}
}
}
}
long long ans=;
for(int i=;i<=n;i++)ans+=f[(<<n)-][i];
printf(LL,ans);
}

100分 递推

洛谷P2915 [USACO08NOV]奶牛混合起来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 状压动归

    考场上空间开大了一倍就爆0了QAQ- Code: #include<cstdio> #include<algorithm> #include<cmath> usin ...

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

    一道水状压,然而不知道是不是太久没做过dp了,我盯着它二十分钟才反应过来.... 还把数组开小了WA了一发QAQ //Twenty #include<algorithm> #include ...

  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. P2915 [USACO08NOV]奶牛混合起来Mixed Up Cows

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

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

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

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

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

  9. [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 & ...

随机推荐

  1. hadoop_学习_02_Hadoop环境搭建(单机)

    一.环境准备 1.说明 hadoop的下载来源有: 官方版本:http://archive.apache.org/dist/hadoop/ CDH版本:http://archive.cloudera. ...

  2. BEC listen and translation exercise 35

    高中听力: At five o'clock, we have afternoon tea, but we don't have it in the kitchen. Father's Day is t ...

  3. visual studio 高级选项及配置

    visual studio 是一款强大的 IDE,所谓 IDE 即是将通过命令行(一系列复杂的参数选项)编译.链接等操作内置到 IDE 的界面按钮处. 为什么新建的工程,可以直接 #include & ...

  4. 【二叉树的递归】03判断二叉树中有没有和为给定值的路径【Path Sum】

    ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 给定一个二叉树和一个和,判断这个树 ...

  5. raspi-config Expand root partition to fill SD card 原理

    /********************************************************************************** * raspi-config E ...

  6. [转]sscanf函数具体用法

    大学生程序代写 sscanf 名称: sscanf() - 从一个字符串中读进与指定格式相符的数据. 函数原型: Int sscanf( string str, string fmt, mixed v ...

  7. Wannafly #4 F 线路规划

    数据范围252501 劲啊 Q国的监察院是一个神秘的组织. 这个组织掌握了整个Q国的地下力量,监察着Q国的每一个人. 监察院一共有N个成员,每一个成员都有且仅有1个直接上司,而他只听从其上直接司的命令 ...

  8. Gym - 100851G:Generators(人尽皆知但是WA题)

    题意:现在有函数,每一项Xi=(A*X(i-1)+B)%C.现在给定N个函数以及K:X0,A,B,C.然你再每个函数选择一个数,使得其和最大,而且不被K整除. X0,A,B,C<=1e3 :K& ...

  9. 浅谈MVC、MVP、MVVM模式

    mvc的模式已经深入人心,想必大家都很熟悉,但是未必都能遵守mvc模式.我们的一个mvc项目比较简单,主要是数据库的查询.一个DBHelp类,封装了数据库的操作,然后Controller中进行中各种查 ...

  10. codevs1060 搞笑世界杯

    题目描述 Description 随着世界杯小组赛的结束,法国,阿根廷等世界强队都纷纷被淘汰,让人心痛不已. 于是有 人组织了一场搞笑世界杯,将这些被淘汰的强队重新组织起来和世界杯一同比赛.你和你的朋 ...