ACM (TJU) : 2792


#include <iostream>
#include <algorithm>
using namespace std;

int main ()
{
    int n;
    int k;
    cin >> n >> k;

    int a [10];
    int input [10];
    int count = 0;

    for ( int i = 0; i < n; i++ ) {
        cin >> input [i];
        a [i] = i;
    }

    do {
        if ( a [0] == 1 )
        break;
        bool flag = true;
        int diff;
        for ( int i = 0; i < n; i++ ) {
            if ( i + 1 == n )
            diff = input [a [0]] - input [a [i]];
            else
            diff = input [a [i]] - input [a [i+1]];
            if ( diff < 0 )
            diff *= -1;
            if ( diff > k ) {
                flag = false;
                break;
            }
        }

        if ( flag )
        count++;
        //cout << a [0] << a [1] << a [2] << a [3] << a [5] << a [6] << a [7] << a [8] << a [9] << endl;
    } while ( next_permutation (a, a + n));

    cout << count << endl;

    return 0;
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s