首页 > 其他 > 详细

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

时间:2019-06-16 20:38:55      阅读:109      评论:0      收藏:0      [点我收藏+]

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.

遇到这种状压题目,为了记录序列,只要记录前一位就好了

#include<bits/stdc++.h>
using namespace std;
int n,k,a[20];
long long f[100001][20];
int main(){
    cin>>n>>k;
    int i,j,l;
    for(i=1;i<=n;i++)scanf("%d",&a[i]);
    int m=(1<<n)-1;
    for(i=1;i<=n;i++)f[1<<(i-1)][i]=1;
    for(i=0;i<=m;i++){
        for(j=1;j<=n;j++){
            if(i&(1<<(j-1))){
                for(l=1;l<=n;l++){
                    if(!(i&(1<<(l-1)))&&abs(a[l]-a[j])>k)
                    f[i|(1<<(l-1))][l]+=f[i][j];
                }
            }
        
        }
    }
    long long s=0;
    for(i=1;i<=n;i++)s+=f[m][i];
    cout<<s<<endl;
    return 0;
}

 

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

原文:https://www.cnblogs.com/zbsakioi/p/11032678.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!