boladji
September 16, 2017, 6:52pm
1
Please help me check this code. It is working fine but the judge is still rejecting it.
#include <stdio.h>
#include <vector>
bool isin (std::vector <int> array, int const& n)
{
int start=0, end=array.size();
while (start<=end){
int m = (start+end)/2;
if (array[m] == n)
return m<array.size();
else
{
if (array[m] < n)
start = m+1;
else if(array[m] > n)
end = m-1;
}
}
return false;
}
int main(){
short int tests, length, ignored, tracked;
scanf("%hd", &tests);
while (tests--){
scanf("%hd %hd %hd", &length, &ignored, &tracked);
std::vector <int> ign(ignored,0);
std::vector <int> igntrc;
int i=-1, lower=0;
while (++i<ignored){
scanf ("%d", &ign[i]);
while (++lower<ign[i])
igntrc.push_back(lower);
}
int ok = 0, nok=igntrc.size();
while (tracked--){
int n;
scanf ("%d", &n);
if (isin(igntrc, n))
--nok;
if (isin(ign, n))
ok++;
}
printf("%d %d\n", ok, nok);
}
return 0;
}
Well, the easier approach for this question is to use sets for both the sequences…
Then you may observe that the solution for this problem is
Number of elements in intersection of given sets
N - Number of elements in union of given sets
You can read more about sets in c++ here and about union and intersection of arrays in c++ here
Have a look at my code (java) here because i don’t code in C++.
Please ACCEPT and UPVOTE if you find this helpful…
boladji
September 16, 2017, 11:11pm
3
Thanks taran_1407. Your approach worked but I cannot accept it because it is not really the answer I was looking for. My code is working fine while testing on local/online compilers but it has gotten rejected by the judge and that’s what I need to understand.
Looks like your code working now!!
Well, you are approaching the problem in a wrong manner…
First Thing, In your code, where are you adding elements to ign vector
Second thing, i feel you are handling input in a wrong manner…
Intended sequence of Operations
First add elements to first vector,
Then add elements to second vector,
ok = 0; nok = 0;
Then run a for loop from 1 to length
if(isin(ign) && isin(igntrc))ok++;
if(!isin(ign) && !isin(igntrc))nok++;
then print ok and nok
Please Accept my answer if you find this helpful…
Feel free to ask anything…
sprea27
December 12, 2017, 10:35am
6
Can someone please tell me what is wrong with this code?
https://www.codechef.com/viewsolution/16556494