SLOOP - Editorial

PROBLEM LINK:

Contest Division 1
Contest Division 2
Contest Division 3
Practice

Setter: Daanish Mahajan
Tester & Editorialist: Taranpreet Singh

DIFFICULTY

Cakewalk

PREREQUISITES

None

PROBLEM

Given a song of duration S, find out the number of times you can play it completely during M minutes ride.

QUICK EXPLANATION

The song can be played at most M/S times.

EXPLANATION

Let’s simulate the process for M = 14, S = 3.

  • Song plays for first time during time [1, 3]
  • Song plays for second time during time [4, 6]
  • Song plays for third time during time [7, 9]
  • Song plays for fourth time during time [10,12]
  • Song plays for fifth time during time [13,15], but the ride got over at the end of 14-th minute, so this song is not counted.

Hence, for M = 14 and S = 3, we can play song 4 times.

It is easy to write a solution simulating above process, keeping count of the number of times song is played.

count = 0
while M >= S:
    count++
    M -= S

The count shall store the final number of times song is played completely.

O(1) solution

What we are doing above is repeated subtraction while M >= S. We can see that above loop continues for \lfloor M/S\rfloor iterations. Indeed, \lfloor M/S\rfloor is the number of times the song is played, hence we can directly output \lfloor M/S\rfloor as the answer in O(1) time.

Bonus

Given a song of duration S, find out the number of times you can play it during M minutes ride. If a song is in going on at the end of ride, it is counted as played.

TIME COMPLEXITY

The time complexity is O(1) per test case.

SOLUTIONS

Setter's Solution
#include<bits/stdc++.h>
# define pb push_back 
#define pii pair<int, int>
#define mp make_pair
# define ll long long int

using namespace std;

const int maxt = 1000, maxm = 100, maxs = 10;

int main()
{   
    int t; cin >> t;
    while(t--){
        int m, s; cin >> m >> s;
        cout << (int)(m / s) << endl;
    }
} 
Tester's Solution
import java.util.*;
import java.io.*;
class SLOOP{
    //SOLUTION BEGIN
    void pre() throws Exception{}
    void solve(int TC) throws Exception{
        int M = ni(), S = ni();
        pn(M/S);
    }
    //SOLUTION END
    void hold(boolean b)throws Exception{if(!b)throw new Exception("Hold right there, Sparky!");}
    static boolean multipleTC = true;
    FastReader in;PrintWriter out;
    void run() throws Exception{
        in = new FastReader();
        out = new PrintWriter(System.out);
        //Solution Credits: Taranpreet Singh
        int T = (multipleTC)?ni():1;
        pre();for(int t = 1; t<= T; t++)solve(t);
        out.flush();
        out.close();
    }
    public static void main(String[] args) throws Exception{
        new SLOOP().run();
    }
    int bit(long n){return (n==0)?0:(1+bit(n&(n-1)));}
    void p(Object o){out.print(o);}
    void pn(Object o){out.println(o);}
    void pni(Object o){out.println(o);out.flush();}
    String n()throws Exception{return in.next();}
    String nln()throws Exception{return in.nextLine();}
    int ni()throws Exception{return Integer.parseInt(in.next());}
    long nl()throws Exception{return Long.parseLong(in.next());}
    double nd()throws Exception{return Double.parseDouble(in.next());}

    class FastReader{
        BufferedReader br;
        StringTokenizer st;
        public FastReader(){
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        public FastReader(String s) throws Exception{
            br = new BufferedReader(new FileReader(s));
        }

        String next() throws Exception{
            while (st == null || !st.hasMoreElements()){
                try{
                    st = new StringTokenizer(br.readLine());
                }catch (IOException  e){
                    throw new Exception(e.toString());
                }
            }
            return st.nextToken();
        }

        String nextLine() throws Exception{
            String str = "";
            try{   
                str = br.readLine();
            }catch (IOException e){
                throw new Exception(e.toString());
            }  
            return str;
        }
    }
}

Feel free to share your approach. Suggestions are welcomed as always. :slight_smile:

2 Likes