Cod sursa(job #3132409)

Utilizator omaclearuMacelaru Octavian Andrei omaclearu Data 22 mai 2023 18:35:52
Problema Farfurii Scor 90
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.97 kb
#include <iostream>
#include <fstream>

using namespace std;

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

class FarfuriiSolver {
private:
    int n, k, x;

    void calculateX() {
        x = 1;
        while (x * (x + 1) / 2 < k)
            x++;
        x++;
    }

    void afisMaiMiciX(int x) {
        for (int i = 1; i <= n - x; i++)
            fout << i << " ";
    }

    void afisMaiMariX(int x, int nr) {
        for (int i = n; i > n - x; i--)
            if (i != nr)
                fout << i << " ";
    }

public:

    void solveProblem() {
        fin >> n >> k;

        calculateX();
        
        afisMaiMiciX(x);

        if (k == x * (x - 1) / 2) {
            for (int i = n; i > n - x; i--)
                fout << i << " ";
            return;
        }

        int nr = n - (x * (x - 1) / 2 - k);
        fout << nr << " ";
        afisMaiMariX(x, nr);
    }
};

int main() {
    FarfuriiSolver solver;
    solver.solveProblem();
    return 0;
}