MEENA_009 Editorial

PROBLEM LINK:

Practice

Author: Ram Agrawal
Tester: Yogesh Deolalkar
Editorialist:Prathamesh Sogale

DIFFICULTY:

CAKEWALK, SIMPLE.

PREREQUISITES:

Greedy, Math

PROBLEM:

Meena has given two integers A,B. You have to choose an integer X in the range [minimum(A,B), maximum(A,B)] such that:

⌈B−X⌉/2 +⌈X−A⌉ /2 is maximum.

What is the maximum sum Meena can obtain if you choose X optimally?

Note: ⌈x⌉ : Returns the smallest integer that is greater than or equal to x (i.e rounds up to the nearest integer). For example, ⌈1.4⌉=2, ⌈5⌉=5, ⌈−1.5⌉=−1, ⌈−3⌉=−3 , ⌈0⌉=0.


# SOLUTIONS:

[details="Setter's Solution"]
# cook your dish here
import math
for t in range(int(input())):
    a,b=map(int,input().split())
    max1=max(a,b)
    min1=min(a,b)
    mx=-100000
    
    for i in range(min1,max1+1):
        ans=math.ceil(math.ceil((b-i)/2)+math.ceil((i-a)/2))
        mx=max(mx,ans)
    print(mx)    

[/details]

[details="Tester's Solution"]
#include <iostream>
using namespace std;

int main() {
	// your code goes here
	int k;
	cin>>k;
	while(k--){
	    long long int a,b;
	    cin>>a>>b;
	    long long int diff=b-a;
	    if(a==b){
	        cout<<"0"<<endl;
	    }
	    else if(b>a){
	        cout<<(diff/2)+1<<endl;
	    }
	    else{
	        if(diff%2==0){
	            cout<<(diff/2)+1<<endl;
	        }
	        else{
	            cout<<(diff/2)<<endl;
	        }
	    }
	}
	return 0;
}

[/details]