The All-Berland Team Programming Contest will take place very soon. This year, teams of four are allowed to participate.
There are aa programmers and bb mathematicians at Berland State University. How many maximum teams can be made if:
- each team must consist of exactly 44 students,
- teams of 44 mathematicians or 44 programmers are unlikely to perform well, so the decision was made not to compose such teams.
Thus, each team must have at least one programmer and at least one mathematician.
Print the required maximum number of teams. Each person can be a member of no more than one team.
Input
The first line contains an integer tt (1≤t≤1041≤t≤104) —the number of test cases.
This is followed by descriptions of tt sets, one per line. Each set is given by two integers aa and bb (0≤a,b≤1090≤a,b≤109).
Output
Print tt lines. Each line must contain the answer to the corresponding set of input data — the required maximum number of teams.
这道题就只有两种分配方式,13&24;所以我们可以将a,b分成AB AB AB AB AA AA AA AA(假设a > b),所以如果b的数量小于AA的数量(b < (a - b) / 2 ),则组数直接为b的数量。如果情况不是这样,则需要考虑1322混合情况.(13总比1322多),(a - b) / 2 【之前的组数abaa】 + (b - (a - b) / 2) / 2【剩余的AB组自行组合】 = (a + b) / 4.易证明,当b > a 时,上式也成立。故最终主语句为
min(min(a, b), (a + b) /4 ) 这里的min不是逻辑上的最小,而是为了确保答案绝对正确。
代码如下
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
int n;
cin >> n;
while(n --)
{
int a, b;
cin >> a >> b;
cout << min(min(a, b), (a + b) / 4) << endl;
}
return 0;
}