题目描述

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: 复制

  1. 4 1
  2. 3
  3. 4
  4. 2
  5. 1
输出样例#1: 复制

  1. 2

说明

The 2 possible Mixed Up arrangements are:

3 1 4 2

2 4 1 3

简单的装压dp

保证后一个状态比前一个小

题目没有翻译long long

  1. #include<cstdio>
  2. #include<algorithm>
  3. const int maxn = ;
  4. #define LL long long
  5. inline int read() {
  6. int x=,f=;
  7. char c=getchar() ;
  8. while(c<''||c>''){ if(c=='-')f=-;c=getchar();};
  9. while(c<=''&&c>='')x=x*+c-'',c=getchar();
  10. return x*f;
  11. }
  12. int n,op,s[maxn];
  13. LL dp[maxn][<<maxn];
  14. int main() {
  15. n=read(),op=read();
  16. for(int i=;i<=n;++i) {
  17. s[i]=read();
  18. }
  19. for(int i=;i<=n;++i) {
  20. dp[i][<<i-]=;
  21. }
  22. for(int j=;j<=(<<n)+;++j)
  23. for(int i=;i<=n;++i) {
  24. if((<<i-)&j) {
  25. for(int k=;k<=n;k++) {
  26. if(!(j&(<<k-))&&abs(s[i]-s[k])>op) {
  27. dp[k][j|(<<k-)]+=dp[i][j];
  28. }
  29. }
  30. }
  31. }
  32. LL ans=;
  33. for(int i=;i<=n;++i) {
  34. ans+=dp[i][(<<n)-];
  35. }
  36. printf("%lld\n",ans);
  37. return ;
  38. }

luogu 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

    P2915 [USACO08NOV]奶牛混合起来Mixed Up Cows 题目描述 Each of Farmer John's N (4 <= N <= 16) cows has a u ...

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

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

  5. 洛谷P2915 [USACO08NOV]奶牛混合起来Mixed Up Cows 状压动归

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

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

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

  7. Luogu P2915 [USACO08NOV]奶牛混合起来

    题外话: 是非常颓废的博主 写题解也不在于能不能通过啦,主要是缓解颓废 首先看到这个题,肯定是可以暴力搜索的: 不得不说这道题还是很善良的,一波大暴力dfs,居然有70pts: #include< ...

  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. Careercup - Microsoft面试题 - 5799446021406720

    2014-05-12 07:17 题目链接 原题: Given below is a tree/trie A B c D e F a<b<e<>>c<>d&l ...

  2. Python学习-day15-JavaScript

    JavaScript是一门编程语言,浏览器内置了JavaScript语言的解释器,所以在浏览器上按照JavaScript语言的规则编写相应代码之,浏览器可以解释并做出相应的处理. 一.如何编写 1.J ...

  3. python re模块详解

    re模块 re模块使用python拥有全部的正则表达式功能 1 2 3 4 re.I(re.IGNORECASE): 忽略大小写(括号内是完整写法)  re.M(MULTILINE):(多行模式,改变 ...

  4. 【bzoj4066】简单题 KD-tree

    题目描述 你有一个N*N的棋盘,每个格子内有一个整数,初始时的时候全部为0,现在需要维护两种操作: 命令 参数限制 内容 1 x y A 1<=x,y<=N,A是正整数 将格子x,y里的数 ...

  5. HDU 4557 非诚勿扰(Treap找后继)

    非诚勿扰 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) Total Submi ...

  6. display:table布局总结

    1. table布局方式 2. table布局实际应用 效果: 代码: <!DOCTYPE html> <html lang="en"> <head& ...

  7. JavaScript内存分配

    1.栈内存和堆内存 栈内存为自动分配的内存空间,由系统自动释放堆内存是动态分配的内存,大小不固定,也不会自动释放 js的值类型直接分配在栈内存中,引用类型分配在堆内存中引用类型变量保存的是引用类型的指 ...

  8. LVS Mode&Method

    LVS NAT 模式: Summary: 普通的NAT模式为DNAT,即只更改目的地址,不改源端口. LVS在转发报文时,将Client的源IP透传给Server,类似于透明传输. 优点: 1. 可提 ...

  9. eclspse魔板

    window->prefence->搜索 template即可

  10. Idea下maven的配置和使用

    maven的主要功能就是依赖管理,jar包仓库.和C#中的NuGet仓库差不多.另外也提供打包构建,启动插件等功能.下面主要讲一下,在使用Idea开发时,maven的配置和使用. maven的安装和配 ...