Medeali's blog

By Medeali, history, 6 months ago, In English

https://arena.moi/problem/moi21qualnones this is my code #include

include

using namespace std;

int main() { int t; cin>>t; while(t--){ string n; long long a; cin>>n>>a; long long o=0,s=0; for(char i : n){ if(i=='1') s++; if(s==a){ o++; s=0; } } cout<<o<<endl; } return 0; } i am struggling in finding the issue

  • Vote: I like it
  • 0
  • Vote: I do not like it

»
6 months ago, # |
  Vote: I like it 0 Vote: I do not like it

Seems like you forgot to reset s when you encounter a '0'.

Also next time please format your code like this so it's readable:

~~~~~
Your code here...
~~~~~

»
6 months ago, # |
Rev. 7   Vote: I like it 0 Vote: I do not like it

here is your solution

int minOperations(string s, int N) {
    int operations = 0;

    for (int i = 0; i < s.size(); ++i) {
        if (s[i] == '1') {
            int onesCount = 0;
            for (int j = i; j < min(i + N, int(s.size())); ++j) {
                if (s[j] == '1') {
                    onesCount++;
                }
            }
            if (onesCount == N) {
                operations++;
                for (int j = i; j < min(i + N, int(s.size())); ++j) {
                    s[j] = (s[j] == '0') ? '1' : '0';
                }
            }
        }
    }

    return operations;
}

int main() {

    int t;
    cin >> t;

    while (t--) {
        string s;
        int N;
        cin >> s >> N;

        int result = minOperations(s, N);
        cout << result << endl;
    }

    return 0;
}

or you can just use

while (t--) {
        string s;
        int N;
        cin >> s >> N;

        int operations = 0;
        for (int i = 0; i < s.size(); ++i) {
            if (i + N <= s.size() && s[i] == '1') {
                for (int j = i; j < i + N; ++j) {
                    s[j] = (s[j] == '0') ? '1' : '0';
                }
                operations++;
            }
        }

        cout << operations << endl;
    }

in the main function