• 洛谷 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 <= 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

    思路:

    40分暴力

    #include<cstdio>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    #define MAXN 17
    using namespace std;
    int N,K,ans;
    int num[MAXN],vis[MAXN];
    void dfs(int now,int pre){    
        if(now==N){
            ans++;
            return ;
        }
        for(int i=1;i<=N;i++)
            if(!vis[i]&&abs(num[pre]-num[i])>K){
                vis[i]=1;
                dfs(now+1,i);
                vis[i]=0;
            }
    }
    int main(){
        scanf("%d%d",&N,&K);
        for(int i=1;i<=N;i++)
            scanf("%d",&num[i]);
        dfs(0,-1);
        cout<<ans;
    }

    70分暴力:

    #include<cstdio>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    #define MAXN 17
    using namespace std;
    int N,K,ans;
    int num[MAXN],vis[MAXN];
    void dfs(int now,int pre){    
        if(now==N){
            ans++;
            return ;
        }
        for(int i=1;i<=N;i++)
            if(!vis[i]&&abs(pre-num[i])>K){
                vis[i]=1;
                dfs(now+1,num[i]);
                vis[i]=0;
            }
    }
    int main(){
        scanf("%d%d",&N,&K);
        for(int i=1;i<=N;i++)
            scanf("%d",&num[i]);
        dfs(0,-3400);
        cout<<ans;
    }

    100分记忆化搜索

    #include<cstdio>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    #define MAXN 17
    using namespace std;
    int N,K;
    int num[MAXN];
    bool vis[MAXN];
    long long f[1<<16][20];
    long long dfs(int sta,int cnt,int pre){
        if(cnt==N) return f[sta][pre]=1;
        if(f[sta][pre]!=-1)    return f[sta][pre];
        long long res=0;
        for(int i=1;i<=N;i++)
            if(!vis[i]&&abs(num[i]-num[pre])>K){
                vis[i]=1;
                res+=dfs(sta+(1<<(i-1)),cnt+1,i);
                vis[i]=0;
            }
        f[sta][pre]=res;
        return res;
    }
    int main(){
        memset(f,-1,sizeof(f));
        scanf("%d%d",&N,&K);
        for(int i=1;i<=N;i++)
            scanf("%d",&num[i]);
        num[0]=-3400;
        dfs(0,0,0);
        printf("%lld",f[0][0]);
    }
    细雨斜风作晓寒。淡烟疏柳媚晴滩。入淮清洛渐漫漫。 雪沫乳花浮午盏,蓼茸蒿笋试春盘。人间有味是清欢。
  • 相关阅读:
    在Android工程中运行Java程序问题
    sklearn.neighbors.kneighbors_graph的简单属性介绍
    python中的“.T”操作
    numpy中关于*和dot的区别
    一个Window Service引发的感想
    项目管理之初步认识
    由敏捷开发中开发认领自己的工作内容的感想
    SQL Server2008 inner join多种方式的实践
    浅谈业务逻辑和技术哪个更重要
    敏捷人生之初步认识
  • 原文地址:https://www.cnblogs.com/cangT-Tlan/p/8641629.html
Copyright © 2020-2023  润新知