洛谷 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.
输入输出样例
说明
The 2 possible Mixed Up arrangements are:
3 1 4 2
2 4 1 3
思路:
40分暴力
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define MAXN 17
using namespace std;
int N,K,ans;
int num[MAXN],vis[MAXN];
void dfs(int now,int pre){
if(now==N){
ans++;
return ;
}
for(int i=;i<=N;i++)
if(!vis[i]&&abs(num[pre]-num[i])>K){
vis[i]=;
dfs(now+,i);
vis[i]=;
}
}
int main(){
scanf("%d%d",&N,&K);
for(int i=;i<=N;i++)
scanf("%d",&num[i]);
dfs(,-);
cout<<ans;
}
70分暴力:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define MAXN 17
using namespace std;
int N,K,ans;
int num[MAXN],vis[MAXN];
void dfs(int now,int pre){
if(now==N){
ans++;
return ;
}
for(int i=;i<=N;i++)
if(!vis[i]&&abs(pre-num[i])>K){
vis[i]=;
dfs(now+,num[i]);
vis[i]=;
}
}
int main(){
scanf("%d%d",&N,&K);
for(int i=;i<=N;i++)
scanf("%d",&num[i]);
dfs(,-);
cout<<ans;
}
100分记忆化搜索
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define MAXN 17
using namespace std;
int N,K;
int num[MAXN];
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(num[i]-num[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",&num[i]);
num[]=-;
dfs(,,);
printf("%lld",f[][]);
}
洛谷 P2915 [USACO08NOV]奶牛混合起来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 状压动归
考场上空间开大了一倍就爆0了QAQ- Code: #include<cstdio> #include<algorithm> #include<cmath> usin ...
- 洛谷 2915 [USACO08NOV]奶牛混合起来Mixed Up Cows
一道水状压,然而不知道是不是太久没做过dp了,我盯着它二十分钟才反应过来.... 还把数组开小了WA了一发QAQ //Twenty #include<algorithm> #include ...
- 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 & ...
- P2915 [USACO08NOV]奶牛混合起来Mixed Up Cows
题目描述 约翰家有N头奶牛,第i头奶牛的编号是Si,每头奶牛的编号都是唯一的.这些奶牛最近 在闹脾气,为表达不满的情绪,她们在挤奶的时候一定要排成混乱的队伍.在一只混乱的队 伍中,相邻奶牛的编号之差均 ...
- 洛谷 P2915 【[USACO08NOV]奶牛混合起来Mixed Up Cows】
类似于n皇后的思想,只要把dfs表示放置情况的数字压缩成一个整数,就能实现记忆化搜索了. 一些有关集合的操作: {i}在集合S内:S&(1<<i)==1: 将{i}加入集合S:S= ...
- [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 & ...
随机推荐
- 禁用backspace网页回退功能
<script language="JavaScript">document.onkeydown = check;function check(e) { var cod ...
- [Swift通天遁地]三、手势与图表-(13)制作美观简介的滚动图表:折线图表、面积图表、柱形图表、散点图表
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- [Swift通天遁地]七、数据与安全-(1)XML文档的创建和解析
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- Sublime Text Version 3.0,Build3143注册码
1.打开sublime text软件2.Help->Enter License3.复制以下BEGIN LICENSE和END LICENSE之间的部分,粘贴进去.(注意:不要复制BEGIN LI ...
- 普通平衡树代码。。。Treap
应一些人之邀...发一篇代码 #include <iostream> #include <cstdio> #include <cstdlib> #include & ...
- Android内存管理(8)Dalvik,ART和 .dex 是什么*
什么是Dalvik: Dalvik是Google公司自己设计用于Android平台的Java虚拟机.Dalvik虚拟机是Google等厂商合作开发的Android移动设备平台的核心组成部分之一. Da ...
- Offer收割_5
训练 投入 欲望. ---贾森博尔特 第一题:二分枚举答案,check时候模拟一下即可. 时间复杂度: O(n*logn). 第二题: 描述 小Hi在虚拟世界中有一只小宠物小P.小P有K种属性,每种 ...
- CentOS配置网卡以及克隆
上一篇的虚拟机安装完成后是可以上网的,但是ip地址是动态的,因为后期的需要,我们要配置下网卡,改成静态的IP地址 1.打开终端,输入 ifconfig 查看虚拟机中的网卡,发现时ens33而不是平常见 ...
- Android基础TOP2:单机按钮改变字体颜色
---恢复内容开始--- Activity: <TextView android:id="@+id/t1" android:textSize="30dp" ...
- 【PostgreSQL-9.6.3】触发器实例
1. 创建一个触发器,表中的行在任何时候被插入或更新时,当前用户名和时间也会被标记在该行中.并且它会检查雇员的姓名以及薪水. --创建测试表 CREATE TABLE emp ( empname te ...