Advertisement
Guest User

Untitled

a guest
Sep 15th, 2019
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.61 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. #define endl '\n'
  4. using namespace std;
  5.  
  6. typedef struct carros {
  7. string placa;
  8. int d;
  9.  
  10. carros() {
  11. this->placa = "oba";
  12. this->d = -1;
  13. }
  14.  
  15. } carros;
  16.  
  17. double VagaInicial(string placa, int tamanhoEs) //CALCULA A POSICAO "INICIAL" DO CARRO;
  18. {
  19. int posicao = (placa[0] - (int) 'A') + ((placa[1] - (int) 'A') * 26) + (placa[2] - (int) 'A') * pow(26, 2) +
  20. (placa[3] - (int) 'A') * pow(26, 3) + (placa[4] - (int) 'A') * pow(26, 4) +
  21. (placa[5] - (int) 'A') * pow(26, 5);
  22.  
  23. return posicao % tamanhoEs;
  24. }
  25.  
  26. carros *realocacao(carros *x, int novoTam, bool change) //REALOCA OS CARROS EM UM NOVO VETOR;
  27. {
  28. carros *newEstacionamento = new carros[novoTam]; //CRIA UM NOVO VETOR COM O DOBRO DO TAMANHO
  29. int index;
  30.  
  31. if(change == true) index = novoTam/2; //SE FOR DOBRAR O ARRAY
  32. else index = novoTam; //SE FOR DIVIDIR O ARRAY
  33.  
  34. for (int i = 0; i < index; i++) {
  35.  
  36. int aux = VagaInicial(x[i].placa,
  37. novoTam); //CALCULA A NOVA POSICAO COM BASE NO NOVO TAMANHO DO ESTACIONAMENTO
  38.  
  39. if (newEstacionamento[aux].d != -1) newEstacionamento[aux] = x[i];
  40. else {
  41. while (newEstacionamento[aux].d != -1) {
  42. aux = (aux+1)%novoTam;
  43. x[i].d += 1; //INCREMENTA O DESLOCAMENTO DO CARRO EM 1;
  44. }
  45. newEstacionamento[aux] = x[i];
  46. }
  47. }
  48. return newEstacionamento; //DEVERIA RETORNAR UM ARRAY
  49. }
  50.  
  51. int main() {
  52. ios::sync_with_stdio(false);
  53. cin.tie(0);
  54.  
  55. int Mmin, Fmin, Fmax; //DADOS INICIAIS
  56. cin >> Mmin >> Fmin >> Fmax;
  57.  
  58. carros *estacionamento = new carros[Mmin];
  59.  
  60. int vagasOcupadas = 0; //REALMENTE OCUPADAS + INDISPONIVEIS
  61. int vagasIndisp = 0; //VAGAS INDISPONIVEIS
  62. int m = Mmin; //VAGAS ATUAIS
  63.  
  64. string comando;
  65. cin >> comando;
  66.  
  67. while (comando.compare("END") != 0) {
  68. string placa;
  69. cin >> placa; //PEGA A PLACA DO CARRO
  70.  
  71. if (comando[0] == 'I') //ENTRADA DE CARRO
  72. {
  73. if (100 * (vagasOcupadas+vagasIndisp) > m * Fmax) {
  74. m *= 2;
  75. estacionamento = realocacao(estacionamento, m, true); //RECEBE UM NOVO ARRAY
  76. vagasIndisp = 0;
  77. }
  78.  
  79. carros a; //CRIA UM OBJ DO TIPO CARROS
  80. a.placa = placa;
  81. a.d = 0;
  82.  
  83. int aux = VagaInicial(a.placa, m);
  84.  
  85. if (estacionamento[aux].d == -1) {
  86. estacionamento[aux] = a; //VE SE A VAGA INICIAL ESTA DISPONIVEL P OCUPACAO
  87. cout << aux << " " << a.d << endl;
  88.  
  89. vagasOcupadas++;
  90.  
  91. } else {
  92. while (estacionamento[aux].placa == "oba") {
  93. a.d++;
  94. aux = (aux+1)%m;
  95. }
  96. estacionamento[aux] = a;
  97. cout << aux << " " << a.d << endl;
  98.  
  99. vagasOcupadas++;
  100. }
  101. } else if (comando[0] == 'S') //PROCURA O CARRO
  102. {
  103.  
  104. int aux = VagaInicial(placa, m);
  105.  
  106. if (estacionamento[aux].placa == placa) {
  107. cout << aux << " " << estacionamento[aux].d << endl;
  108.  
  109. } else {
  110. bool achou = false;
  111. while (!achou) {
  112.  
  113. int aux = (aux+1)%m;
  114. if (estacionamento[aux].placa == placa) {
  115. cout << aux << " " << estacionamento[aux].d << endl;
  116.  
  117. achou = true;
  118.  
  119. } else if (estacionamento[aux].d == -1) {
  120.  
  121. cout << "-1 -1 -1 -1" << endl;
  122. achou = true;
  123. }
  124. }
  125. }
  126. } else { //REMOVE O CARRO
  127. int aux = VagaInicial(placa, m);
  128.  
  129. if (estacionamento[aux].placa == placa) {
  130. cout << aux << " " << estacionamento[aux].d << endl;
  131. estacionamento[aux].placa = "off"; //TORNA A VAGA INDISPONIVEL
  132.  
  133. vagasIndisp++;
  134. vagasOcupadas--;
  135.  
  136. } else {
  137. bool achou = false;
  138.  
  139. while (!achou) {
  140. aux = (aux+1)%m;
  141.  
  142. if (estacionamento[aux].placa == placa) {
  143.  
  144. cout << aux << " " << estacionamento[aux].d << endl;
  145. estacionamento[aux].placa = "off"; //TORNA A VAGA INDISPONIVEL
  146. achou = true;
  147.  
  148. vagasIndisp++;
  149. vagasOcupadas--;
  150. }
  151. }
  152. }
  153. if(100*vagasOcupadas < m*Fmin){
  154. m = max(m/2, m);
  155. estacionamento = realocacao(estacionamento, m, false); //RECEBE UM NOVO ARRAY
  156. vagasIndisp = 0;
  157. }
  158. }
  159. cin >> comando;
  160. }
  161. return 0;
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement