117472024-11-08 17:41:19balintÁruszállítás üres szakaszaicpp17Wrong answer 2/5075ms1420 KiB
#include <iostream>
#include <vector>
#include <bitset>

// Function to count the number of zero groups in the given integer
int count_zero_groups_from_int(unsigned int number, int bit_width) {
    int count = 0;
    bool in_zero_group = false;

    // Iterate through the bits of the number from the most significant bit (MSB) to the least significant bit (LSB)
    for (int i = bit_width - 1; i >= 0; --i) {
        if ((number & (1 << i)) == 0) {
            if (!in_zero_group) {
                count++;  // Found a new group of zeros
                in_zero_group = true;
            }
        } else {
            // If the bit is 1, we're no longer in a zero group
            in_zero_group = false;
        }
    }

    return count;
}

// Function to set bits in the given range [start, start + amount - 1]
void set_bits(std::vector<unsigned int>& bit_array, size_t start, size_t amount) {
    unsigned int mask = (1U << amount) - 1;  // Create a mask with 'amount' number of 1s
    mask <<= start;  // Shift the mask to the 'start' position

    size_t index = start / 32;  // Which unsigned integer holds the bit
    bit_array[index] |= mask;  // Set the bits using bitwise OR
}

// Function to print the bits of the integer in binary format
void print_bits(const std::vector<unsigned int>& bit_array, int size) {
    for (int i = size - 1; i >= 0; --i) {
        size_t index = i / 32;  // Which unsigned integer holds the bit
        int bit = (bit_array[index] >> (i % 32)) & 1;  // Extract and print each bit
        std::cout << bit;
    }
    std::cout << std::endl;
}

int main() {
    unsigned int N, M;
    std::cin >> N >> M;  // Read the number of ranges and the bit width

    // Allocate memory for the bit array: each unsigned int holds 32 bits
    unsigned int num_elements = (N + 31) / 32;  // Number of unsigned ints needed
    std::vector<unsigned int> bit_array(num_elements, 0);  // Vector of unsigned ints initialized to 0

    int from, to;
    for (unsigned int i = 0; i < M; ++i) {  // Read M ranges and modify the bit array
        std::cin >> from >> to;  // Read the range (1-based indexing)
        set_bits(bit_array, from - 1, to - from);  // Set the bits from 'from' to 'to' (inclusive)
        //print_bits(bit_array, N);  // Print the bit array after modification
    }

    // Call the count_zero_groups_from_int function and print the result
    unsigned int number = bit_array[0];  // Convert bit array to a single unsigned integer (only works for small bit widths)
    int zero_groups = count_zero_groups_from_int(number, N);
    std::cout << zero_groups << std::endl;  // Print the number of zero groups

    return 0;
}
SubtaskSumTestVerdictTimeMemory
base2/50
1Accepted0/01ms320 KiB
2Wrong answer0/075ms1336 KiB
3Accepted2/21ms320 KiB
4Wrong answer0/21ms508 KiB
5Wrong answer0/21ms320 KiB
6Wrong answer0/21ms320 KiB
7Wrong answer0/21ms320 KiB
8Wrong answer0/21ms508 KiB
9Wrong answer0/21ms320 KiB
10Wrong answer0/21ms320 KiB
11Wrong answer0/21ms320 KiB
12Wrong answer0/21ms320 KiB
13Wrong answer0/34ms508 KiB
14Wrong answer0/37ms424 KiB
15Wrong answer0/34ms320 KiB
16Wrong answer0/359ms1032 KiB
17Wrong answer0/361ms1076 KiB
18Wrong answer0/371ms1420 KiB
19Wrong answer0/37ms320 KiB
20Wrong answer0/38ms468 KiB
21Wrong answer0/368ms1336 KiB
22Wrong answer0/371ms1292 KiB