Cod sursa(job #2544808)

Utilizator stormy_weatherelena cristina stormy_weather Data 12 februarie 2020 15:38:09
Problema Farfurii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.85 kb
#include<fstream>
#include<iostream>
using namespace std;

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

  int n;
  int64_t k;
  fin >> n >> k;

  int chosen = -1;
  for (int i = 1; i <= n; i++) {
    if ((int64_t) i * (i - 1) / 2 >= k) {
      chosen = i;
      break;
    }
  }
  // cout << chosen << "\n";
  if (chosen > - 1) {
    int64_t cur_inv = (int64_t) chosen * (chosen - 1) / 2;
    int bring_in_front = -1;
    if (cur_inv > k) {
      bring_in_front = n - (cur_inv - k);
    }
    for (int i = 1; i <= (n - chosen); i++)
      fout << i << " ";
    if (bring_in_front > -1) {
      fout << bring_in_front << " ";
      for (int i = n; i > (n - chosen); i--)
        if (i != bring_in_front)
          fout << i << " ";
    } else {
      for (int i = n; i > (n - chosen); i--)
        fout << i << " ";
    }
  }
  return 0;
}