SHROUTE June Long Challenge

This is the problem link.
https://www.codechef.com/viewsolution/47773528
This is my solution. Its passing given testcases but giving wa. Where have I gone wrong?

I solved it like this.

#include <bits/stdc++.h>
using namespace std;

int main() {
// your code goes here
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin>>t;
while(t–){
int n,m;
cin>>n>>m;
multimap<int, int> mp1;
multimap<int, int> mp2;
for(int i=1; i<=n; ++i){
int x;
cin>>x;
if(x==1){
mp1.insert({i,1});
}
else if(x==2){
mp2.insert({i,2});
}
}
int size1 = mp1.size();
int size2 = mp2.size();

    for(int i=1; i<=m; ++i){
        int x;
        cin>>x;
        if(x==1){
            cout<<0<<" ";
        }
        else{
            auto it1 = mp1.lower_bound(x);
	        auto it2 = mp2.lower_bound(x);
	        int behind = -1;
	        int next = -1;
	        if((it1->first==x && it1->second!=0) || (it2->first==x && it2->second!=0)){
	            behind=0;
	            next=0;
	        }else{
	            if(it1->second!=0){
	                auto f=mp1.begin();
	                if(f->first == it1->first)
	                    behind=-1;
	                else{
	                    it1--;
	                    behind = x-(it1->first);
	                }
	            }
	            else{
	                if(size1>0){
	                    --it1;
	                    if(it1->second!=0)
	                        behind = x - (it1->first);
	                    else
	                        behind=-1;
	                }
	                else{
	                    behind = -1;
	                }
	            }
	            if(it2->second!=0){
	                next=(it2->first)-x;
	            }
	            else{
	                next = -1;
	            }
	        }
	        
	        if(behind==-1&&next==-1){
	            cout<<-1<<" ";
	        }
	        else if(behind == -1&& next!=-1){
	            cout<<next<<" ";
	        }
	        else if(behind!=-1&& next==-1){
	            cout<<behind<<" ";
	        }
	        else{
	            cout<<min(behind, next)<<" ";
	        }
        }
    }
    cout<<endl;
}
return 0;

}

5 Likes