1

i tried to seek help from codechef forums but couldn't get any major help hence i posting my problem here !

the question is from July long challenge on codechef. problem link : https://www.codechef.com/JULY20B/problems/DRCHEF

code link : https://www.codechef.com/viewsolution/35869168

My code __

#include <iostream>
#include <set>
#include <iterator>
#include <stdio.h>

using namespace std ;

// it returns highest value present in multiset within the range [x,2x]
long int range_search (multiset <long int> S, long int X)

{
    multiset<long int>::iterator it1, it2;
    it1 = S.lower_bound(X);
    it2 = S.upper_bound(2 * X);
    long int ans;

    if (it1 != it2)
    {
        --it2;
        ans = *(it2);
    }

    else
    {
        ans = -1;
    }

    return ans;
}



// driver function
int main () {
    int T;
    cin>>T;

    for (int k =0; k<T; k++){
        int N ;
        long int x ;
        scanf("%d",&N) ;
        scanf ("%ld",&x) ;
        multiset<long int> s ;  // storing country populations in multiset
        multiset<long int>::iterator itr, itr1, itr2 ;
        long int days =0;
        long int val ;
        long int temp ;
        short int test = 0;
        


        for (int i=0; i<N; i++){
            scanf ("%ld",&val) ;
            s.insert (val) ;
        }   // inserting population of N countries
        
        
    /*     multiset <long int> :: iterator itrrr; 
    cout << "\nThe multiset gquiz1 is : "; 
    for (itrrr = s.begin(); itrrr != s.end(); ++itrrr) 
    { 
        cout << '\t' << *itrrr; 
    } 
    cout << endl; 
    */
    
        while (test == 0){
            // <0>  if set is empty 
            if (s.size() == 0){               // <0.1>
                test = 1;
                break;
            }

            else {                          // <0.2>

                // <1>  
                if (days == 0) {            // <1.1>     if its the first day 
                    itr = s.end () ;
                    --itr ;
                    temp = *(itr) ;       
                    
                    if (  (temp - x)*2 > temp ) {
                        days++ ;
                    }   
                    else {
                        s.erase(temp);
                        s.insert( (temp -x)*2 ) ;
                        days++ ;
                    }   
                    test = 0 ;  
                    continue ;   
                }    //end of <1.1> 

                else {                      // <1.2>     if it isnt first day we will do rangesearch [x,2x]
                    long int Z = range_search(s,x) ;
                    itr = s.end();
                    --itr;
                    temp = *(itr) ;

                    // <2>

                    if (Z != -1) {           // <2.1>     if some value found using rangesearch
                        if (Z == temp) {     // when Z is equal to most populated country's value
                            days = days + s.size() ;
                            test = 0 ;
                            break;
                        }

                        else {
                            x = Z;
                            s.erase (Z) ;
                            days++ ;
                            test = 0 ;
                            continue;
                        }

                    }    // end of <2.1>

                    else {                  // <2.2>
                        x = x*2;

                        if (  (temp - x)*2 > temp){
                            s.insert(temp) ;

                        } 
                        else {
                            s.erase(itr) ;
                            s.insert (  (temp - x)*2) ;
                        }

                        days++ ;
                        test = 0 ;
                        continue ;
                                            
                    }    // end of <2.2>

                    
                }    // end of <1.2>   
            }    // end of <0.2>
            
        }  // end of while loop

        cout<<days<<endl ;



    }  // end of testcase loop
    return 0;
}

Note : - I don't care whether my logic gives me wrong answer on edge cases or other test cases, i m particularly focusing to fix my code to simply execute my logic and i need help to fix my code errors as i m not getting the correct answers which my logic gives me on paper (meaning i have done some code blunder maybe overflow or something else)

my logic - i have created a multiset storing poppulations of every country in ascending order.

1.At any day (be it the first day of curing) if the number of cures available, X, is equal or greater than the largest element in multiset (country with the largest population) then the answer will be size of the multiset (as if we start from the last we can cure each country in one day)

2.IF X is less than last element of multiset then arises two cases :

2.A) if its the first day of curing - then simply use X to cure the country with highest population (last element of set), no need to update X (X remains same for the next day) and then update the countries population (after doubling) on which we used X cures. and we increment the tally of total days required by one.

2.B) else (when its not first day) - then we further check a condition hence two cases arise -

 2.B.a) we will search for a element in multiset within the range [x,2x] inclusively
        if a value found then we will use as many cures as the value of element returned 
        i.e size of population found within range [x,2x] let that population be P, 
        thus X = P (updating X for next day) and we remove that element from multiset 
        (because this country will be country cured on that day) and we increment the 
        total days required by one.



 2.B.b) else (when no such value in multiset exits in range [x,2x] ) then again we will 
        go to cure the country with largest population present. X will be updated to 2*X 
        (because today we can use 2*X cures to cure the most populated country), thus 
        X = X*2, we also update the population of that country after doubling at end of 
        the day.

Note : Also before doing all these tasks i also check whether my multiset is empty or not.

I have also attached explanation with sample test-cases.

Testcase 1 : when X = 1, N =5 and country’s populations are 10, 20, 30, 40, 50

enter image description here

Testcase 2 : when X = 10, N = 3 and country’s populations are 1, 20, 110 enter image description here

L. F.
  • 19,445
  • 8
  • 48
  • 82
  • Put the code in the question please. No external links to code – Waqar Aug 08 '20 at 06:58
  • okay sure i will add the code right now. shall i put the full description of problem as well instead of its link ? –  Aug 08 '20 at 07:03
  • This article might help: [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – L. F. Aug 08 '20 at 07:11

1 Answers1

0

You don't have to write a huge and lengthy code

The logic is very simple for this problem

  1. You just have sort the input array and check the nearest close population number than x
  1. Then you should count the days by supplying vaccines which increases your daily output
  1. The root part of this problem is to figure out supplying vaccines with 2X increase in cases daily

Logic

Check for population is even or odd Now two case arises in upper two cases

  1. x is smaller than half of population
  2. x is greater than or equal to half population (This is very crucial for the problem solving )

for the first case you have to supply vaccines in that country until x doesn't falls under second case when it happens you just supply vaccines half of population and make x double of it which finally makes x equal to the population

when it happens you can remove all patients of that country and obtain x double of population of current country at the end of that day

This goes sequently and don't forget to add the previous days or no of countries with population less than x their patients can be recovered in one day after we reach country with highest population...;)

Here is AC code for you..Hope you'll get the logic

#include<bits/stdc++.h>
using namespace std;
#define ll long long 
#define f(i,u,w) for(ll i=u;i<w;i++)
#define AI(u,w) ll u[w]; f(i,0,w)cin>>u[i]
#define T ll t; cin>>t; while(t--)

int main(){
    
/*  freopen("input.txt", "r" , stdin);
    freopen("output.txt", "w" , stdout);  */
    
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    
    T{
        ll n,x,ans=0;
        cin>>n>>x;
        AI(a,n);
        sort(a,a+n);
        ll b=lower_bound(a,a+n,x)-a;
        ans=ans+b;
        if(b!=0){
            if(2*a[b-1]>x)
            x=2*a[b-1];
        }
        f(i,b,n){
            if(x>=a[i]){
                ans++;
                x=a[i]*2;
            }
            else{
                ll c=(a[i]+1)/2;
                if(x>=c){
                    ans+=2;
                    x=a[i]*2;
                }
                else{
                    ans++;
                    while(x<c){
                        x=x<<1;
                        ans++;
                    }
                    ans++;
                    x=a[i]*2;
                }
            }
        }
        cout<<ans<<"\n";
    }
}
  • Firstly, [Why should I not #include ?](https://stackoverflow.com/Questions/31816095/Why-Should-I-Not-Include-Bits-Stdc-H.) Secondly, [Why is “using namespace std;” considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – Rohan Bari Aug 08 '20 at 08:01
  • @RohanBari I don't understand what does your comment have to do with this answer or the problem asked ....the person who asked it mentioned that it is codechef's question ....the code which I provided works perfectly on that site's compiler whether it is C++14 or C++17 ..(as i got AC)....but the info i already know about both the things and I never use offline compiler so this works for me fine – Stylebender Aug 08 '20 at 11:22
  • @Stylebender i appreciate your effort. But i wanted to basically debug my code as on running it i m not getting expected answers like the one i got on paper (in the pictures attached). can you help me in debugging my code ? –  Aug 09 '20 at 10:47
  • @humblefool Sorry can't help you – Stylebender Aug 16 '20 at 08:51