题目链接:Left-handers, Right-handers and Ambidexters
题意
有 个擅长使用左手的人, 个擅长使用右手的人,有 个左右手都擅长的人,要求组建一支人数为人偶数的队伍,队伍中使用左手的人数和右手的人数一样多(每个人都只能使用其中一只自己擅长的手),求队伍的最大人数。
输入
输入只包含 个整数 。
输出
输出能组建的队伍的最大人数。
样例
输入 |
---|
1 4 2 |
输出 |
6 |
提示 |
个人中有 个人只擅长使用左手, 个两只手都擅长的人只使用左手, 个擅长使用右手的人。 |
输入 |
---|
5 5 5 |
输出 |
14 |
提示 |
个人中 个人只擅长使用左手, 个人只擅长使用右手, 个两只手都擅长的人 个人只使用右手, 个人只使用左手。 |
输入 |
---|
0 2 0 |
输出 |
0 |
题解
先将 个人分配给人数少的那一边,使得擅长左手的人数和擅长右手的人数尽量相等,如果还有多余人数,就自行两两组队。
过题代码
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cstring>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <bitset>
#include <algorithm>
#include <sstream>
using namespace std;
#define LL long long
int l, r, a;
int main() {
#ifdef LOCAL
freopen("test.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
#endif // LOCAL
ios::sync_with_stdio(false);
while(scanf("%d%d%d", &l, &r, &a) != EOF) {
if(l > r) {
swap(l, r);
}
int ans = r - l;
if(ans > a) {
ans = 2 * (a + l);
} else {
ans = 2 * (ans + l) + (a - ans) / 2 * 2;
}
printf("%d\n", ans);
}
return 0;
}