FRUITS - Editorial

ad-hoc
cakewalk
cook66
editorial
fruits

#1

PROBLEM LINK:

Practice
Contest

Author: Sunny Aggarwal
Tester: Vasya Antoniuk
Editorialist: Pushkar Mishra

DIFFICULTY:

Cakewalk

PREREQUISITES:

Ad-hoc

PROBLEM:

Given are the number of apples (N) and oranges(M) and k, i.e., the amount of money that chef has to buy apples/oranges. Each orange/apple costs 1 unit. What is the minimum difference that can be achieved.

EXPLANATION:

The problem is a very direct one. We have to minimize the difference. We can only increase the number of apples or oranges that we have. The logical step is to increase that commodity which is less because increasing the which is already more will only increase the the difference. Let minC denote the quantity which is less. Let maxC be the quantity which is more. The answer is given by:

ans = maxC - min(minC+k, maxC)

We have a min operation in the second term because the optimal case is to make both the quantities equal. If we have enough money to increase the lesser one to more than the other one, then we would rather make them equal so as to have a difference of zero, which is optimal.

COMPLEXITY:

\mathcal{O}(1) per test case.

SAMPLE SOLUTIONS:

Author
Tester
Editorialist


#2

#include “iostream”
using namespace std;
int k;
void dif(int x,int y,int z);
void dif(int x,int y,int z)
{
int d;
if(x>y || x==y)
d=x-y;
else
d=y-x;
if(d<k || d==k)
{k=k-d;
if(k%2==0)
cout<<"0
";
else
cout<<"1
“;}
else
{
if(x>y)
{y=y+k;
cout<<x-y<<”
“;
}
else
{x=x+k;
cout<<y-x<<”
";
}
}
}

int main()
{
int t,m,n;
cin>>t;//no of test cases
do
{
cin>>m>>n>>k;//apple,oranges,money
dif(m,n,k);
t–;
}while(t!=0);
return 0;
}
//why is this giving a wrong answer


#3

#include <stdio.h>
int same(int ,int ,int);
int fgreat(int ,int ,int);
int main(void) {

int t,n,m,k,result[10],i;
scanf("%d",&t);
printf("

“);
for(i=0;i<t;i++)
{
scanf(”%d %d %d",&n,&m,&k);
if(n==m)
result* = same(n,m,k);
else if(n>m)
if(k<=(n-m))
{
m+=k;
result* = n-m;
}
else
result* = fgreat(n,m,k);
else
if(k<=(m-n))
{
n+=k;
result* = m-n;
}
else
result* = fgreat(m,n,k);
}

for(i=0;i<t;i++)
printf("

%d",result*);

return 0;

}
int same(int n,int m,int k)
{
if(k%2==0)
{
n+=k/2;
m+=k/2;
}
else
{
n+=k/2;
m+=k-k/2;
}
return m-n;
}
int fgreat(int n,int m,int k)
{
k-=(n-m);
m+=n-m;
return same(n,m,k);
}

what is wrong in this code?


#4

All 3 solution links are broken. Great. :frowning: Don’t know why such a well known issue crops up each time.


#5

https://www.codechef.com/viewsolution/10575699

What is wrong with my solution? Can anybody please help me cuz its supposed to be working fine for all test cases.


#6

include

using namespace std; int main() { int t; cin>>t; while(t--) { int n,m,k; cin>>n>>m>>k; while(k--) { if(nm) cout<<n-m<<" "; else cout<<m-n<<" "; } return(0); }


#7

T=input()

C=0
while C<T :

N,M,K=map(int,raw_input().split(' ')) 
if N>M :
    if (N-M)>K:
        print N-(M+K)
    elif (N-M)==K:
        print 0
    else:
        if (K-(N-M))%2==0:
            print 0
        else:
            print 1

else :
    if (M-N)>K:
        print M-(N+K)
    elif (M-N)==K:
        print 0
    else:
        if (K-(M-N))%2==0:
            print 0
        else:
            print 1
   
C=C+1

#8

#include<stdio.h>
int main()
{
int t1,t2,t=0,k=0,diff=0;
scanf("%d",&t);
while(t–)
{
scanf("%d%d%d",&t1,&t2,&k);
while(k>0&&(t1>0||t2>0))
{
if(t1>t2)
{
t1–;
}
else
{
t2–;
}
k–;

       }
       if(t1>t2)
       {
    diff=t1-t2;
    printf("%d

“,diff);
}
else{
diff=t2-t1;
printf(”%d
",diff);
}
}
return 0;
}

why it give wrong answer pledge help me


#9

@laukesh_123

In your while loop you are checking if(t1>t2) or not.
That’s just two out of three possibilities…

  1. t1>t2
  2. t2>t1
  3. t2=t1
    The second and third conditions are in the “else” section of your code.
    You should check for all three possibilities.

#10

Can anyone explain me the condition 3 4 4 ? According to the formula the answer is coming as 0.
But the answer should come as 1.
Can anyone explain me this?


#11

Calculate the absolute difference between no. of apples and oranges. We can further minimize this difference by buying either of apple or oranges accordingly:

int diff = abs(n-m)-k;

Spend all our coins to buy fruits accordingly. Minimum absolute difference can be 0 only, so for negative answer output 0 i.e. dont spend more coins afterwards we’ve equal no. of fruits:

int ans = diff<0?0:diff;

Solution Link:
https://www.codechef.com/viewsolution/15787068


#12

#include<stdio.h>

#include<stdlib.h>

int main()

{

int t,n,m,k;

scanf("%d",&t);

while(t–)

{

scanf("%d %d %d",&n,&m,&k);

while(k–)

{

if(n<=m)

    n += 1;

else

    m += 1;

}

printf("%d
",abs(m-n));

}

return 0;

}

This is my code. It resulted WA. Can you help me to find error ? condition in if : n is less than or equal to.


#13

/when i run this program on codeblock the output is correct it also satisfy the test case of this question but when i submit it it give the wrong answer what can i do please give some suggestion/
#include<bits/stdc++.h>
using namespace std;
int main(void)
{
int i,j,b,n,q;
cin>>q;
while(q–)
{
cin>>n;
int a[100];i=0;
a*=n;
for(i=1;i<n+1;i++)
cin>>a*;b=1;
for(i=1;i<n;i++)
{
for(j=b>a*?b:a*;a*%j || a[i+1]%j;j–);b=j;
}
for(i=1;i<n+1;i++)
{
a*=a*/b;
}
for(i=1;i<n+1;i++)
{
cout<<a*<<" ";
}

 }

}


#14

The testcase 5 4 4 is not giving the right answer.
It is giving answer 0 but actually it should be 1. can anyone tell why> and if it is true edtior approach is wrong.


#15

Please try to indent your code properly before submitting here if you want anyone to help you. I promise I will help you with this once you get this code all clearly indented. You can do so by just provide 4 times spaces before each line of your code.


#16

Check this out: https://www.codechef.com/viewsolution/9223853


#17

why my answer is wrong


#18

Fixed the formatting.


#19

while(k>0&&(t1>0||t2>0))

{
       
   if(t1>t2)

       {

           t1--;
       }

    else

       {
           t2--;
       }
       k--;

   }

This part seems to be wrong. Lets say I got t1=t2 in between, then I should print 0. But your program would further reduce t2 and print 1.


#20

please tell me why this is causing an incorrect answer.

#include
#include
using namespace std;

class Functions
{
public:
int getMinDifference(int apple, int orange, int coins)
{
int minDifference = 0;

		if(apple != orange)
		{
			if(apple > orange)
			{
				while(orange != apple)
				{
					coins = coins - 1;
					orange = orange + 1;
					
					if(coins == 0)
					{
						break;
					}
				}
				
				if(coins != 0)
				{
					if(coins % 2 == 0)
					{
						apple = apple + (coins / 2);
						orange = orange + (coins / 2); 
					}
					else if(coins % 2 != 0)
					{
						apple = apple + ((coins / 2) + 1);
						orange = orange + (coins / 2);
					}
				}
				
				minDifference = apple - orange;
			}
			else if(apple < orange)
			{
				while(apple != orange)
				{
					coins = coins - 1;
					apple = apple + 1;
					
					if(coins == 0)
					{
						break;
					}
				}
				
				if(coins != 0)
				{
					if(coins % 2 == 0)
					{
						orange = orange + (coins / 2);
						apple = apple + (coins / 2); 
					}
					else if(coins % 2 != 0)
					{
						orange = orange + ((coins / 2) + 1);
						apple = apple + (coins / 2);
					}
				}
				
				minDifference = orange - apple;
			}
		}
		else if(apple == orange)
		{
			if(coins % 2 == 0)
			{
				apple = apple + (coins / 2);
				orange = orange + (coins / 2);
				
				minDifference = apple - orange;
			}
			else if(coins % 2 != 0)
			{
				apple = apple + ((coins / 2) + 1);
				orange = orange + (coins / 2);
				
				minDifference = apple - orange;
			}
		}
		
		return minDifference;
	}

};

int main()
{
Functions functions;

int testCase;
int apple, orange, coins;

cin >> testCase;

if((1 <= testCase) && (testCase <= 100))
{
	for(int i = 0; i < testCase; i++)
	{
		cin >> apple >> orange >> coins;
		
		if((1 <= apple) && (apple <= 100) && (1 <= orange) && (orange <= 100) && (1 <= coins) && (coins <= 100))
		{
			cout << functions.getMinDifference(apple, orange, coins) << endl;
		}
	}
}

return 0;

}