Algorithm
problem link- https://www.spoj.com/problems/ACMT/
ACMT - Acm Teams
Engineer ahmed sror one of the best coches, He picks team members by using two ways.
First one that the optimal team of three people should consist of one experienced participant and two newbies. Thus, each experienced participant can share the experience with a large number of people.
Second one that the optimal team should have two experienced members plus one newbie. Thus, each newbie can gain more knowledge and experience.
All the teams during the training session should belong to one of the two ways described above. Furthermore,they agree that the total number of teams should be as much as possible.
There are E experienced members and N newbies on the training session.Can you calculate what maximum number of teams can be formed?
Input
The first line of the input contains a single integer corresponding to number of test cases t (1 ≤ t ≤ 10) ,The second line contains two integers E and N (0 ≤ E, N ≤ 5·105) — the number of experienced participants and newbies that are present at the training session.
Output
For each test case print a single integer representing the maximum number of teams that can be formed.
Example
Input: 2 2 6 4 5 Output: 2 3
Note Let's represent the experienced players as XP and newbies as NB.
In the first test the teams look as follows: (XP, NB, NB), (XP, NB, NB).
In the second test sample the teams look as follows: (XP, NB, NB), (XP, NB, NB), (XP, XP, NB).
Code Examples
#1 Code Example with C++ Programming
Code -
C++ Programming
#include <bits/stdc++.h>
#include <limits.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;
#define MOD (ll)1000000007
#define pb push_back
#define EPS 1e-9
#define FOR(i,n) for(int i = 0;i < n; i++)
#define FORE(i,a,b) for(int i = a;i <= b; i++)
#define pi(a) printf("%d\n", a)
#define all(c) c.begin(), c.end()
#define tr(container, it) for(typeof(container.begin()) it = container.begin(); it != container.end(); it++)
#define gc getchar_unlocked
#define sdi(a, b) si(a);si(b)
#define io ios_base::sync_with_stdio(false);cin.tie(NULL);
#define endl '\n'
#define F first
#define S second
template <typename T> T gcd(T a, T b){return (b==0)?a:gcd(b,a%b);}
template <typename T> T lcm(T a, T b){return a*(b/gcd(a,b));}
template <typename T> T mod_exp(T b, T p, T m){T x = 1;while(p){if(p&1)x=(x*b)%m;b=(b*b)%m;p=p>>1;}return x;}
template <typename T> T invFermat(T a, T p){return mod_exp(a, p-2, p);}
template <typename T> T exp(T b, T p){T x = 1;while(p){if(p&1)x=(x*b);b=(b*b);p=p>>1;}return x;}
void si(int &x){
register int c = gc();
x = 0;
int neg = 0;
for(;((c<48 || c>57) && c != '-');c = gc());
if(c=='-') {neg=1;c=gc();}
for(;c>47 && c<58;c = gc()) {x = (x<<1) + (x<<3) + c - 48;}
if(neg) x=-x;
}
int main(){
io;
int t;
cin >> t;
while(t--){
int e, n;
cin >> e >> n;
if(e > 2*n){
cout << n << endl;
}
else if(n > 2*e){
cout << e << endl;
}
else{
cout << (e+n)/3 << endl;
}
}
return 0;
}
Copy The Code &
Try With Live Editor
Input
2 6
4 5
Output
3
Demonstration
SPOJ Solution-Acm Teams-Solution in C, C++, Java, Python