Educational Codeforces Round 89 (Rated for Div. 2)~~A. Shovels and Swords

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.

思路:两组方案,1.2a+b 2.a+2b,可以看出两种方案的公共部分 a+b,假设m<n,用公共部分填满m个,然后判断n的剩余部分(n-m),如果剩余部分过多(n-m>=m),最终结果就是m(m<(m+n)/3),若剩余部分不多(n-m<m),那就先把剩余部分填满,再把多于部分充分利用,最终结果就是(m+n)/ 3,且此时(m+n)/ 3 < m,所以结果为min( (m+n)/3,m)

#include<bits/stdc++.h>

using namespace std;

typedef long long LL;
const int mod = 1e9 + 7;
const int maxn = 1e6 + 1;
const int inf = 0x3f3f3f3f;
const double Pi = acos(-1.0);
const LL INF = 0x3f3f3f3f3f3f3f3f;

template<class T, class F> inline void mem(T a,F b, int c) {for(int i=0;i<=c;++i)a[i]=b;}
template<class T> inline void read(T &x,T xk=10) { // xk 为进制
	char ch = getchar(); T f = 1, t = 0.1;
	for(x=0; ch>'9'||ch<'0'; ch=getchar()) if(ch=='-')f=-1;
	for(;ch<='9'&&ch>='0';ch=getchar())x=x*xk+ch-'0';if(ch=='.') 
	for(ch=getchar();ch<='9'&&ch>='0';ch=getchar(),t*=0.1)x+=t*(ch-'0');x*=f;
}

int main() {
    int T; read(T);
    while(T --) {
        int n, m; read(n), read(m);
        if (n < m) swap(n, m);
        cout << min( (n + m) / 3, m )  << endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值