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)/~

Shovels and Swords CodeForces - 1366A(二分)相关推荐

  1. Match Points CodeForces 1156C 二分答案

    CodeForces 1156C Match Points 传送门:https://codeforces.com/problemset/problem/1156/C You are given a s ...

  2. Codeforces 650B 二分

    题目:http://codeforces.com/problemset/problem/650/B 题意: :给n张图片循环可看,每张图片的朝向为横(w)|竖(v),但是手机是竖直放置的.开始时打开的 ...

  3. CodeForces - 786C——二分+模拟?

    [题目描述] Rick and Morty want to find MR. PBH and they can't do it alone. So they need of Mr. Meeseeks. ...

  4. Frodo and pillows CodeForces - 760B 二分 注意l和r的选择

    以后写l和r的初始值的时候,在不影响合理性的前提下,尽量写大一点 比如下面这个代码,如果r不加以或者l==0就不行 #include <iostream> #include <cst ...

  5. Educational Codeforces Round 89 (Rated for Div. 2)(A, B, C, D)

    Educational Codeforces Round 89 (Rated for Div. 2) A. Shovels and Swords 思路 题意非常简单,就是得到最多的物品嘛,我们假定a, ...

  6. CF1366 EDU89 菜鸡的ABC题解

    Educational Codeforces Round 89 (Rated for Div. 2) A.Shovels and Swords 题意: 已知有a个木棍和b个钻石,做一个钻石铲要2个木棍 ...

  7. SCAU-春季训练-不应该啊(怎么这么菜。。。)

    2021/3/14 春季训练2(难度div2d) 反思:(赛前,看什么crt,赛时满脑子都是线性方程组,....................................) 最近表现都不太好.. ...

  8. javascript字典中添加数组_Javascript 数组与字典

    Javascript 的数组Array,既是一个数组,也是一个字典(Dictionary). 先举例看看数组的用法. var a = new Array(); a[0] = "Acer&qu ...

  9. 22.10.27补卡 CF-279B

    Problem - 279B - Codeforces 用二分前缀和写的 首先前缀和处理数组, 然后从1开始枚举1以后的前缀和是否满足小于等于m, 每次循环更新ans的值, 最后输出最大的答案 /* ...

最新文章

  1. 2021年度最有成就感的几件事
  2. ios UIScrollView 中控件自动增加间隔
  3. Java接受blob类型图片_原生JS上传图片接收服务器端图片并且显示图片(主要描述blob类型)...
  4. 3d建模电脑配置要求_学习3D建模需要什么软件,电脑配置应该如何?新手自学会遇到的问题...
  5. 32乘法运算_算术运算指令
  6. 电阻带行业调研报告 - 市场现状分析与发展前景预测(2021-2027年)
  7. Azure HDInsight与Hadoop周边系统集成
  8. (How to)Windows Live Writer插入Latex公式(补充)
  9. 原来做浏览器这么简单
  10. Java 学习笔记·十二 —— Java 案例·网上商城系统
  11. 数据分析 --- 如何处理脏数据
  12. 大数据DMP画像系统(转载 简介-龙果学院)
  13. ctf题库--天下武功唯快不破
  14. 【无标题】 天气决策树
  15. springboot入门(四):ajax实现登录
  16. 如何减少http请求
  17. 硬件开发规范化管理_华为硬件工程师手册_笔记1
  18. Rufus开源版U盘引导制作工具V3.16
  19. 可燃气体在线监测无线传输终端
  20. 爱因斯坦的宇宙(纪念爱因斯坦)

热门文章

  1. 如何将计算机专业知识和水文结合,2016水文勘测理论知识及参考答案 B卷
  2. linux 怎么设置静态ip,如何在Linux中设置静态IP地址和配置网络
  3. Android与Js进行交互
  4. android设置title_所见即所得的 Android 自动化神器,用 Automate 一键收藏文章
  5. mysql在linux下的安装_mysql在linux下的安装
  6. java jframe全屏_Java-将JFrame设置为全屏时,屏幕变黑
  7. RabbitMQ学习之Routing(4)
  8. Apache优化——日志管理
  9. ASP.NET MVC下使用AngularJs语言(三):ng-options
  10. WPF:Graphics绘图--Shapes形状