[USACO08NOV]Mixed Up Cows
一看n那么小,那一定是状压dp了(表示从没写过,慌)。
首先dp[i][j](i 是一个二进制数,第x位为1代表选了第x头牛),表示 i 这个状态最后一头牛是第 j 头牛时的方案数。
然后当 j 被选上时,即 if(i & (1 << (j - 1)))时,我们在枚举倒数第二头牛p,也是当他存在时,且满足 abs(s[p] - s[j]) > k时,dp[i][j] += dp[i ^ (1 << (j - 1))][p],因为最后一头牛为p时,j 还没有被选,所以 j 这一位要亦或掉。
初始化就是只选一头牛的选法时一种,dp[1 << (i - 1)][i] = 1.
令 ms = (1 << n) - 1,则答案ans = sum(dp[ms][i]) (i = 1~n).
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<stack>
#include<queue>
#include<vector>
#include<cctype>
using namespace std;
#define space putchar(' ')
#define enter puts("")
#define Mem(a) memset(a, 0, sizeof(a))
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-;
const int maxn = ;
const int Maxs = 7e4;
inline ll read()
{
ll ans = ;
char ch = getchar(), last = ' ';
while(!isdigit(ch)) {last = ch; ch = getchar();}
while(isdigit(ch)) {ans = (ans << ) + (ans << ) + ch - ''; ch = getchar();}
if(last == '-') ans = -ans;
return ans;
}
inline void write(ll x)
{
if(x < ) putchar('-'), x = -x;
if(x >= ) write(x / );
putchar(x % + '');
} int n, k, a[maxn];
ll dp[Maxs][maxn]; int main()
{
n = read(); k = read();
for(int i = ; i <= n; ++i) a[i] = read(), dp[ << (i - )][i] = ;
int ms = ( << n) - ;
for(int i = ; i <= ms; ++i)
for(int j = ; j <= n; ++j)
if(i & ( << (j - )))
for(int p = ; p <= n; ++p)
if(p != j && (i & ( << (p - ))) && abs(a[p] - a[j]) > k)
dp[i][j] += dp[i ^ ( << (j - ))][p];
ll ans = ;
for(int i = ; i <= n; ++i) ans += dp[ms][i];
write(ans); enter;
return ;
}
[USACO08NOV]Mixed Up Cows的更多相关文章
- 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(状压动规)』
原题地址 观察数据范围:4 ≤ N ≤ 16. 很明显,这是一道状压DP. 定义:dp[i][j]表示队尾为奶牛i,当前含奶牛的状态为j,共有多少组符合条件的队伍. 代码实现如下: #include ...
- 洛谷 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 & ...
随机推荐
- JSONArray排序[收藏]
问题 JSONArray中嵌套JSONObject, 对JSONArray进行排序 排序前: [{"id":1,"name":"ljw"}, ...
- Session和Cookie的区别与联系
一. 概念理解 你可能有留意到当你浏览网页时,会有一些推送消息,大多数是你最近留意过的同类东西,比如你想买桌子,上淘宝搜了一下,结果连着几天会有各种各样的桌子的链接.这是因为 你浏览某个网页的时候,W ...
- Nginx代理后服务端使用remote_addr获取真实IP
直奔主题,在代理服务器的Nginx配置(yourWebsite.conf)的location /中添加: #获取客户端IP proxy_set_header Host $host; proxy_set ...
- Code Signal_练习题_Array Replace
Given an array of integers, replace all the occurrences of elemToReplace with substitutionElem. Exam ...
- elastic job will never fire
1. 描述 2018-08-20 18:11:01.912 [Thread-8] INFO org.quartz.impl.StdSchedulerFactory - Using default i ...
- request获取当前用户
1.request.getRemoteUser();//获取当前缓存的用户,比如Spring Security做权限控制后就会将用户登录名缓存到这里 request.getRemoteAddr();/ ...
- Thymeleaf模板表达式
日期格式.组件提取等. ${#dates.format(date)}${#dates.arrayFormat(datesArray)}${#dates.listFormat(datesList)}${ ...
- for、for..in、forEach、$.each等循环性能测试
var num = 10000000,arr = []; for(i=0;i<num;i++){ arr[i] = i+2; } //1) 使用 for 循环 function test1() ...
- 在已配置成功的opencv3.2.0下配置opencv_contrib模块
简介: 之前在Ubuntu下配置OpenCV时,因为对opencv3..0不是特别了解,没有把opencv_contrib进行安装,这里提醒大家尽量要一次性安装完毕,减少不必要的麻烦. .0文件夹 ( ...
- Ubuntu中网络配置interfaces与界面网络配置NetworkManager
[Server版本] 在Ubuntu Server版本中,因为只存有命令行模式,所以要想进行网络参数设置,只能通过修改 /etc/network/interfaces .具体设置方法如下: (1) U ...