题目传送
题意:
给你a颗砖石和b根木棍,现在你可以用一颗砖石和俩个木棍或者一根木棍和俩颗砖石来换翡翠,问你最多能换多少翡翠?
思路:
偏解
在分情况之前,我们先执行操作
if(a > b)
swap(a,b);
1.当 a*2 <= b时,这时候,全部用俩个b所代表的东西和一个a所代表的东西来做,一定是最多的。
2.当a * 2 > b时,我们就尽力让a的2倍 等于 b(资源最大利用),那么我们就可以列一个方程式: 设我们要用去x件b,那么有方程式: (a - 2 * x) * 2 == b - x ,就可以得出x = (2 * a - b)/3
接下来那么剩下的a,b就是最接近a * 2 == b了
正解:
我们可以看到,无论是哪一种换翡翠的方法,我们都是用的3的倍数次材料去换翡翠,那么我们直接(a+b)/3即可,但是还要与a,b的最小值比较,举个例子就很容易懂 a = 100000,b = 1,那么我们肯定是只能做一种,而(a+b)/3可就大多了
偏解AC代码
#include <bits/stdc++.h>
inline long long read(){char c = getchar();long long x = 0,s = 1;
while(c < '0' || c > '9') {if(c == '-') s = -1;c = getchar();}
while(c >= '0' && c <= '9') {x = x*10 + c -'0';c = getchar();}
return x*s;}
using namespace std;
#define NewNode (TreeNode *)malloc(sizeof(TreeNode))
#define Mem(a,b) memset(a,b,sizeof(a))
#define lowbit(x) (x)&(-x)
const int N = 1e5 + 10;
const long long INFINF = 0x7f7f7f7f7f7f7f;
const int INF = 0x3f3f3f3f;
const double EPS = 1e-7;
const unsigned long long mod = 998244353;
const double II = acos(-1);
const double PP = (II*1.0)/(180.00);
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> piil;
int main()
{
std::ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
int t;
cin >> t;
while(t--)
{
ll a,b;
cin >> a >> b;
if(a > b) swap(a,b);
if(a*2 <= b) cout << a << endl;
else
{
ll ans = (2*a-b)/3;
a -= 2*ans,b -= ans;
if(a >= b/2)
ans += b/2;
else
ans += a;
cout << ans << endl;
}
}
}
正解代码
#include <bits/stdc++.h>
inline long long read(){char c = getchar();long long x = 0,s = 1;
while(c < '0' || c > '9') {if(c == '-') s = -1;c = getchar();}
while(c >= '0' && c <= '9') {x = x*10 + c -'0';c = getchar();}
return x*s;}
using namespace std;
#define NewNode (TreeNode *)malloc(sizeof(TreeNode))
#define Mem(a,b) memset(a,b,sizeof(a))
#define lowbit(x) (x)&(-x)
const int N = 1e5 + 10;
const long long INFINF = 0x7f7f7f7f7f7f7f;
const int INF = 0x3f3f3f3f;
const double EPS = 1e-7;
const unsigned long long mod = 998244353;
const double II = acos(-1);
const double PP = (II*1.0)/(180.00);
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> piil;
int main()
{
std::ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
ll t;
cin >> t;
while(t--)
{
ll a,b;
cin >> a >> b;
cout << min((a+b)/3,min(a,b)) << endl;
}
}