CHARGES - Editorial

#include
using namespace std;

int main() {
// your code goes here
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin>>t;
while(t–){
long long int n,k;
cin>>n>>k;
if(n==1){
cout<<“0”<<"\n";
continue;
}
string s;
cin>>s;
long long int sum=0;
for(long long int i=0;i<=n-2;i++){
if(s[i]!=s[i+1])
sum=sum+1;
else
sum=sum+2;
}

    for(long long int i=0;i<k;i++){
        long long int a;
        cin>>a;
        if(a==1){
           if(s[0]==s[1])
             sum=sum-1;
           else
            sum=sum+1;
        }
       else if(a==n){
           if(s[n-2]==s[n-1])
             sum=sum-1;
           else
             sum=sum+1;
       }
       else{
          if(s[a-1]==s[a-2])
            sum=sum-1;
       	  else
       	   sum=sum+1;
       	   
       	  if(s[a-1]==s[a])
       	    sum=sum-1;
       	  else
       	    sum=sum+1;
           
       }
       if(s[a-1]=='0')
         s[a-1]='1';
        else
         s[a-1]='0';
       
       cout<<sum<<"\n";
           
    }
    
    
}
return 0;

}

Can somebody tell me what is wrong with this code on submission?

`#include<bits/stdc++.h>

using namespace std;

#define loop(i,n) for(int i=0;i<n;i++)
#define ll long long
#define scan(arr) for(auto &it:arr)cin>>it;
#define print(a) {for(auto it:a)cout<<it<<" ";cout<<endl;}

int main() {
int t;
cin>>t;
while(t–)
{
int n,k;
cin>>n>>k;
string s;
cin>>s;
int dist=0;
// cout<<s<<endl;
for(int i=1;i<n;i++)
{
if(s[i]!=s[i-1])
dist++;
else
dist+=2;
}
// cout<<s<<endl;
for(int i=1;i<=k;i++)
{
int q;
cin>>q;
if(q==1&&s[q]==s[q-1])
{
dist–;
}
else if(q==1&&s[q]!=s[q-1])
{
dist++;
}
else if(q==n&&s[q-1]!=s[q-2])
{
dist++;
}
else if(q==n&&s[q-1]==s[q-2])
{
dist–;
}
else if(s[q-1]!=s[q]&&s[q-1]!=s[q-2])
{
dist+=2;
}
else if(s[q-1]==s[q]&&s[q-1]==s[q-2])
{
dist-=2;
}

        if(s[q-1]=='1')
            s[q-1]='0';
        else
            s[q-1]='1';
       // cout<<s<<endl;
       cout<<dist<<endl;
    }
}
return 0;

}`

Can someone help me out with this code??
It’s passing many test cases but I cant figure out where it fails.

https://www.codechef.com/viewsolution/47308647
GUYS please help me find out mistake ,i have tried a lot but still not getting whats wrong .
I have checked almost all cases including n=1 and getting correct answer , but still WA .Please help me out

My code always getting runtime error :

#include <bits/stdc++.h>
#define ll long long int
using namespace std;

int main()
{
int t;
cin>>t;

while(t--)
{
    int n,k;
    cin>>n>>k;
    
    string s;
    cin>>s;
    
    
    
    
    
    int d = 0;
    
    for(int z=0;z<=s.size()-2;z++)
        {
           if(s[z]==s[z+1])
           d+=2;
           
           else
           d+=1;
        }
        
       int len = 0;
       
       
       
    while(k--)
    {
        int j;
        cin>>j;
        j--;
        
        
        
        if(s[j] =='1')
        s[j] = '0';
        
        else
        s[j] = '1';
        
        
        if(j>0)  //left
        {
        if(s[j-1]==s[j])
        len++;
        
        else 
        len--;
        }
        
        
        if(j<s.size()-1)  //right
        {
        if(s[j+1]==s[j])
        len++;
        
        else
        len--;
        }
        
        d = d + len;
        cout<< d <<"\n";
        
        
        len = 0;
        
    }
    
    
    
}
return 0;

}

@darshancool25

My code is always giving RUNTIME ERROR, i tried many times…

#include <bits/stdc++.h>
#define ll long long int
using namespace std;

int main()
{
int t;
cin>>t;

while(t--)
{
    int n,k;
    cin>>n>>k;
    
    string s;
    cin>>s;
    
    
    
    
    
    int d = 0;
    
    for(int z=0;z<=s.size()-2;z++)
        {
           if(s[z]==s[z+1])
           d+=2;
           
           else
           d+=1;
        }
        
       int len = 0;
       
       
       
    while(k--)
    {
        int j;
        cin>>j;
        j--;
        
        
        
        if(s[j] =='1')
        s[j] = '0';
        
        else
        s[j] = '1';
        
        
        if(j>0)  //left
        {
        if(s[j-1]==s[j])
        len++;
        
        else 
        len--;
        }
        
        
        if(j<s.size()-1)  //right
        {
        if(s[j+1]==s[j])
        len++;
        
        else
        len--;
        }
        
        d = d + len;
        cout<< d <<"\n";
        
        
        len = 0;
        
    }
    
    
    
}
return 0;

}

What is wrong with this solution I tried many test cases! :upside_down_face:
:no_mouth:

#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
#define ll long long

int main()
{
    // your code goes here
    int t = 1;
    cin >> t;
    while (t--)
    {
        ll n, k;
        cin >> n >> k;
        string s;
        cin >> s;
        int a[k];
        for (int i = 0; i < k; i++)
        {
            cin >> a[i];
        }
        ll intans = 0;
        for (int i = 0; i < n - 1; i++)
        {
            if ((s[i] == '0' && s[i + 1] == '0') || (s[i] == '1' && s[i + 1] == '1'))
            {
                intans += 2;
            }
            else
            {
                intans += 1;
            }
        }
        for (int i = 0; i < k; i++)
        {
            ll ans = 0;

            if(a[i]-1 == 0)
            {
                if(s[a[i]] == s[a[i] - 1]) 
                {
                    ans = intans - 1;
                }
                else
                {
                    ans = intans + 1;
                }
                cout << ans << "\n";
                intans = ans;
                if(s[a[i]-1] == '0') s[a[i]-1] = '1';
                else s[a[i]-1] = '0';
                continue;
            }
            if(a[i]-1 == n-1)
            {
                if(s[a[i]-2] == s[a[i] - 1]) 
                {
                    ans = intans - 1;
                }
                else
                {
                    ans = intans + 1;
                }
                cout << ans << "\n";
                intans = ans;
                if(s[a[i]-1] == '0') s[a[i]-1] = '1';
                else s[a[i]-1] = '0';
                continue;
            }

            if ((s[a[i] - 2] == s[a[i] - 1]) && (s[a[i]] == s[a[i] - 1]))
            {
                ans = intans - 2;
            }
            else if ((s[a[i] - 2] != s[a[i] - 1]) && (s[a[i]] == s[a[i] - 1]))
            {
                ans = intans;
            }
            else if ((s[a[i] - 2] == s[a[i] - 1]) && (s[a[i]] != s[a[i] - 1]))
            {
                ans = intans;
            }
            else
            {
                ans = intans + 2;
            }
            cout << ans << "\n";
            intans = ans;
            if(s[a[i]-1] == '0') s[a[i]-1] = '1';
            else s[a[i]-1] = '0';
        }
    }
    return 0;
}

Type cast s.size() to int OR just define a variable int sz = s.size() and use sz everywhere. You were getting runtime error as generally .size() returns an unsigned int value

1 Like

@darshancool25 Thankyou so so much it finally worked…I am happy because after a lot of attempts i got it and also I watched your video before the code part so that I can attempt from myself.
It feels really nice if we did from ourselves.

Can someone explain why there is a Time Limit Exceeded error here? View Solution
@daanish_adm @cherry0697 @monogon

@henrychen222
Thanks a lot sir . I have one doubt .
if string in c++ is immutable objects the how can
s[index] ^= 1;
this line is valid ?

One thing that’s bad for performance: IsSame and Score both take strings by value instead of by reference, causing the passed string to be copied each time they are called.

Yaa, I realized later that I didn’t consider the edge case of string of 1 unit length. Thanks for pointing it out.

although this code passes all the test cases still gives WA please help…
/*
first of all as stated in the question calculate the distance between the first and last
particle. denote the change in the given position by the variable “change”
so to check for the change in the string we just haveto check in the vicinity that is left and the right
THIS PROBLEM IS MEDIUM FOR ME
so after making a change if the particle in vicinity is opposite then len–
and if they are of the same chare then len++

/
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
int main()
{
ll t;
cin>>t;
while(t–)
{
ll n,k;
cin>>n>>k;
string s;
cin>>s;//since we only need a single string therefore not creating a loop
ll length=n-1;//distance between first and last
//calculating the initial length
for(ll i=1;i<n;i++)
{
length+=s[i]==s[i-1];//
**
}
while(k–)//executing till changes become zero
{
ll queries;
cin>>queries;
queries–;
if(s[queries]==‘1’)
{
s[queries]=‘0’;
}
else
{
s[queries]=‘1’;
}
if(queries<n-1) //charge is flipped and now checking vicinity
{
if(s[queries]==s[queries+1])
{
length++;
}
else
{
length–;
}
}
// now checking if the previous configuration was same or different
if(queries>0)
{
if(s[queries]==s[queries-1])
{
length++;
}
else
{
length–;
}
}
cout<<length<<endl;
//woah!! this was a nasty one…xd

}
return 0;
    }

}

Please either format your code or (better!) link to your submission - the forum software has mangled it and it won’t compile! :slight_smile:

Edit:

Deciphered it. Obvious one:

2
3 3
010
2 1 3
3 3
010
2 1 3

This is something people should try whenever they have a WA.

Edit2:

Second one of these today, interestingly :slight_smile: Running testcases with array - #2 by ssjgz

strings in C++ are not immutable.

1 Like

Your solution fails for the following test input:

1
1 1
0
1

@sarcastic_yogi Yours too :slight_smile:

@aadarshsinha Yours has an out-of-bounds access with this testcase.

1 Like

s[index] ^= 1, means index char switch. xor 1 change ‘0’ → ‘1’, ‘1’ → ‘0’ easily, I think c++ string chars can be changed.

example s= ‘abc’, s[1] = ‘d’. the s = ‘adc’

1 Like

@ssjgz
Understood sir.

1 Like

@henrychen222
thanks sir , now i understand.

#include
using namespace std;

int main() {
int x,a,b,c,d,y,z;
string s;
cin >> x;
for(int i=0;i<x;i++)
{
cin>>a>>b;
char arr[a-1];
char k;
int jar[a-1];
cin>>s;
for(int j=0;j<a;j++){
arr[j]=s[j];
k=arr[j];
jar[j] = (int)k;
}
for(int lo=0;lo<b;lo++){
cin>>d;
if(jar[d-1]==0)
{
jar[d-1]=1;
}
else
{
jar[d-1]=0;
}
int dis=0;
for(int ll=1;ll<a;ll++){
if(jar[ll]-jar[ll-1]==0){
dis=dis+1;
}
else{
dis=dis+2;
}
}
cout<<dis<<endl;
}
}

return 0;

}
@darshancool25 can you tell me what is the problem if I code it this way
and why the submission has errors
It will be very kind of you.Thank you.