Cod sursa(job #3231425)

Utilizator catalinaionela77Catalina Ionela Florescu catalinaionela77 Data 26 mai 2024 13:18:11
Problema Hotel Scor 0
Compilator c-64 Status done
Runda Arhiva de probleme Marime 1.68 kb
#include <stdio.h>
#include <stdlib.h>

// Structura pentru a reprezenta instructiunile
typedef struct {
    int type; // Tipul instrucțiunii: 1, 2 sau 3
    int start; // Numarul primei camere
    int members; // Numarul de membri ai grupului de turisti
} Instruction;

int main() {
    FILE *input = fopen("hotel.in", "r");
    FILE *output = fopen("hotel.out", "w");

    if (input == NULL || output == NULL) {
        printf("Nu s-a putut deschide fisierul!");
        return 1;
    }

    int N, P;
    fscanf(input, "%d %d", &N, &P);

    int *hotel = (int *)malloc((N + 1) * sizeof(int));
    for (int i = 0; i <= N; ++i) {
        hotel[i] = 0; // Initial, toate camerele sunt libere
    }

    int max_consecutive_length = N;
    for (int i = 0; i < P; ++i) {
        Instruction instr;
        fscanf(input, "%d", &instr.type);
        if (instr.type == 1 || instr.type == 2) {
            fscanf(input, "%d %d", &instr.start, &instr.members);
            if (instr.type == 1) {
                for (int j = instr.start; j < instr.start + instr.members; ++j) {
                    hotel[j] = 1; // Marcheaza camerele ocupate de grupul nou de turiști
                }
            } else {
                for (int j = instr.start; j < instr.start + instr.members; ++j) {
                    hotel[j] = 0; // Marcheaza camerele eliberate de grupul care pleaca
                }
            }
        } else if (instr.type == 3) {
            // Afiseaza lungimea maxima a secventei de camere libere
            fprintf(output, "%d\n", max_consecutive_length);
        }
    }

    fclose(input);
    fclose(output);
    free(hotel);

    return 0;
}