Bagaimana cara mengatasi masalah ini untuk semua kendala

pemrograman


Petani John mempunyai N ekor sapi dalam satu baris (1≤N≤3⋅10^5). Sayangnya, ada penyakit yang menyebar ke mana-mana.
Awalnya, beberapa sapi mulai terinfeksi. Setiap malam, sapi yang terinfeksi menularkan penyakitnya ke sapi-sapi di kiri dan kanannya (jika ada). Sekali seekor sapi terinfeksi, ia tetap terinfeksi.
Setelah beberapa malam, Petani John menyadari bahwa masalahnya sudah tidak terkendali, jadi dia menguji sapinya untuk menentukan siapa yang mengidap penyakit tersebut. Temukan jumlah minimum sapi yang mungkin tertular penyakit.
MEMASUKKAN:
Baris pertama berisi N, jumlah sapi yang dimiliki Petani John.
Baris berikutnya berisi bitstring karakter N yang hanya terdiri dari 1 dan 0 dengan angka 1 mewakili sapi yang terinfeksi dan 0 mewakili sapi yang tidak terinfeksi setelah beberapa malam.
KELUARAN:
Keluarkan bilangan bulat tunggal: jumlah minimum sapi yang mungkin mulai sakit.
SAMPEL MASUKAN:
5
11111

KELUARAN SAMPEL:
1

Misalkan sapi tengah adalah satu-satunya sapi yang mulai terinfeksi. Kemudian sapi-sapi tersebut akan tertular dengan urutan sebagai berikut:
0 malam: 00100 (sapi ketiga pertama kali terinfeksi)
1 malam: -> 01110 (sapi kedua dan keempat sekarang tertular)
2 malam: -> 11111 (sapi pertama dan kelima sekarang tertular)
3 malam: -> 11111 (semua sapi sudah tertular, jadi tidak ada sapi tambahan yang tertular)
-> …

Setelah dua malam atau lebih, keadaan akhir sapi akan terlihat seperti masukan. Ada banyak keadaan awal dan jumlah malam lainnya yang dapat menghasilkan keadaan masukan, seperti:
0 malam: 10001
1 malam: -> 11011
2 malam: -> 11111

atau:
0 malam: 01001
1 malam: -> 11111

atau:
0 malam: 01000
1 malam: -> 11100
2 malam: -> 11110
3 malam: -> 11111

Semua negara bagian awal ini memiliki setidaknya satu sapi yang terinfeksi.
SAMPEL MASUKAN:
6
011101

KELUARAN SAMPEL:
4

Apa yang saya coba:

Saya telah mencoba 2 solusi berbeda, melewati beberapa titik tes, beberapa berbeda:
1.

#include <iostream>
#include <vector>

int calculate_infected_cows(const std::vector<int>& cow_states) {
    int total_infected = 0;
    int consecutive_ones = 0;

    for (int i = 0; i < cow_states.size(); ++i) {
        if (cow_states[i] == 1) {
            consecutive_ones++;
        } else {
            int current_sequence_length = consecutive_ones;
            consecutive_ones = 0;

            if (current_sequence_length == 1 || current_sequence_length == 2) {
                total_infected += current_sequence_length;
            } else if (current_sequence_length >= 3) {
                if (i + 1 < cow_states.size() && cow_states[i + 1] == 0) {
                    total_infected += (current_sequence_length + 1) / 2; // Adjusted logic
                } else {
                    total_infected += current_sequence_length;
                }
            } else {
                total_infected += current_sequence_length;
            }
        }
    }

    // Consider the last cow
    total_infected += consecutive_ones;

    // If all cows are infected, set total_infected to 1
    if (total_infected == cow_states.size()) {
        total_infected = 1;
    }

    return total_infected;
}

int main() {
    int n;
    std::cin >> n;

    std::vector<int> cow_states(n);
    for (int i = 0; i < n; ++i) {
        char c;
        std::cin >> c;
        cow_states[i] = c - '0';
    }

    int result = calculate_infected_cows(cow_states);
    std::cout << result << std::endl;

    return 0;
}

2.

#include <iostream>
#include <vector>

bool is_consecutive_ones(const std::vector<int>& cow_states, int index) {
    return index >= 2 && cow_states[index - 1] == 1 && cow_states[index - 2] == 1;
}

int calculate_infected_cows(const std::vector<int>& cow_states) {
    int total_infected = 0;
    int consecutive_ones = 0;
    bool override_total = false;

    for (int i = 0; i < cow_states.size(); ++i) {
        if (cow_states[i] == 1) {
            consecutive_ones++;
        } else {
            int current_sequence_length = consecutive_ones;
            consecutive_ones = 0;

            if (current_sequence_length == 1 || current_sequence_length == 2) {
                total_infected += current_sequence_length;
            } else if (current_sequence_length >= 3 && current_sequence_length % 2 == 1) {
                if (i + 1 < cow_states.size() && cow_states[i + 1] == 0) {
                    total_infected++;
                } else if (is_consecutive_ones(cow_states, i)) {
                    total_infected++;
                    override_total = true;
                } else {
                    total_infected += current_sequence_length;
                }
            } else {
                if (current_sequence_length == 3) {
                    if (i - 1 >= 0 && cow_states[i - 1] == 0 && i + 1 < cow_states.size() && cow_states[i + 1] == 0) {
                        total_infected++;
                    } else {
                        total_infected += current_sequence_length;
                    }
                } else {
                    total_infected += current_sequence_length;
                }
            }
        }
    }

    if (override_total) {
        total_infected += consecutive_ones;
    } else {
        total_infected += cow_states.back(); // Consider the last cow
    }

    return total_infected;
}

int main() {
    int n;
    std::cin >> n;

    std::vector<int> cow_states(n);
    for (int i = 0; i < n; ++i) {
        char c;
        std::cin >> c;
        cow_states[i] = c - '0';
    }

    int result = calculate_infected_cows(cow_states);
    std::cout << result << std::endl;

    return 0;
}


This is my basic logic, however I do believe it may be incorrect.

Logic for counting how many cows (n is a constant for a number of 1s):

Middle cases:
If there is a sequence of 1s with only a single 1 (a sequence is a continued string of 1s, separated by 0s from other 1s), count how many 1s there are to find total.
If there is a sequence of 2 1s, count how many 1s there are to find total
If there is a sequence of n 1s, where n is odd and n>=3, use the formula: x=(n+1)/2 to determine the day it is. Then apply the formula n=2x-1 to weed through all sequences of consecutive 1s to determine if the day count is correct.
If it is not correct (a sequence of an even number of consecutive 1s) or (an odd number of 1s not equating to the aforementioned string), count how many 1s there are to find total.
If it is correct and applies to all other sequences, add 1 to the total.

Special cases (on both ends):
If the bitstring starts with 10, erase current total and count how many 1s there are to find correct total.
If the bitstring ends in 01, erase current total and count how many 1s there are to find correct total.
If the bristring starts with 110, there may be 2 original cows, or only 1, depending on the middle sections.
If the bristring ends with 011, there may be 2 original cows, or only 1, depending on the middle sections.
If the bitstring starts with x0 (where z is a bitstring representing the number x of 1s), there may be x original cows or only 1, depending on the middle sections.
If the bitstring starts with 0x (where z is a bitstring representing the number x of 1s), there may be x original cows or only 1, depending on the middle sections.

Solusi 1

Anda harus menggunakan strategi “memecah belah dan menaklukkan” untuk menyelesaikan masalah tersebut. Untuk melakukannya, Anda harus menulis berbeda fungsi untuk setiap kriteria.
Yang terbaik adalah memikirkan beberapa struktur data seperti susunan sapi untuk data tempat fungsi Anda bekerja.
Daripada menulis tes untuk setiap fungsi dan menghilangkan bug. Tulis beberapa keluaran yang berguna, seperti cetakan semua status sapi sebagai teks yang diformat.

Jadi, Anda mungkin mengakhirinya dengan beberapa

C++
infect(Cows *cows, Cows *resulting, int pos)

fungsi yang melihat apakah pos terinfeksi dan kemudian menginfeksi pos sebelumnya dan selanjutnya di area yang dihasilkan. Jika tidak, Anda mungkin terinfeksi terlalu banyak. Bolehkah Anda menambahkan beberapa nilai hari_terinfeksi untuk men-debugnya dengan lebih baik.

Semoga berhasil dengan pekerjaan rumah Anda. Gunakan untuk meningkatkan keterampilan coding Anda seperti debugging.

コメント

タイトルとURLをコピーしました