Cod sursa(job #1988340)

Utilizator laurageorgescuLaura Georgescu laurageorgescu Data 2 iunie 2017 18:59:51
Problema Grigo Scor 10
Compilator cpp Status done
Runda Simulare 13 Marime 0.78 kb
#include <fstream>
#include <algorithm>

using namespace std;

ifstream fin ("grigo.in"); ofstream fout ("grigo.out");

const int nmax = 1e5;
int v[nmax + 1];
bool ok[nmax + 1];

int main() {
    int n, m;

    fin >> n >> m;
    for (int i = 0; i < m; ++ i) {
        int x;
        fin >> x;
        ok[ x ] = 1;
    }

    for (int i = 1; i <= n; ++ i) {
        v[ i ] = i;
    }

    int ans = 0;

    do {
        bool fl = 1;
        int mx = 0;
        for (int i = 1; i <= n; ++ i) {
            if (mx < v[ i ]) {
                if (ok[ i ] == 0) fl = 0;
                mx = v[ i ];
            } else {
                if (ok[ i ] == 1) fl = 0;
            }
        }

        if (fl) ++ ans;
    } while (next_permutation(v + 1, v + n + 1));

    fout << ans << "\n";
    return 0;
}