मैं सभी बाधाओं के बावजूद इस समस्या का समाधान कैसे करूँ?


किसान जॉन के पास एक पंक्ति में N गायें हैं (1≤N≤3⋅10^5)। दुर्भाग्य से, चारों ओर एक बीमारी फैल रही है।
प्रारंभ में, कुछ गायें संक्रमित होने लगती हैं। हर रात, एक संक्रमित गाय बायीं और दायीं ओर (यदि मौजूद हो) गायों में बीमारी फैलाती है। एक बार जब गाय संक्रमित हो जाती है, तो वह संक्रमित ही रहती है।
कुछ रातों के बाद, किसान जॉन को एहसास हुआ कि समस्या नियंत्रण से बाहर हो गई है, इसलिए वह यह निर्धारित करने के लिए अपनी गायों का परीक्षण करता है कि बीमारी किसे है। गायों की न्यूनतम संख्या ज्ञात कीजिए जिनकी बीमारी की शुरुआत हो सकती है।
इनपुट:
पहली पंक्ति में N शामिल है, किसान जॉन के पास गायों की संख्या।
अगली पंक्ति में केवल 1 और 0 की एन अक्षर बिटस्ट्रिंग है जहां 1 एक संक्रमित गाय का प्रतिनिधित्व करता है और 0 कुछ रातों के बाद एक असंक्रमित गाय का प्रतिनिधित्व करता है।
आउटपुट:
एक पूर्णांक आउटपुट करें: गायों की न्यूनतम संख्या जो बीमारी से शुरू हो सकती थी।
नमूना इनपुट:
5
11111

नमूना आउटपुट:
1

मान लीजिए कि बीच वाली गाय ही एकमात्र ऐसी गाय थी जो संक्रमित होने लगी। फिर गायें निम्नलिखित क्रम में संक्रमित होंगी:
0 रातें: 00100 (तीसरी गाय प्रारंभ में संक्रमित है)
1 रात:-> 01110 (अब दूसरी और चौथी गायें संक्रमित हैं)
2 रातें:-> 11111 (पहली और पांचवीं गायें अब संक्रमित हैं)
3 रातें: -> 11111 (सभी गायें पहले से ही संक्रमित थीं, इसलिए कोई अतिरिक्त गाय संक्रमित नहीं हैं)
->…

दो या अधिक रातों के बाद, गायों की अंतिम स्थिति इनपुट की तरह दिखेगी। ऐसी कई अन्य प्रारंभिक अवस्थाएँ और रातों की संख्या हैं जो इनपुट अवस्था उत्पन्न कर सकती थीं, जैसे:
0 रातें: 10001
1 रात:-> 11011
2 रातें:->11111

या:
0 रातें: 01001
1 रात:-> 11111

या:
0 रातें: 01000
1 रात:->11100
2 रातें:->11110
3 रातें:-> 11111

इन सभी प्रारंभिक अवस्थाओं में कम से कम एक संक्रमित गाय है।
नमूना इनपुट:
6
011101

नमूना आउटपुट:
4

मैंने क्या प्रयास किया है:

मैंने 2 अलग-अलग समाधान आज़माए हैं, कुछ परीक्षण बिंदु पास किए हैं, कुछ अलग:
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.

समाधान 1

आपको उस समस्या को हल करने के लिए कुछ “फूट डालो और जीतो” रणनीति का उपयोग करना चाहिए। ऐसा करने के लिए आपको लिखना चाहिए अलग हर मानदंड के लिए कार्य करता है।
जिस डेटा पर आपके फ़ंक्शन काम कर रहे हैं, उसके लिए कुछ डेटा संरचना जैसे गायों की एक श्रृंखला के बारे में सोचना सबसे अच्छा है।
प्रत्येक फ़ंक्शन के लिए परीक्षण लिखें और बग्स को समाप्त करें। कुछ उपयोगी आउटपुट लिखें, जैसे सभी गायों के कुछ प्रिंट स्वरूपित पाठ के रूप में बताए गए हैं।

तो आप कुछ के साथ समाप्त कर सकते हैं

सी++
infect(Cows *cows, Cows *resulting, int pos)

फ़ंक्शन जो देखता है कि क्या पॉज़ संक्रमित है और परिणामी क्षेत्र में पहले और बाद में संक्रमित करता है। अन्यथा आप बहुत अधिक संक्रमित हो सकते हैं। क्या आप इसे बेहतर ढंग से डीबग करने के लिए कुछ दिन_संक्रमित मान जोड़ सकते हैं।

आपके गृहकार्य में शुभकामनाएँ. डिबगिंग जैसे अपने कोडिंग कौशल को बेहतर बनाने के लिए इसका उपयोग करें।

コメント

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