Maximum number of pairwise decrements possible in three numbers

Revision en1, by liveoverflow, 2020-03-29 00:21:51

Given are the 3 non-negative integers a,b,c

In a single operation, we have to subtract 1 from two integers only if they don't become negative.

We have to find the maximum no of operations possible that we can do until the given operation is not possible.

constraints:1<=a,b,c<=10^18 , 1<=test-cases<=10^5

Example- (1,2,3) = (1,1,2) -> (1,0,1) -> (0,0,0) , ans is 3

(1,1,8) = (1,0,7) -> (0,0,6) , ans is 2

Any approach or proof will be highly helpful.

I have actually written a code that's working as far as I know, but I don't know if it's completely true,

Thanks ~~~~~

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

#define fastio ios_base::sync_with_stdio(0); cin.tie(0)
#define LL long long 

int main(){
fastio;
int t; cin>>t;
while(t--){
LL a[3]; cin>>a[0]>>a[1]>>a[2];
sort(a,a+3);
if(a[0]+a[1]>=a[2]){
   LL ans = a[2] + (a[0]+a[1]-a[2])/2;
   cout<<ans;
 }
else {
   LL ans = a[1] + min(a[0],a[2]-a[1]);
   cout<<ans;
 }
cout<<"\n";
}
}

~~~~~

Tags #math, #algorithms

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en1 English liveoverflow 2020-03-29 00:21:51 1155 Initial revision (published)