题目戳这里
对于三种礼物来说,都需要用到一个a、一个b,所以抽象来说每个礼物对应a、b、c。于是二分答案,见代码
#include <iostream>
#include <algorithm>
#include <queue>
#include <stack>
#include <cstdio>
#include <string>
#include <cstring>
#include <vector>
#include <set>
#define LL long long
#define INF 0x3f3f3f3f
#define MOD 998244353
const int maxn = 1e5 + 5;
using namespace std;
bool judge(int a, int b, int c, int x){
a -= x;
b -= x;
if(a < 0 || b < 0) return false;
if(a + b + c < x) return false;//保证了有x份
if(a > b) swap(a,b);
if(b > c) swap(b,c);
if(a + b < x / 2) return false;//保证不相邻
return true;
}
int main(){
int T;
scanf("%d",&T);
while(T--){
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
int l = 0, r = min(a,b);
int ans = 0, m;
while(l <= r){
m = (l + r) / 2;
if(judge(a,b,c,m)){
ans = m;
l = m + 1;
}
else r = m - 1;
}
printf("%d\n",ans);
}
}
/*
2
4 4 0
1 1 1
*/