Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define endl '\n'
- using namespace std;
- typedef struct carros {
- string placa;
- int d;
- carros() {
- this->placa = "oba";
- this->d = -1;
- }
- } carros;
- double VagaInicial(string placa, int tamanhoEs) //CALCULA A POSICAO "INICIAL" DO CARRO;
- {
- int posicao = (placa[0] - (int) 'A') + ((placa[1] - (int) 'A') * 26) + (placa[2] - (int) 'A') * pow(26, 2) +
- (placa[3] - (int) 'A') * pow(26, 3) + (placa[4] - (int) 'A') * pow(26, 4) +
- (placa[5] - (int) 'A') * pow(26, 5);
- return posicao % tamanhoEs;
- }
- carros *realocacao(carros *x, int novoTam, bool change) //REALOCA OS CARROS EM UM NOVO VETOR;
- {
- carros *newEstacionamento = new carros[novoTam]; //CRIA UM NOVO VETOR COM O DOBRO DO TAMANHO
- int index;
- if(change == true) index = novoTam/2; //SE FOR DOBRAR O ARRAY
- else index = novoTam; //SE FOR DIVIDIR O ARRAY
- for (int i = 0; i < index; i++) {
- int aux = VagaInicial(x[i].placa,
- novoTam); //CALCULA A NOVA POSICAO COM BASE NO NOVO TAMANHO DO ESTACIONAMENTO
- if (newEstacionamento[aux].d != -1) newEstacionamento[aux] = x[i];
- else {
- while (newEstacionamento[aux].d != -1) {
- aux = (aux+1)%novoTam;
- x[i].d += 1; //INCREMENTA O DESLOCAMENTO DO CARRO EM 1;
- }
- newEstacionamento[aux] = x[i];
- }
- }
- return newEstacionamento; //DEVERIA RETORNAR UM ARRAY
- }
- int main() {
- ios::sync_with_stdio(false);
- cin.tie(0);
- int Mmin, Fmin, Fmax; //DADOS INICIAIS
- cin >> Mmin >> Fmin >> Fmax;
- carros *estacionamento = new carros[Mmin];
- int vagasOcupadas = 0; //REALMENTE OCUPADAS + INDISPONIVEIS
- int vagasIndisp = 0; //VAGAS INDISPONIVEIS
- int m = Mmin; //VAGAS ATUAIS
- string comando;
- cin >> comando;
- while (comando.compare("END") != 0) {
- string placa;
- cin >> placa; //PEGA A PLACA DO CARRO
- if (comando[0] == 'I') //ENTRADA DE CARRO
- {
- if (100 * (vagasOcupadas+vagasIndisp) > m * Fmax) {
- m *= 2;
- estacionamento = realocacao(estacionamento, m, true); //RECEBE UM NOVO ARRAY
- vagasIndisp = 0;
- }
- carros a; //CRIA UM OBJ DO TIPO CARROS
- a.placa = placa;
- a.d = 0;
- int aux = VagaInicial(a.placa, m);
- if (estacionamento[aux].d == -1) {
- estacionamento[aux] = a; //VE SE A VAGA INICIAL ESTA DISPONIVEL P OCUPACAO
- cout << aux << " " << a.d << endl;
- vagasOcupadas++;
- } else {
- while (estacionamento[aux].placa == "oba") {
- a.d++;
- aux = (aux+1)%m;
- }
- estacionamento[aux] = a;
- cout << aux << " " << a.d << endl;
- vagasOcupadas++;
- }
- } else if (comando[0] == 'S') //PROCURA O CARRO
- {
- int aux = VagaInicial(placa, m);
- if (estacionamento[aux].placa == placa) {
- cout << aux << " " << estacionamento[aux].d << endl;
- } else {
- bool achou = false;
- while (!achou) {
- int aux = (aux+1)%m;
- if (estacionamento[aux].placa == placa) {
- cout << aux << " " << estacionamento[aux].d << endl;
- achou = true;
- } else if (estacionamento[aux].d == -1) {
- cout << "-1 -1 -1 -1" << endl;
- achou = true;
- }
- }
- }
- } else { //REMOVE O CARRO
- int aux = VagaInicial(placa, m);
- if (estacionamento[aux].placa == placa) {
- cout << aux << " " << estacionamento[aux].d << endl;
- estacionamento[aux].placa = "off"; //TORNA A VAGA INDISPONIVEL
- vagasIndisp++;
- vagasOcupadas--;
- } else {
- bool achou = false;
- while (!achou) {
- aux = (aux+1)%m;
- if (estacionamento[aux].placa == placa) {
- cout << aux << " " << estacionamento[aux].d << endl;
- estacionamento[aux].placa = "off"; //TORNA A VAGA INDISPONIVEL
- achou = true;
- vagasIndisp++;
- vagasOcupadas--;
- }
- }
- }
- if(100*vagasOcupadas < m*Fmin){
- m = max(m/2, m);
- estacionamento = realocacao(estacionamento, m, false); //RECEBE UM NOVO ARRAY
- vagasIndisp = 0;
- }
- }
- cin >> comando;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement