链接:https://www.nowcoder.net/acm/contest/78/F
来源:牛客网
题目描述
今天是某不愿透露姓名的谈姓大佬的生日,转发这场比赛到三个群就可以,获得以下三种礼包之一。
豪华礼包:一个U盘、一个鼠标和一个机械键盘。
幸运礼包:一个U盘、两个鼠标。
普通礼包:两个U盘、一个鼠标。
大佬一共准备了a个U盘、b个鼠标和c个机械键盘。为了给更多的人带来足够多的惊喜,大佬希望相邻的两位领礼包的参赛选手拿到的礼包类型都是不同的。
由于大佬正在宴请Final选手,并没有空打理这些,所以想让你告诉他 这些奖品最多可以发出多少份礼包。
输入描述:
输入第一行包含一个正整数T。
接下来T行每行包含3个正整数a, b, c,依次表示U盘、鼠标和机械键盘各有多少个。
输出描述:
输出T行,每行一个整数,表示最多能发出多少份礼包。
示例1
输入
2 4 4 0 1 1 1
输出
2 1
备注:
T<=100000
0<=a,b,c<=1000000
思路:发现三种方案的共同点是都需要一个a和一个b,去掉相同点后即每种方案分别对应一个a,b,c。比赛时一直想O(1)求,赛后看别人代码才明白二分即可。
#include <iostream> #include <fstream> #include <sstream> #include <cstdlib> #include <cstdio> #include <cmath> #include <string> #include <cstring> #include <algorithm> #include <queue> #include <stack> #include <vector> #include <set> #include <map> #include <list> #include <iomanip> #include <cctype> #include <cassert> #include <bitset> #include <ctime> using namespace std; #define pau system("pause") #define ll long long #define pii pair<int, int> #define pb push_back #define mp make_pair #define clr(a, x) memset(a, x, sizeof(a)) const double pi = acos(-1.0); const int INF = 0x3f3f3f3f; const int MOD = 1e9 + 7; const double EPS = 1e-9; int T, a, b, c; bool check(int a, int b, int c, int x) { a -= x, b -= x; if (a + b + c < x) { return false; } if (a > b) swap(a, b); if (b > c) swap(b, c); return a + b < x >> 1 ? false : true; } int main() { scanf("%d", &T); while (T--) { scanf("%d%d%d", &a, &b, &c); int s = 0, e = min(a, b), ans, mi; while (s <= e) { mi = s + e >> 1; if (check(a, b, c, mi)) { s = (ans = mi) + 1; } else { e = mi - 1; } } printf("%d ", ans); } return 0; }