Perfect Team CodeForces - 1221C(二分/推公式)

C. Perfect Team
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
You may have already known that a standard ICPC team consists of exactly three members. The perfect team however has more restrictions. A student can have some specialization: coder or mathematician. She/he can have no specialization, but can’t have both at the same time.

So the team is considered perfect if it includes at least one coder, at least one mathematician and it consists of exactly three members.

You are a coach at a very large university and you know that ? of your students are coders, ? are mathematicians and ? have no specialization.

What is the maximum number of full perfect teams you can distribute them into?

Note that some students can be left without a team and each student can be a part of no more than one team.

You are also asked to answer ? independent queries.

Input
The first line contains a single integer ? (1≤?≤104) — the number of queries.

Each of the next ? lines contains three integers ?, ? and ? (0≤?,?,?≤108) — the number of coders, mathematicians and students without any specialization in the university, respectively.

Note that the no student is both coder and mathematician at the same time.

Output
Print ? integers — the ?-th of them should be the answer to the ? query in the order they are given in the input. The answer is the maximum number of full perfect teams you can distribute your students into.

Example
inputCopy
6
1 1 1
3 6 0
0 0 0
0 1 1
10 1 10
4 4 1
outputCopy
1
3
0
0
1
3
Note
In the first example here are how teams are formed:

the only team of 1 coder, 1 mathematician and 1 without specialization;
all three teams consist of 1 coder and 2 mathematicians;
no teams can be formed;
no teams can be formed;
one team consists of 1 coder, 1 mathematician and 1 without specialization, the rest aren’t able to form any team;
one team consists of 1 coder, 1 mathematician and 1 without specialization, one consists of 2 coders and 1 mathematician and one consists of 1 coder and 2 mathematicians.

题意: 有a,b,c三种数,有多少种三个数的组合,使得每组数,至少一个a一个b。

思路: 二分是很显然的,但是还可以推公式。比赛的时候写的是二分。赛后看大牛的代码,貌似就3种情况:x,y,(x+y+z)/3。取最小值就可以了。理由的话,就是按种类数足的话,就按人数取,种类数不足的话,就按照种类数取。

但是这样感觉还是不自然(没有严谨的数学推理)。
假设三类数数目分别为x,y,z。令a = min(x, y) ,b = x + y + z - 3 * a。那么,a代表能组成最多多少组,b代表组成a组后剩下的人数。如果b >= 0,很明显可以组成a组,那就输出a。否则,那组成的组数就小于a组,那么此时所有数都分组,就是(x+y+z)/3,此时组数小于a组,那么种类数的条件一定能满足。

同理可证,x,y,z,d,每组4个人,至少一个x和y和z,则结果也是一样,min(x,y,z,(x+y+z+d))。更多的数也是这样推。

再拓展思考一下,要是是n种数,1~n-1种数有效,每组数有n个,至少m个不同有效种类数。问最多组成多少组?就贪心的一下选数目最多的m个有效种类数就可以了。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;

typedef long long ll;
int main()
{
    int T;scanf("%d",&T);
    while(T--)
    {
        ll a,b,c;scanf("%lld%lld%lld",&a,&b,&c);
        ll x = min(a,b);
        ll y = a + b + c - 3 * x;
        if(y >= 0)
        {
            printf("%lld\n",x);
        }
        else
        {
            printf("%lld\n",(3 * x + y) / 3);
        }
    }
    return 0;
}


二分

#include <cstdio>
#include <algorithm>
#include <algorithm>

using namespace std;

typedef long long ll;
const ll mod = 1e9 + 7;

int main()
{
    int n;scanf("%d",&n);
    while(n--)
    {
        int a,b,c;scanf("%d%d%d",&a,&b,&c);
        int r = min(a,b),l = 0;
        int sum = a + b + c;
        int ans = 0;
        while(l < r)
        {
            int m = (l + r + 1) >> 1;
            if(m * 3 == sum)
            {
                ans = m;
                break;
            }
            else if(m * 3 > sum)
            {
                r = m - 1;
            }
            else
            {
                ans = m;
                l = m;
            }
        }

        printf("%d\n",ans);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值