Https://www.codechef.com/viewsolution/27971127 (TRAINSET)

https://www.codechef.com/viewsolution/27971127

please anyone help in trainset. i think its working fine but codechef is giving WA.

There’s a vector index-out-of-bounds at line 30 which might be causing the problem.

2 Likes

but in that case sample test cases also should not work. but they are working.

No, because it’s Undefined Behaviour so anything can happen, including the program working fine for the sample testcases.

ok. I will try another approach.

For a concrete testcase that (might) fail, try this:

2
2
a 0
a 0
1
a 0

But as I said, it’s Undefined Behaviour, so anything can happen (though it fails on my machine :))

1 Like

https://www.codechef.com/viewsolution/27971508
will you see this one.

That doesn’t pass the sample testcase :slight_smile:

As a hint, you were very close with your original submission - you just needed a small amount of code to guard against the index-out-of-bounds.

2 Likes

a small off topic question.
I saw your codechef contest graph has always positive slope. How did you manage to reach 6 stars in just 4 months.
How can I improve my skills like yours.

another question: how did you find that

this test case will fail.

1 Like

I was doing Hackerrank for a few years before I joined here: https://www.hackerrank.com/ssjgz

Edit: Plus I only do Long Challenges, which are easier in many ways :slight_smile:

I just saw what the flaw in your submission was (the out-of-bounds access) and tried to come up with a testcase that could feasibly exploit it :slight_smile:

3 Likes

In case you want the compiler to inform you when there is an attempt to subscript a container with an out-of-bounds index, you may add the -D_GLIBCXX_DEBUG compiler flag.
Credits: @ssjgz
Source: COPRIME3 using Mobius Function - #5 by ssjgz
:slightly_smiling_face:

2 Likes

#include <bits/stdc++.h>
using namespace std;
int main(){
int test;
cin>>test;
while(test–){
int n;
cin>>n;
string s[n];
int a[n];
for(int i=0;i<n;i++){
cin>>s[i]>>a[i];
}
int ttl[2];
ttl[0]=0;
ttl[1]=0;
int sum=0;
bool p[n];

for(int i=0;i<n;i++){
if(p[i]==0){

    if(a[i]==0){
     ttl[0]++;
    }else{
    ttl[1]++;
    }

for(int j=i+1;j<n;j++){
        if(p[j]==0){
    if(s[i]==s[j]){
         if(a[j]==0){
     ttl[0]++;
    }else{
    ttl[1]++;
    }
     p[j]=true;
    }
        }
}
    }

sum+=max(ttl[0],ttl[1]);
ttl[0]=0;
ttl[1]=0;

}
cout<<sum<<endl;
}
return 0;
}

why is this giving wa problem TRAINSET.

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

https://www.codechef.com/viewsolution/31584195
pls check it out for TRAINSET not getting ac i dont know why

1 Like

You need to initialise all elements of b - they’ll contain random values, otherwise.

2 Likes

More importantly what is this

for(int i=0;i<n;i++){
        if(p[i]==0){

        if(a[i]==0){
         ttl[0]++;
        }else{
        ttl[1]++;
        }

    for(int j=i+1;j<n;j++){
            if(p[j]==0){
        if(s[i]==s[j]){
             if(a[j]==0){
         ttl[0]++;
        }else{
        ttl[1]++;
        }
         p[j]=true;
        }
            }
    }
        }

    sum+=max(ttl[0],ttl[1]);
    ttl[0]=0;
    ttl[1]=0;

}

Please use better indentation and intuitive variable names. Instead of using so many nested ifs, you can use the && operator, and make sure deeper ifs are indented more, and so are their corresponding curly brackets.

2 Likes