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分 递推

最新文章

  1. nyoj220 推桌子(贪心算法)
  2. js的几种排序
  3. 拒绝IE8-,CSS3 transform rotate旋转动画效果(支持IE9+/chrome/firefox)
  4. Apache模块mod_security 和 Nginx过滤配置
  5. HashMap和Hashtable及HashSet的区别
  6. RAM云存储已经出现了,就是特别贵
  7. Mysql 分别按月, 日为组group,进行统计排序order
  8. Udacity调试课笔记之断言异常
  9. Web API 2中的Action Results
  10. eclipse如何导入项目和文件
  11. MySQL视图view/存储过程和函数的使用
  12. Python3 的注释
  13. Mac 上安装 GCC
  14. Java位运算原理及使用讲解
  15. 2 - Binary Search &amp; LogN Algorithm - Apr 18
  16. 浅谈FFT、NTT和MTT
  17. Shell 编程(实例一)
  18. C++ new动态数组初始化
  19. .NetCore Cap 结合 RabbitMQ 实现消息订阅
  20. FPGA配置方式

热门文章

  1. 关于ios::sync_with_stdio(false);和 cin.tie(0)加速c++输入输出流
  2. mysql编码
  3. 【转】CSS制作图形速查表-存档
  4. redisCheckMem脚本
  5. ACM学习历程—CSU 1216 异或最大值(xor &amp;&amp; 贪心 &amp;&amp; 字典树)
  6. java中的设计模式及其六大原则
  7. Git 权限控制
  8. Poj 2136 Vertical Histogram(打印垂直直方图)
  9. nmap 快速扫描所有端口
  10. JS中数组方法小总结