Maximum Cinema Seat Allocation In C++

In this article, you will learn how to find maximum cinema seat allocation in C++.

Overview:

A movie theatre with many rows and a set number of seats in each row is given to you. There are N chairs in each row, and the positions are all sequential. With the limitation that a family requires 4 adjacent seats, you must arrange the chairs so that the largest number of people may be accommodated.

For ease,

  • The seats in a row are numbered from 1 to N.
  • Each family occupies exactly 4 seats.
  • Some seats may already be blocked or occupied, which means a family cannot sit there.
  • The problem is to find the maximum number of families that can be seated across all rows.
  • Approach:

This problem can be solved using efficient algorithms to calculate the maximum number of families. The main considerations are:

  • Identify seat blocks: Divide rows into sections where 4 consecutive seats are available.
  • Constraints: Blocked or occupied seats, which limit possible family placements.
  • Optimizing placement: Greedily assign families to valid segments.
  • Input Format:

  • R: Number of rows.
  • N: Number of seats per row.
  • B: Number of blocked seats.
  • A list of blocked seats, where each entry specifies the row and seat index.
  • Algorithm:

  • Data Representation: It represents each row as a vector or bitmask to quickly identify blocked seats and free regions.
  • Valid Seat Configurations: A family can sit in four seats at indices (x, x+1, x+2, x+3) if none of these are blocked. If N < 4, it is impossible to seat any family.
  • Iterate Through Rows: For each row, calculate: The total number of valid 4-seat groups. Ensure blocked seats do not overlap with these groups.
  • Optimization: Use bitmasks for faster checks of consecutive free seats. For each row, attempt to maximize families.
  • Output: Total families that can be seated across all rows.
  • A family can sit in four seats at indices (x, x+1, x+2, x+3) if none of these are blocked.
  • If N < 4, it is impossible to seat any family.
  • The total number of valid 4-seat groups.
  • Ensure blocked seats do not overlap with these groups.
  • Use bitmasks for faster checks of consecutive free seats.
  • For each row, attempt to maximize families.
  • Example:

Let us take an example to illustrate the maximum cinema seat allocation in C++ .

Example

#include <iostream>
#include <vector>
#include <unordered_map>
#include <set>

using namespace std;

// Function to calculate max families in one row
int maxFamiliesInRow(int N, set<int> &blockedSeats) 
{
    vector<int> seats(N + 1, 0); // Mark seats: 0 - free, 1 - blocked
    
    // Mark blocked seats
    for (int seat : blockedSeats) {
        if (seat >= 1 && seat <= N) {
            seats[seat] = 1;
        }
    }

    // Count valid groups of 4 adjacent free seats
    int families = 0;
    if (N >= 10) {  // Cinema seats typically have 10 seats per row
        bool leftBlock = seats[2] == 0 && seats[3] == 0 && seats[4] == 0 && seats[5] == 0;
        bool middleBlock = seats[4] == 0 && seats[5] == 0 && seats[6] == 0 && seats[7] == 0;
        bool rightBlock = seats[6] == 0 && seats[7] == 0 && seats[8] == 0 && seats[9] == 0;

        // Allocate families in non-overlapping sections
        if (leftBlock) {
            families++;
        }
        if (rightBlock) { 
            families++;
        } else if (!leftBlock && middleBlock) { 
            families++;
        }
    }
    return families;
}

// Main function to calculate max families for all rows
int maxCinemaFamilies(int R, int N, unordered_map<int, set<int>> &blockedSeats) 
{
    int totalFamilies = 0;
    for (int row = 1; row <= R; ++row) {
        set<int> blocked = blockedSeats[row]; // Get blocked seats for the row
        totalFamilies += maxFamiliesInRow(N, blocked);
    }
    return totalFamilies;
}

int main() 
{
    int R, N, B;
    cout << "Enter number of rows (R): ";
    cin >> R;
    cout << "Enter number of seats per row (N): ";
    cin >> N;
    cout << "Enter number of blocked seats (B): ";
    cin >> B;

    unordered_map<int, set<int>> blockedSeats; // Blocked seats for each row
    cout << "Enter blocked seats as row-seat pairs (e.g., 1 4):" << endl;
    for (int i = 0; i < B; ++i) {
        int row, seat;
        cin >> row >> seat;
        blockedSeats[row].insert(seat);
    }

    int result = maxCinemaFamilies(R, N, blockedSeats);
    cout << "Maximum number of families that can be seated: " << result << endl;
    return 0;
}

Output:

Input:

Example

Enter number of rows (R): 3
Enter number of seats per row (N): 10
Enter number of blocked seats (B): 4
Enter blocked seats as row-seat pairs (e.g., 1 4):
1 4
1 5
2 7
3 3

Output:

Output

Maximum number of families that can be seated: 6

Code Explanation:

  • Data Structures : unordered_map<int, set<int>>: It stores blocked seats for each row. vector<int>: It represents each row’s seating arrangement to mark free and blocked seats.
  • Key functions : maxFamiliesInRow: It calculates the maximum number of families that can fit in one row based on blocked seats. maxCinemaFamilies: It iterates through all rows and aggregates the maximum families across rows.
  • Input/Output: Input: Rows, seats per row, and blocked seat positions. Output: Maximum families that can be seated.
  • unordered_map<int, set<int>>: It stores blocked seats for each row.
  • vector<int>: It represents each row’s seating arrangement to mark free and blocked seats.
  • maxFamiliesInRow: It calculates the maximum number of families that can fit in one row based on blocked seats.
  • maxCinemaFamilies: It iterates through all rows and aggregates the maximum families across rows.
  • Input: Rows, seats per row, and blocked seat positions.
  • Output: Maximum families that can be seated.
  • Complexity Analysis:

  • Time Complexity: Iterating through rows: O(R)O(R)O(R). Processing blocked seats for a row: O(B)O(B)O(B) (for set operations). Checking configurations: O(N)O(N)O(N). Overall: O(R+B+N)O(R + B + N)O(R+B+N).
  • Space Complexity: Storage for blocked seats: O(B)O(B)O(B). Row-wise storage for seat status: O(N)O(N)O(N). Overall: O(B+N)O(B + N)O(B+N).
  • Iterating through rows: O(R)O(R)O(R).
  • Processing blocked seats for a row: O(B)O(B)O(B) (for set operations).
  • Checking configurations: O(N)O(N)O(N).
  • Overall: O(R+B+N)O(R + B + N)O(R+B+N).
  • Storage for blocked seats: O(B)O(B)O(B).
  • Row-wise storage for seat status: O(N)O(N)O(N).
  • Overall: O(B+N)O(B + N)O(B+N).
  • Example Walkthrough

Input:

  • Rows: R=3R = 3R=3
  • Seats per row: N=10N = 10N=10
  • Blocked seats: B=4B = 4B=4 (positions: (1, 4), (1, 5), (2, 2), (3, 9))

Output:

Maximum families = 444

Explanation:

  • Row 1: Blocked seats (4, 5) prevent families in the middle block. Left and right blocks are available → 222 families.
  • Row 2: Blocked seat (2) does not affect placement. Both left and right blocks are free → 222 families.
  • Row 3: Blocked seat (9) affects the right block. Only left block is free → 111 family. Total families = 2+2+1=42 + 2 + 1 = 42+2+1=4.
  • Optimizations:

  • Bitmasking: Instead of storing rows as vectors, use bitmasks for efficient space and time.
  • Parallel Processing: Rows are independent; computations can be parallelized.
  • Early Termination: If a row has too many blocked seats, skip unnecessary checks.
  • Conclusion:

In conclusion, this solution describes an efficient and scalable algorithm in C++ for the “Maximum Cinema Seat Allocation” problem. Combining modular design with careful optimizations ensures that the program is efficient in handling real-world constraints, such as large seating arrangements and hundreds of blocked seats.

Input Required

This code uses input(). Please provide values below: