Shovels and Swords CodeForces - 1366A(二分)
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。二分去做就可以了。
代碼如下:
努力加油a啊,(o)/~
總結
以上是生活随笔為你收集整理的Shovels and Swords CodeForces - 1366A(二分)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 数据结构-判断一棵树是否为二叉排序树
- 下一篇: NASA计划明年年底启用蓝色起源火箭送两