题目描述

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

干了一下午的线段树合并和主席树还没整明白,想到自己还这么菜就很烦,看到一道状压DP能自己静下心来推了还是挺开心的。

如果$n$小于10就可以dfs回溯求了。

$f\left[ S\right] \left[ i\right]$表示集合$S$已被排列 且已第$i$个编号结尾的方案数

$f\left[ S|(1<<i)\right]\left[i \right ] =\sum  f\left[ s\right]\left[j \right ]$ 其中$j\in S\& \& \left| s_{i}-s_{j}\right| >k$

复杂度$O\left( n^{2}2^{n}\right)$

#include <bits/stdc++.h>
#define ll long long
using namespace std;inline int read() {int x = 0, f = 1; char ch = getchar();while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); }while (ch >= '0' && ch <= '9') { x = x * 10 + ch - 48; ch = getchar(); }return x * f;
}const int N = 17;
ll f[1<<N][N];
int t[N], n, k;int main() {n = read(), k = read();for (int i = 1; i <= n; i++) t[i] = read();f[0][0] = 1;int S = 1 << n;for (int s = 0; s < S; s++) {for (int i = 1; i <= n; i++) {int ii = i - 1;if ((1 << ii) & s) continue;for (int j = 0; j <= n; j++) {int jj = j - 1;if (jj < 0) { f[s | (1 << ii)][i] += f[s][j]; continue; }if ((1 << jj) & s && abs(t[j] - t[i]) > k) {f[s | (1 << ii)][i] += f[s][j];}}}}ll ans = 0;for (int i = 1; i <= n; i++) ans += f[S - 1][i];printf("%lld\n", ans);return 0;
}

View Code

转载于:https://www.cnblogs.com/Mrzdtz220/p/11124095.html

P2915 [USACO08NOV] Mixed Up Cows相关推荐

  1. [Luogu2915] [USACO08Nov,Gold] Mixed Up Cows [状态压缩][dp]

    [Link\frak{Link}Link] 如果这道题目的K≤9那就是一道数位dp了 不过它的K≤3400,N≤16. 水状压. #include<cstdio> #include< ...

  2. 状压dp个人刷题记录

    目录 一.普通型 蒙德里安的梦想 题意: 思路: code: #2153. 「SCOI2005」互不侵犯 题意: 思路: code: P1879 [USACO06NOV]Corn Fields G 题 ...

  3. 记录错误or日记(更新中)

    前言: 从2018.8-17开始记录 本篇随笔记录做题时的小错误(大多数),考试总结(懒得总结了),做过的每个题的错误 2019.8.10 欧吼 2019.8.9 台风呢,台风呢,被我的砖头本挡住了. ...

  4. 洛谷——P2916 [USACO08NOV]为母牛欢呼Cheering up the Cows

    https://www.luogu.org/problem/show?pid=2916 题目描述 Farmer John has grown so lazy that he no longer wan ...

  5. 洛谷P2826 [USACO08NOV]光开关Light Switching [2017年6月计划 线段树02]

    P2826 [USACO08NOV]光开关Light Switching 题目描述 Farmer John tries to keep the cows sharp by letting them p ...

  6. Milking Cows 挤牛奶

    1.2.1 Milking Cows 挤牛奶 Time Limit: 1 Sec  Memory Limit: 64 MB Submit: 554  Solved: 108 [Submit][Stat ...

  7. 制作 Swift 和 Objective-C Mixed 的 Pod

    来源:南栀倾寒 www.jianshu.com/p/c7623c31d77b 如有好文章投稿,请点击 → 这里了解详情 知识背景 What is CocoaPods(https://guides.co ...

  8. 二分搜索 POJ 2456 Aggressive cows

    题目传送门 1 /* 2 二分搜索:搜索安排最近牛的距离不小于d 3 */ 4 #include <cstdio> 5 #include <algorithm> 6 #incl ...

  9. POJ 2456 Aggressive cows(二分答案)

    Aggressive cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 22674 Accepted: 10636 Des ...

  10. 洛谷——P2341 [HAOI2006]受欢迎的牛//POJ2186:Popular Cows

    P2341 [HAOI2006]受欢迎的牛/POJ2186:Popular Cows 题目背景 本题测试数据已修复. 题目描述 每头奶牛都梦想成为牛棚里的明星.被所有奶牛喜欢的奶牛就是一头明星奶牛.所 ...

最新文章

  1. Android-Binder进程间通讯机制-多图详解
  2. html5 css3 卡片切换,HTML5之纯CSS3实现的tab标签切换
  3. window 添加环境变量
  4. 第四范式入选Forrester中国机器学习Now Tech™,成唯一AutoML专注类大型厂商
  5. 服务器向客户端不响应为null的属性(为了便于查询JSON数据)spring.jackson.default-property-inclusion=NON_NULL
  6. 32添加组件_软件开发32条法则:经过实践检验的实用建议和经验教训
  7. oracle11g win10版本,win10系统安装的oracle11g和cloud6.2 创建数据中心报错
  8. 金三银五,金九银十,找工作的好时间
  9. halcon深度学习
  10. sql语句实现动态添加查询条件
  11. 量化交易策略五_PEG策略
  12. 计算机数据管理应用,浅析计算机数据库管理系统的应用.pdf
  13. 拥有百万粉丝的大牛讲述学Android的历程程。看看你缺了哪些?
  14. Eclipse开发环境搭建
  15. 如何添加旺旺客户,淘宝店铺左侧代码
  16. jupyter lab R
  17. 9.5 考试 第三题 奇袭题解(codeforce 526f)
  18. 群辉中安装openwrt
  19. linux zip 命令的大坑
  20. hdu1426一道很有意思的题目:数独

热门文章

  1. [渝粤教育] 西南科技大学 经济法概论 在线考试复习资料2021版
  2. 【渝粤教育】电大中专品牌管理与推广 (2)_1作业 题库
  3. C++中的左值和右值的区别
  4. Springboot vue.js html 跨域 前后分离 Activiti6 shiro 权限
  5. Linux之ssh无密码登录
  6. JavaScript数据结构与算法-列表练习
  7. Android高级控件(一)——ListView绑定CheckBox实现全选,添加和删除等功能
  8. [贪心+模拟] zoj 3829 Known Notation
  9. [转]asp 没有权限: 'CreateObject'的解决方法
  10. 【转】Laplace 算子