Cod sursa(job #2796404)

Utilizator db_123Balaban David db_123 Data 8 noiembrie 2021 00:00:42
Problema Combinari Scor 80
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.45 kb
#include <fstream>
#include <stack>
#include <unordered_map>

///  this gets only 80 / 100
///  time limits exceeded

using namespace std;
ifstream cin("combinari.in");
ofstream cout("combinari.out");

stack<int> st;
unordered_map<int,bool> fr;

int LIMIT_SOLUTION_SPACE=0;
int n,k;

bool isOK(int nr)
{
    if(st.size()>= LIMIT_SOLUTION_SPACE)
        return false;

    /// number does not exists already in stack
    if(fr[nr])
        return false;

    /// number to be added should be greater then the last one added
    if(st.empty())
        return true;
    else if (nr > st.top())
        return true;
    else
        return false;

    return true;
}
void displayContent()
{
    for(int i = 0; i <= n; i++)
        if(fr[i])
        cout << i << " ";
    cout << '\n';
}

/// backtracking CHOOSE CONSTRAINT GOAL
/// choose explore unchoose
void back()
{
    /// base condition
    if(st.size()== LIMIT_SOLUTION_SPACE)
    {
        displayContent();
        return;
    }

    /// recursive function

    ///navigate through all solutions
    for(int i = 1 ; i <= n ; i++)
    {
        if(isOK(i))     /// constraint
        {
            st.push(i); /// choose
            fr[i] =  true;
            back();     /// explore
            st.pop();   /// unchoose
            fr[i] =  false;
        }
    }
}
int main()
{
    cin>>n>>k;
    LIMIT_SOLUTION_SPACE = k;

    back();
    return 0;
}