COUPON2 - Editorial

PROBLEM LINK:

Contest Division 1
Contest Division 2
Contest Division 3
Practice

Setter:
Tester: Istvan Nagy
Editorialist: Taranpreet Singh

DIFFICULTY

Cakewalk

PREREQUISITES

None

PROBLEM

Given order details for two days, delivery charge and cost of a coupon which waives the delivery charges on orders worth \geq 150 rupees, determine whether it is beneficial to apply coupon or not.

QUICK EXPLANATION

Compute the cost with and without coupon and compare them.

EXPLANATION

Since we are checking whether buying coupon is beneficial or not, all we need to do is to compute the cost with and without coupon. It is useful to apply coupon only if the cost including coupon cost is strictly less than the total cost without coupon.

Since all the items on same day are delivered together, we only care about sum of cost of items. So let’s denote A = A_1+A_2+A_3 and B = B_1+B_2+B_3 be the order cost of first and second day.

Cost without coupon is easy to compute. Order of first day is worth A rupees and delivery charges D, so cost for first day is A+D. Cost for second day is B+D. Hence, total cost, if coupon is not purchased is A+B+2*D

Cost with coupon firstly includes coupon cost C. Order costs remain unaffected with coupon, so order costs A and B remain same.

  • If none of A and B is \geq 150, then delivery cost is added twice.
  • If exactly one of A and B is \geq 150, then delivery cost is added once.
  • If both A and B are \geq 150, then delivery cost is not added.

Hence, the order cost when coupon is purchased is C+A+B+x where x denote the number of orders with cost <150 rupees.

So now we have cost in both cases, so we can compare them and determine whether it is beneficial to purchase coupon or not.

Follow up

Suppose you are given order amounts for N days, coupon cost C and delivery cost D. A coupon is valid only for K days. Find the minimum cost, if coupon can be bought any number of times, any day.

TIME COMPLEXITY

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

SOLUTIONS

Setter's Solution
//created by Whiplash99
import java.io.*;
import java.util.*;
class A
{
    public static void main(String[] args) throws Exception
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

        int i,N;

        int T=Integer.parseInt(br.readLine().trim());
        StringBuilder sb=new StringBuilder();

        while (T-->0)
        {
            String[] s=br.readLine().trim().split(" ");
            int D=Integer.parseInt(s[0]);
            int C=Integer.parseInt(s[1]);

            s=br.readLine().trim().split(" ");
            int A1=Integer.parseInt(s[0]);
            int A2=Integer.parseInt(s[1]);
            int A3=Integer.parseInt(s[2]);
            
            s=br.readLine().trim().split(" ");
            int B1=Integer.parseInt(s[0]);
            int B2=Integer.parseInt(s[1]);
            int B3=Integer.parseInt(s[2]);

            int X=C+((A1+A2+A3)>=150?0:D)+((B1+B2+B3)>=150?0:D);
            int Y=2*D;

            sb.append(X<Y?"YES\n":"NO\n");
        }
        System.out.println(sb);
    }
}
Tester's Solution
#include <iostream>
#include <cassert>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
#include <random>
#include <limits>

#ifdef HOME
#define NOMINMAX   
#include <windows.h>
#endif

#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define forn(i, n) for (int i = 0; i < (int)(n); ++i)
#define for1(i, n) for (int i = 1; i <= (int)(n); ++i)
#define ford(i, n) for (int i = (int)(n) - 1; i >= 0; --i)
#define fore(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i)

template<class T> bool umin(T& a, T b) { return a > b ? (a = b, true) : false; }
template<class T> bool umax(T& a, T b) { return a < b ? (a = b, true) : false; }

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) {
		    assert(cnt > 0);
		    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;
	    }
	    // 		if(g == '\r')
	    // 			continue;

	    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, ' ');
}

std::mt19937 rng(0x15c51);

template<typename T>
T getRandom(T from, T to)
{
    std::uniform_int_distribution<T> distribution(from, to);
    return distribution(rng);
}


int main(int argc, char** argv)
{
#ifdef HOME
    if (IsDebuggerPresent())
    {
	    freopen("../in.txt", "rb", stdin);
	    freopen("../out.txt", "wb", stdout);
    }

// 	int Tc = 100000;
// 	printf("%d\n", Tc);
// 	forn(tc, Tc)
// 	{
// 		printf("%d %d\n", getRandom(1, 100), getRandom(1, 100));
// 		printf("%d %d %d\n", getRandom(1, 100), getRandom(1, 100));
// 		printf("%d %d %d\n", getRandom(1, 100), getRandom(1, 100));
// 	}
// 
// 	return 0;

#endif
    int T = readIntLn(1, 100000);
    forn(tc, T)
    {
	    int D = readIntSp(1, 100);
	    int C = readIntLn(1, 100);
	    
	    int A1 = readIntSp(1, 100);
	    int A2 = readIntSp(1, 100);
	    int A3 = readIntLn(1, 100);

	    int B1 = readIntSp(1, 100);
	    int B2 = readIntSp(1, 100);
	    int B3 = readIntLn(1, 100);

	    int v1 = A1 + A2 + A3 + B1 + B2 + B3 + D + D;

	    int v2 = A1 + A2 + A3 + B1 + B2 + B3 + C;
	    if (A1 + A2 + A3 < 150)
		    v2 += D;
	    if (B1 + B2 + B3 < 150)
		    v2 += D;

	    if (v2 < v1)
		    printf("YES\n");
	    else
		    printf("NO\n");
    }
    assert(getchar() == -1);
    return 0;
}
Editorialist's Solution
import java.util.*;
import java.io.*;
class COUPON2{
    //SOLUTION BEGIN
    void pre() throws Exception{}
    void solve(int TC) throws Exception{
        int D = ni(), C = ni();
        int A = ni()+ni()+ni();
        int B = ni()+ni()+ni();
        int withoutCoupon = A+B+D+D;
        int withCoupon = C + A+B+ (A >= 150?0:D) + (B >= 150?0:D);
        pn(withCoupon < withoutCoupon ?"YES":"NO");
    }
    //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 COUPON2().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;
        }
    }
}

VIDEO EDITORIAL:

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

1 Like

Can anyone please help me regarding whats wrong with my code, it did work fine for the test cases.

#include <stdio.h>

int main(void) {
// your code goes here

int T;
scanf("%d",&T);

for(int j=0;j<T;j++)
{
    int D,C,sumA=0,sumB=0;
    scanf("%d %d",&D,&C);
    int A[3],B[3];
    //printf("%d %d",D,C);
    for(int i=0;i<3;i++)
    {
        scanf("%d",&A[i]);
        sumA = sumA + A[i];
        
    }
    
     for(int i=0;i<3;i++)
     {
         scanf("%d",&B[i]);
         sumB = sumB + B[i];
         
     }
     
     
     
     
     if(sumA>=150&&sumB>=150)
     {
         if(D==C)
         {
             printf("NO\n");
         }
         
         else if(D<C)
         {
             if(sumA+sumB+D+D<sumA+sumB+C)
             {
                 printf("NO\n");
             }
             else
             {
                 printf("YES\n");
             }
         }
         else if(D>C)
         { 
             printf("YES\n");
         }
     }
     
     else if(sumA>=150&&sumB<=150||sumA<=150&&sumB>=150)
     {
         if(D==C)
         {
             printf("NO\n");
         }
         else if(D<C)
         {
             if(sumA+sumB+C+D<sumA+sumB+D+D)
             {
                 printf("YES\n");
             }
             else
             {
                 printf("NO\n");
             }
         }
         else if(D>C)
         {
             if(sumA+sumB+C+D<sumA+sumB+D+D)
             {
                 printf("YES\n");
             }
             else
             {
                 printf("NO\n");
             }
         }
     }
     
     
     
     
     
     
     
     
     
    //printf("%d %d\n",sumA,sumB);
}
return 0;

}

You are comparing with 1 day of delivery cost, you have to compare with 2 day delivery cost or else you can take help with my easy code.

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

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	
	int t;
	cin >> t;
	
	while(t--){
	   int c,d;
	   cin >> d >> c;
	   int a1,a2,a3,b1,b2,b3,sumA=0,sumB=0;
	   cin >> a1 >> a2 >> a3;
	   cin >> b1 >> b2 >> b3;
	   sumA = a1 + a2 + a3;
	   sumB = b1 + b2 + b3;
	   
	   if(sumA>=150 && sumB>=150)
	   {
	       if((sumA+sumB+c)<(sumA+sumB+ 2*d))
	       {
	           cout << "YES" << endl;
	       }
	       else
	        cout <<"NO"<<endl;
	   }
	   else if(sumA>=150 || sumB>=150)
	   {
	       if((sumA+sumB+ c + d)<(sumA+sumB+ 2*d))
	       {
	           cout << "YES" << endl;
	       }
	       else
	        cout <<"NO"<<endl;
	   }
	   else
	        cout <<"NO"<<endl;
	}
	return 0;
}
1 Like

run this program. i think this will be working

#include

using namespace std;

int main()

{

int T;

scanf("%d",&T);

for(int j=0;j<T;j++)

{

    int D,C,sumA=0,sumB=0;

    scanf("%d %d",&D,&C);

    int A[3],B[3];

    //printf("%d %d",D,C);

    for(int i=0;i<3;i++)

    {

        scanf("%d",&A[i]);

        sumA = sumA + A[i];

        

    }

    

    for(int i=0;i<3;i++)

    {

        scanf("%d",&B[i]);

        sumB = sumB + B[i];

        

    }

    

    if(sumA>=150&&sumB>=150)

    {

        if(D==C)

        {

            printf("NO\n");

        }

        

        else if(D<C)

        {

            if(sumA+sumB+D+D<=sumA+sumB+C)   //change

            {

                printf("NO\n");

            }

            else

            {

                printf("YES\n");

            }

        }

        else if(D>C)

        { 

            printf("YES\n");

        }

    }

    

    else if(sumA>=150&&sumB<150||sumA<150&&sumB>=150)  // change

    {

        if(D==C)

        {

            printf("NO\n");

        }

        else if(D<C)

        {

            if(sumA+sumB+C+D<sumA+sumB+D+D)

            {

                printf("YES\n");

            }

            else

            {

                printf("NO\n");

            }

        }

        else if(D>C)

        {

            if(sumA+sumB+C+D<sumA+sumB+D+D)

            {

                printf("YES\n");

            }

            else

            {

                printf("NO\n");

            }

        }

    }

    //printf("%d %d\n",sumA,sumB);

}

return 0;

}

1 Like

the result’s still a WA ,but thanks a lot for stopping by and helping me out ;D.

Can anyone please help me regarding whats wrong with my code :frowning:

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

int sum(int sum,int c,int d){
if(sum>=150){
return (sum+c);
}else{
return (sum+d);
}
}
int main() {

int t;
cin>>t;
while(t--){
    int c,d,a[3],b[3],sumA=0,sumB=0;
    cin>>d>>c;
    for(int i=0;i<3;i++){
        cin>>a[i];
        sumA+=a[i];
    }
    for(int i=0;i<3;i++){
        cin>>b[i];
        sumB+=b[i];
    }
    
    if(sum(sumA,c,d)+sum(sumB,c,d) < (sumA+sumB+d+d) )
        cout<<"Yes"<<endl;
    else
        cout<<"No"<<endl;
}
return 0;

}

Run this code. I think this will be working.

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

int sum(int sum,int c,int d){

if(sum>=150){
    return (sum);       //change
}else{
   return (sum+d);
}

}

int main()

{

int t;

cin>>t;

while(t--)

{

    int c,d,a[3],b[3],sumA=0,sumB=0;

    cin>>d>>c;

    for(int i=0;i<3;i++)

    {

        cin>>a[i];

        sumA+=a[i];

    }

    for(int i=0;i<3;i++)

    {

        cin>>b[i];

        sumB+=b[i];

    }

    if(sum(sumA,c,d)+sum(sumB,c,d)+c < (sumA+sumB+d+d) )   //change 

        cout<<"YES"<<endl;

    else

        cout<<"NO"<<endl;

}

return 0;

}

1 Like

Thanks its working. :slightly_smiling_face:

Can anyone tell me what’s wrong in my code ?
It’s running and passing all test cases without error

#include
using namespace std;

int main() {
// input number of tests
int T;
cin >> T;

// loop over tests
for (int t = 0; t < T; ++t) {

	
	int D, C;
    cin>> D >> C;

	
	int sumA = 0, sumB = 0 ;

	for (int i = 0; i < 3; ++i) {

		
		int Ai;
		cin >> Ai;

		
		sumA += Ai;
		
	}
	for (int i = 0; i < 3; ++i) {

		
		int Bi;
		cin >> Bi;

		
		sumB += Bi;
		
	}
	if(sumA >= 150 && sumB >= 150){
	    if((sumA+sumB+C) < (sumA+sumB+2*D)){
	        cout<<"YES"<<endl;
	  
	    }
	    else{
	        cout << "NO"<<endl;
	    }
	    
	}
	else if (sumA<150 || sumB <150){
	    if((sumA+sumB+C) < (sumA+sumB+D)){
	        cout<<"YES"<<endl;
	  
	    }
	    else{
	        cout << "NO"<<endl;
	    }
	    
	}
	else {
	    if((sumA+sumB+C) < (sumA+sumB)){
	        cout<<"YES"<<endl;
	  
	    }
	    else{
	        cout << "NO"<<endl;
	    }
	    
	}
}
return 0;

}

i think you are not counting up the case when on day 2 the coupon is applicable but not on day1 …

my code