二换一

a、b、c三个物品,两个可以换另一个,三个可以换一个奖品,求最多能换多少个奖品

 1 class Solution {
 2 public:
 3     int numberofprize(int a, int b, int c) {
 4         int lo = 0, hi = 1E9;
 5         int ret = 0;
 6 
 7         auto check = [&](int val) {
 8             int tmp = 0;
 9             int x = a, y = b, z = c;
10             x -= val, y -= val, z -= val;
11             if (x >= 0) tmp += x, x = 0;
12             if (y >= 0) tmp += y, y = 0;
13             if (z >= 0) tmp += z, z = 0;
14             return tmp / 2 >= -(x + y + z);
15         };
16 
17         while (lo <= hi) {
18             int mid = (lo + hi) >> 1;
19             if (check(mid)) {
20                 ret = mid;
21                 lo = mid + 1;
22             }
23             else {
24                 hi = mid - 1;
25             }
26         }
27         return ret;
28     }
29 };

 

posted @ 2020-09-06 13:30  人间有味_是清欢  阅读(162)  评论(0编辑  收藏  举报