题意:给你n个数,求有多少种全排列,使得相邻两个数差的绝对值>k

   (n<=15)

 

信心满满:这回不会再死了。。。。(状压DP啊)

然而。。。。(TM我不会写

依然写了个dfs70分(算是不错了QAQ)

正解:以f[i][j]代表当前排列最后一个是i,j代表状态(排列中的数选/没选)

#include<cstdio>
#include<iostream>
#include<cstring>
#include<cctype>
#include<algorithm>
using namespace std;
#define int long long
#define olinr return
#define _ 0
#define love_nmr 0
#define DB double
inline int read()
{
    int x=0,f=1;
    char ch=getchar();
    while(!isdigit(ch))
    {
        if(ch=='-')
            f=-f;
        ch=getchar();
    }
    while(isdigit(ch))
    {
        x=(x<<1)+(x<<3)+(ch^48);
        ch=getchar();
    }
    return x*f;
}
inline void put(int x)
{
    if(x<0)
    {
        x=-x;
        putchar('-');
    }
    if(x>9)
        put(x/10);
    putchar(x%10+'0');
}
int a[18];
int n;
int k;
signed main()
{
    n=read();
    k=read();
    int ans=0;
    for(int i=1;i<=n;i++)
        a[i]=read();
    sort(a+1,a+n+1);
    do
    {
        for(int i=2;i<=n;i++)
            if(abs(a[i]-a[i-1])<=k) goto wmy;
        ans++;
        wmy:;
    }while(next_permutation(a+1,a+n+1)); //纯暴力啊
    put(ans);
    olinr ~~(0^_^0)+love_nmr;
}
暴力1:50pts

相关文章:

  • 2022-01-29
  • 2022-12-23
  • 2022-02-17
  • 2022-12-23
  • 2022-12-23
  • 2021-08-26
  • 2021-04-14
  • 2022-02-06
猜你喜欢
  • 2021-09-27
  • 2021-07-28
  • 2021-12-13
  • 2021-09-03
  • 2021-09-02
相关资源
相似解决方案