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。二分去做就可以了。
代码如下:

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

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

starlet_kiss

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值