Doctor Chef Problem Code: DRCHEF Codechef Solution

 #include <bits/stdc++.h>
using namespace std;
int main(){
  ios::sync_with_stdio(0);
  cin.tie(NULL);
  int t;
  cin>>t;
  while(t--){
    int n,x;
    cin>>n>>x;
    vector<int> a(n);
    for(int i=0;i<n;i++)
      cin>>a[i];
    sort(a.begin(),a.end());
    auto it=lower_bound(a.begin(), a.end(),x);
    int aft;
    if(it==a.end()){cout<<a.size()<<endl;continue;}
    else if(it==a.begin())aft=1;
    else{
      int d1=1,d2=1,tx=x;
      while(tx<*it)tx*=2,d1++;
      tx=*(it-1);
      if((tx<<d1)>*it)aft=0;
      else aft=1;
    }
    if(aft){
      int d=0;
      auto it2=it;
      while(it!=a.end()){
        ++d;
        if(x<*it)x<<=1;
        else{x=(*it)<<1,it++;}
      }
      cout<<d+(it2-a.begin())<<endl;
    }
    else{
      int d=0;
      it--;
      auto it2=it;
       while(it!=a.end()){
        ++d;
        if(x<*it)x<<=1;
        else{x=(*it)<<1,it++;}
      }
      cout<<d+(it2-a.begin())<<endl;
    }
   }
}

Comments