CHEAPFUEL - Editorial

PROBLEM LINK:

Contest Division 1
Contest Division 2
Contest Division 3
Practice

Setter: Ashish Gupta
Tester: Samarth Gupta
Editorialist: Taranpreet Singh

DIFFICULTY

Cakewalk

PREREQUISITES

None

PROBLEM

The current price of petrol is X rupees, and the current price of diesel is Y rupees. At the start of each month, the price of petrol increases by A rupees and the price of diesel increases by B rupees.

Chef is curious to know which fuel costs less at the end of the K^{th} month. If petrol is cheaper than diesel at the end of K^{th} month, then print \verb+PETROL+. If diesel is cheaper than petrol at the end of the K^{th} month, then print \verb+DIESEL+. If both the fuels have the same price at the end of the K^{th} month, then print \verb+SAME PRICE+.

QUICK EXPLANATION

  • The price of Petrol after K days is X+A*K and the price of Diesel after K days is Y+B*K
  • Compare which is higher and print the answer accordingly.

EXPLANATION

Every day, the price of Petrol rises by A, and the price of diesel rises by B. This repeated for K days.

So the price of Petrol after K days is X+(A+A+A +\ldots K \text{times}) = X + A*K.
Similarly, the price of Diesel after $K$$ days is Y+(B+B+B +\ldots K \text{times}) = Y + B*K.

Now we can compare the values X+A*K and Y+B*K and print the fuel name accordingly.

The following snippet describes the solution except for input.

Petrol = X+A*K
Diesel = Y+B*K
if petrol < diesel: print("PETROL");
else if petrol > diesel: print("DIESEL");
else: print("SAME PRICE");

Constraints were low enough to actually simulate the prices of K days one by one, with time complexity O(K) per test case.

TIME COMPLEXITY

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

SOLUTIONS

Setter's Solution
#include <bits/stdc++.h>
using namespace std;

void solve(){
    int X; cin >> X;
    int Y; cin >> Y;
    int A; cin >> A;
    int B; cin >> B;
    int K; cin >> K;

    int petrol = X + A*K;
    int diesel = Y + B*K;

    if(petrol < diesel){
	    cout << "PETROL" << '\n';
    }else if(diesel < petrol){
	    cout << "DIESEL" << '\n';
    }else{
	    cout << "SAME PRICE" << '\n';
    }
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int t;
    cin >> t;
    for(int i = 0; i < t; i++){
	    solve();
    }

    return 0;
}
Tester's Solution
#include <bits/stdc++.h>
using namespace std;

long long readInt(long long l, long long r, char endd) {
    long long x=0;
    int cnt=0;
    int fi=-1;
    bool is_neg=false;
    while(true) {
        char g=getchar();
        if(g=='-') {
            assert(fi==-1);
            is_neg=true;
            continue;
        }
        if('0'<=g&&g<='9') {
            x*=10;
            x+=g-'0';
            if(cnt==0) {
                fi=g-'0';
            }
            cnt++;
            assert(fi!=0 || cnt==1);
            assert(fi!=0 || is_neg==false);
 
            assert(!(cnt>19 || ( cnt==19 && fi>1) ));
        } else if(g==endd) {
            if(is_neg) {
                x=-x;
            }
            assert(l<=x&&x<=r);
            return x;
        } else {
            assert(false);
        }
    }
}
string readString(int l, int r, char endd) {
    string ret="";
    int cnt=0;
    while(true) {
        char g=getchar();
        assert(g!=-1);
        if(g==endd) {
            break;
        }
        cnt++;
        ret+=g;
    }
    assert(l<=cnt&&cnt<=r);
    return ret;
}
long long readIntSp(long long l, long long r) {
    return readInt(l,r,' ');
}
long long readIntLn(long long l, long long r) {
    return readInt(l,r,'\n');
}
string readStringLn(int l, int r) {
    return readString(l,r,'\n');
}
string readStringSp(int l, int r) {
    return readString(l,r,' ');
}
 
void readEOF(){
    assert(getchar()==EOF);
}

int main() {
    // your code goes here
    int t;
    t = readIntLn(1, 1000);

    while(t--){
        int x, y, a, b, k;
        x = readIntSp(0, 1000);
        y = readIntSp(0, 1000);
        a = readIntSp(0, 1000);
        b = readIntSp(0, 1000);
        k = readIntLn(1, 1000);
        if(x + a*k < y + b*k)
            cout << "PETROL\n";
        else if(x + a*k > y + b*k)
            cout << "DIESEL\n";
        else
            cout << "SAME PRICE\n";
    }
    readEOF();
    return 0;
}
Editorialist's Solution
import java.util.*;
import java.io.*;
class CHEAPFUEL{
    //SOLUTION BEGIN
    void pre() throws Exception{}
    void solve(int TC) throws Exception{
        int X = ni(), Y = ni(), A = ni(), B = ni(), K = ni();
        int petrol = X+K*A, diesel = Y+K*B;
        if(petrol < diesel)pn("PETROL");
        else if(petrol > diesel)pn("DIESEL");
        else pn("SAME PRICE");
    }
    //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 CHEAPFUEL().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: