Advertisement
cosenza987

Untitled

Jan 13th, 2024
1,204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.55 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. int main() {
  4.     freopen("in.txt", "r", stdin);
  5.     freopen("out.txt", "w", stdout);
  6.     int f, q;
  7.     std::cin >> f >> q;
  8.     std::priority_queue<std::pair<int, int>> fileiras[f + 1], sem_assento;
  9.     int n;
  10.     std::cin >> n;
  11.     std::string nomes[n + 1];
  12.     int prioridades[n + 1];
  13.     int cadastro_atual = 1;
  14.     while(n--) {
  15.         std::string type;
  16.         std::cin >> type;
  17.         if(type == "CAD") {
  18.             std::string nome;
  19.             int p;
  20.             std::cin >> nome >> p;
  21.             nomes[cadastro_atual] = nome;
  22.             prioridades[cadastro_atual] = p;
  23.             bool cadastrado = false;
  24.             int fileira = -1;
  25.             for(int i = 1; i <= f; i++) {
  26.                 if(fileiras[i].size() < q) {
  27.                     fileiras[i].push({-p, cadastro_atual});
  28.                     fileira = i;
  29.                     cadastrado = true;
  30.                     break;
  31.                 }
  32.             }
  33.             if(!cadastrado) {
  34.                 std::pair<int, int> mn = {INT_MAX, INT_MAX};
  35.                 int posicao = -1;
  36.                 for(int i = 1; i <= f; i++) {
  37.                     std::pair<int, int> tmp = fileiras[i].top();
  38.                     tmp.first = -tmp.first;
  39.                     if(tmp.first < mn.first) {
  40.                         mn = tmp;
  41.                         posicao = i;
  42.                     } else if(tmp.first == mn.first and tmp.second > mn.second) {
  43.                         mn = tmp;
  44.                         posicao = i;
  45.                     }
  46.                 }
  47.                 bool inserir = true;
  48.                 if(p < mn.first) {
  49.                     inserir = false;
  50.                 } else if(p == mn.first and cadastro_atual > mn.second) {
  51.                     inserir = false;
  52.                 }
  53.                 if(inserir) {
  54.                     cadastrado = true;
  55.                     fileira = posicao;
  56.                     sem_assento.push({mn.first, -mn.second});
  57.                     fileiras[posicao].pop();
  58.                     fileiras[posicao].push({-p, cadastro_atual});
  59.                 }
  60.             }
  61.             if(!cadastrado) {
  62.                 sem_assento.push({p, -cadastro_atual});
  63.             }
  64.             if(cadastrado) {
  65.                 std::cout << nome << " (" << cadastro_atual << ") foi alocado(a) na fileira " << fileira << "\n";
  66.             } else {
  67.                 std::cout << nome << " (" << cadastro_atual << ") nao foi alocado(a) em nenhuma fileira\n";
  68.             }
  69.             cadastro_atual++;
  70.         }
  71.     }
  72.     return 0;
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement