117472024-11-08 17:41:19balintÁruszállítás üres szakaszaicpp17Hibás válasz 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;
}
RészfeladatÖsszpontTesztVerdiktIdőMemória
base2/50
1Elfogadva0/01ms320 KiB
2Hibás válasz0/075ms1336 KiB
3Elfogadva2/21ms320 KiB
4Hibás válasz0/21ms508 KiB
5Hibás válasz0/21ms320 KiB
6Hibás válasz0/21ms320 KiB
7Hibás válasz0/21ms320 KiB
8Hibás válasz0/21ms508 KiB
9Hibás válasz0/21ms320 KiB
10Hibás válasz0/21ms320 KiB
11Hibás válasz0/21ms320 KiB
12Hibás válasz0/21ms320 KiB
13Hibás válasz0/34ms508 KiB
14Hibás válasz0/37ms424 KiB
15Hibás válasz0/34ms320 KiB
16Hibás válasz0/359ms1032 KiB
17Hibás válasz0/361ms1076 KiB
18Hibás válasz0/371ms1420 KiB
19Hibás válasz0/37ms320 KiB
20Hibás válasz0/38ms468 KiB
21Hibás válasz0/368ms1336 KiB
22Hibás válasz0/371ms1292 KiB