日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Shovels and Swords CodeForces - 1366A(二分)

發(fā)布時(shí)間:2023/12/15 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Shovels and Swords CodeForces - 1366A(二分) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

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.
思路: 一開始以為是貪心或者數(shù)學(xué),結(jié)果是二分。
為什么是二分的,我們假設(shè)sword有x個(gè),shovel有y個(gè)。那么我們就需要2x+y個(gè)diamonds和2y+x個(gè)stick。我們可以想一下,最終的結(jié)果,一定是stick或者diamonds有一個(gè)為0,哪一個(gè)為零呢?就是初始值比較小的那一個(gè),最終就是0。假設(shè)一開始最小的是min,最大的是max,那么2x+y=min或者2y+x=min。另一個(gè)方程式小于等于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)/~

總結(jié)

以上是生活随笔為你收集整理的Shovels and Swords CodeForces - 1366A(二分)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。