Codeforces 950A Left-handers, Right-handers and Ambidexters 贪心

题目链接:Left-handers, Right-handers and Ambidexters

题意

l 个擅长使用左手的人, r 个擅长使用右手的人,有 a 个左右手都擅长的人,要求组建一支人数为人偶数的队伍,队伍中使用左手的人数和右手的人数一样多(每个人都只能使用其中一只自己擅长的手),求队伍的最大人数。

输入

输入只包含 3 个整数 l , r , a   ( 0 l , r , a 100 )

输出

输出能组建的队伍的最大人数。

样例

输入
1 4 2
输出
6
提示
6 个人中有 1 个人只擅长使用左手, 2 个两只手都擅长的人只使用左手, 3 个擅长使用右手的人。
输入
5 5 5
输出
14
提示
14 个人中 5 个人只擅长使用左手, 5 个人只擅长使用右手, 4 个两只手都擅长的人 2 个人只使用右手, 2 个人只使用左手。
输入
0 2 0
输出
0

题解

先将 a 个人分配给人数少的那一边,使得擅长左手的人数和擅长右手的人数尽量相等,如果还有多余人数,就自行两两组队。

过题代码

#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;
}

猜你喜欢

转载自blog.csdn.net/CSDNjiangshan/article/details/81388589