前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >P2915 [USACO08NOV]奶牛混合起来Mixed Up Cows

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

作者头像
attack
发布2018-04-12 12:15:17
7890
发布2018-04-12 12:15:17
举报
文章被收录于专栏:数据结构与算法

题目描述

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:

代码语言:javascript
复制
4 1 
3 
4 
2 
1 

输出样例#1:

代码语言:javascript
复制
2 

说明

The 2 possible Mixed Up arrangements are:

3 1 4 2

2 4 1 3

变量名冲突调了好久,,,

状压dp。

代码语言:javascript
复制
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<cmath>
 5 #include<algorithm>
 6 #define lli long long int 
 7 using namespace std;
 8 const int maxn=0x7ffffff;
 9 int n,k1;
10 int a[17];
11 long long  dp[16][1<<16];
12 int main()
13 {
14     scanf("%d%d",&n,&k1);
15     for(int i=0;i<n;i++)
16         scanf("%d",&a[i]);
17     for(int i=0;i<n;i++)
18         dp[i][1<<i]=1;
19     for(int i=0;i<(1<<n);i++)
20         for(int j=0;j<n;j++)
21             if(i & (1<<j))
22                 for(int k=0;k<n;k++)
23                     if(!(i&(1<<k))&&abs(a[j]-a[k])>k1)
24                         dp[k][i|(1<<k)]+=dp[j][i];
25     long long ans=0;
26     for(int i=0;i<n;i++)
27         ans+=dp[i][(1<<n)-1];
28     printf("%lld",ans);
29     return 0;
30 }
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2017-07-18 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 题目描述
  • 输入输出格式
  • 输入输出样例
  • 说明
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档