zoukankan      html  css  js  c++  java
  • 【t034】Matrix67的派对

    Time Limit: 1 second
    Memory Limit: 1 MB

    【问题描述】

    Matrix67发现身高接近的人似乎更合得来。Matrix67举办的派对共有N(1<=N<=10)个人参加,Matrix67需要把他们安排在圆桌上。
    Matrix67的安排原则是,圆桌上任意两个相邻人的身高之差不能超过K。请告诉Matrix67他共有多少种安排方法。

    【输入格式】

    第一行输入两个用空格隔开的数N和K,其中1<=N<=10,1<=K<=1 000 000。
    第二行到第N+1行每行输入一个人的身高值。所有人的身高都是不超过1 000 000的正整数
    【输出格式】

    输出符合要求的安排总数

    Sample Input

    4 10
    2
    16
    6
    10

    Sample Output

    2

    【题目链接】:http://noi.qz5z.com/viewtask.asp?id=t034

    【题解】

    两种不同的安排方式;
    不能是通过旋转、或翻转变换而成的;
    那么我们可以先固定一个人的位置,然后枚举其他人和它的”相对位置”;这样就都是本质不同的方案了;根据相邻人不能一样,写个剪枝就好;
    枚举全排列就好;
    复杂度O(N!*n)
    能够在1s内出解.

    【完整代码】

    #include <cstdio>
    
    const int MAXN = 10+5;
    
    int n,k,ans=0;
    int a[MAXN],sta[MAXN];
    bool bo[MAXN];
    
    void dfs(int x)
    {
        if (x>n)
        {
            int t = sta[n]-sta[1];
            if (t<0) t = -t;
            if (t<=k)
                ans++;
            return;
        }
        for (int i = 1;i <= n;i++)
            if (!bo[i])
            {
                sta[x] = a[i];
                int t = sta[x]-sta[x-1];
                if (t<0) t = -t;
                if (t>k)
                    continue;
                bo[i] = true;
                dfs(x+1);
                bo[i] = false;
            }
    }
    
    int main()
    {
        //freopen("F:\rush.txt","r",stdin);
        scanf("%d%d",&n,&k);
        for (int i = 1;i <= n;i++)
            scanf("%d",&a[i]);
        sta[1] = a[1];
        bo[1] = true;
        dfs(2);
        printf("%d
    ",ans);
        return 0;
    }
  • 相关阅读:
    ixgbe dma 控制器
    per cpu
    HDU 4597 Play Game
    HDU 5115 Dire Wolf
    hdu 5900 QSC and Master
    CodeForces933A A Twisty Movement
    CodeForces 245H Queries for Number of Palindromes
    CodeForces596D Wilbur and Trees
    CodeForces509F Progress Monitoring
    CodeForces149D Coloring Brackets
  • 原文地址:https://www.cnblogs.com/AWCXV/p/7626666.html
Copyright © 2011-2022 走看看