Polycarp plays a well-known computer game (we won’t mention its name). In this game, he can craft tools of two types — shovels and swords. To craft a shovel, Polycarp spends two sticks and one diamond; to craft a sword, Polycarp spends two diamonds and one stick.
Each tool can be sold for exactly one emerald. How many emeralds can Polycarp earn, if he has a sticks and b diamonds?
Input
The first line contains one integer t (1≤t≤1000) — the number of test cases.
The only line of each test case contains two integers a and b (0≤a,b≤109) — the number of sticks and the number of diamonds, respectively.
Output
For each test case print one integer — the maximum number of emeralds Polycarp can earn.
Example
Input
4
4 4
1000000000 0
7 15
8 7
Output
2
0
7
5
Note
In the first test case Polycarp can earn two emeralds as follows: craft one sword and one shovel.
In the second test case Polycarp does not have any diamonds, so he cannot craft anything.
思路: 一开始以为是贪心或者数学,结果是二分。
为什么是二分的,我们假设sword有x个,shovel有y个。那么我们就需要2x+y个diamonds和2y+x个stick。我们可以想一下,最终的结果,一定是stick或者diamonds有一个为0,哪一个为零呢?就是初始值比较小的那一个,最终就是0。假设一开始最小的是min,最大的是max,那么2x+y=min或者2y+x=min。另一个方程式小于等于max。二分去做就可以了。
代码如下:
#include<bits/stdc++.h>
#define ll long long
using namespace std;
int s,d;
inline int check(int x,int y)
{
if(y<0) return 0;
return 2*y+x;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&s,&d);
int a=min(s,d);
int b=max(s,d);
int l=0,r=a;
int mid,ans=0;
while(l<=r)
{
mid=l+r>>1;
if(check(mid,a-mid*2)<=b) ans=mid,r=mid-1;
else l=mid+1;
}
cout<<a-ans<<endl;
}
return 0;
}
努力加油a啊,(o)/~