Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- int main() {
- freopen("in.txt", "r", stdin);
- freopen("out.txt", "w", stdout);
- int f, q;
- std::cin >> f >> q;
- std::priority_queue<std::pair<int, int>> fileiras[f + 1], sem_assento;
- int n;
- std::cin >> n;
- std::string nomes[n + 1];
- int prioridades[n + 1];
- int cadastro_atual = 1;
- while(n--) {
- std::string type;
- std::cin >> type;
- if(type == "CAD") {
- std::string nome;
- int p;
- std::cin >> nome >> p;
- nomes[cadastro_atual] = nome;
- prioridades[cadastro_atual] = p;
- bool cadastrado = false;
- int fileira = -1;
- for(int i = 1; i <= f; i++) {
- if(fileiras[i].size() < q) {
- fileiras[i].push({-p, cadastro_atual});
- fileira = i;
- cadastrado = true;
- break;
- }
- }
- if(!cadastrado) {
- std::pair<int, int> mn = {INT_MAX, INT_MAX};
- int posicao = -1;
- for(int i = 1; i <= f; i++) {
- std::pair<int, int> tmp = fileiras[i].top();
- tmp.first = -tmp.first;
- if(tmp.first < mn.first) {
- mn = tmp;
- posicao = i;
- } else if(tmp.first == mn.first and tmp.second > mn.second) {
- mn = tmp;
- posicao = i;
- }
- }
- bool inserir = true;
- if(p < mn.first) {
- inserir = false;
- } else if(p == mn.first and cadastro_atual > mn.second) {
- inserir = false;
- }
- if(inserir) {
- cadastrado = true;
- fileira = posicao;
- sem_assento.push({mn.first, -mn.second});
- fileiras[posicao].pop();
- fileiras[posicao].push({-p, cadastro_atual});
- }
- }
- if(!cadastrado) {
- sem_assento.push({p, -cadastro_atual});
- }
- if(cadastrado) {
- std::cout << nome << " (" << cadastro_atual << ") foi alocado(a) na fileira " << fileira << "\n";
- } else {
- std::cout << nome << " (" << cadastro_atual << ") nao foi alocado(a) em nenhuma fileira\n";
- }
- cadastro_atual++;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement