Divide The Students CodeForces - 1250L(二分+贪心)

A group of students has recently been admitted to the Faculty of Computer Sciences at the Berland State University. Now the programming teacher wants to divide them into three subgroups for practice sessions.

The teacher knows that a lot of programmers argue which language is the best. The teacher doesn’t want to hear any arguments in the subgroups, so she wants to divide the students into three subgroups so that no pair of students belonging to the same subgroup want to argue.

To perform this division, the teacher asked each student which programming language he likes. There are a students who answered that they enjoy Assembler, b students stated that their favourite language is Basic, and c remaining students claimed that C++ is the best programming language — and there was a large argument between Assembler fans and C++ fans.

Now, knowing that Assembler programmers and C++ programmers can start an argument every minute, the teacher wants to divide the students into three subgroups so that every student belongs to exactly one subgroup, and there is no subgroup that contains at least one Assembler fan and at least one C++ fan. Since teaching a lot of students can be difficult, the teacher wants the size of the largest subgroup to be minimum possible.

Please help the teacher to calculate the minimum possible size of the largest subgroup!

Input
The first line contains one integer t (1≤t≤5) — the number of test cases in the input. Then test cases follow.

Each test case consists of one line containing three integers a, b and c (1≤a,b,c≤1000) — the number of Assembler fans, Basic fans and C++ fans, respectively.

Output
For each test case print one integer — the minimum size of the largest subgroup if the students are divided in such a way that there is no subgroup that contains at least one Assembler fan and at least one C++ fan simultaneously.

Examples
Input
5
3 5 7
4 8 4
13 10 13
1000 1000 1000
13 22 7
Output
5
6
13
1000
14
Input
5
1 3 4
1000 1000 1
4 1 2
325 226 999
939 861 505
Output
3
667
3
517
769
Note
Explanation of the answers for the example 1:

The first subgroup contains 3 Assembler fans and 2 Basic fans, the second subgroup — 5 C++ fans, the third subgroup — 2 C++ fans and 3 Basic fans.
The first subgroup contains 4 Assembler fans, the second subgroup — 6 Basic fans, the third subgroup — 2 Basic fans and 4 C++ fans.
The first subgroup contains all Assembler fans, the second subgroup — all Basic fans, the third subgroup — all C++ fans.
The first subgroup contains all Assembler fans, the second subgroup — all Basic fans, the third subgroup — all C++ fans.
The first subgroup contains 12 Assembler fans and 2 Basic fans, the second subgroup — 1 Assembler fan and 13 Basic fans, the third subgroup — 7 Basic fans and 7 C++ fans.
思路:比较水的一道题目,最大值最小化,我们二分答案,然后进行判断。对于b来说,在a或者c满足的情况下,尽可能的去减少b的值,以尽可能的塞下不满足的那些。具体看代码:

#include<bits/stdc++.h>
#define ll long long
using namespace std;

inline int check(int x,int a,int b,int c)
{
	int flag=1;
	int asum=0,bsum=0,csum=0;
	asum=a-x;
	csum=c-x;
	bsum=b-x;
	if(asum>0&&bsum>0&&csum>0) return 0;//都大于当前最大值,就不行了。
	if(asum>0&&csum>0) return 0;//a和c都大于最大值,也不可以.
	if(asum>0&&max(b+csum,0)+asum>x) return 0;//这里注意要取最大值,因为b不能小于0.
	if(csum>0&&max(b+asum,0)+csum>x) return 0;
	if(bsum>0)
	{
		if(asum<0&&csum<0) return bsum+asum+csum<=0;
		else if(asum<0) return bsum+asum<=0;
		else if(csum<0) return bsum+csum<=0;
		else return 0;
	}
	return 1;
}
int main()
{
	int t,a,b,c;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d%d%d",&a,&b,&c);
		int l=1,r=3e3+100;
		int ans=0,mid;
		while(l<=r)
		{
			mid=l+r>>1;
			if(check(mid,a,b,c)) ans=mid,r=mid-1;
			else l=mid+1;
		}
		cout<<ans<<endl;
	}
	return 0;
}

努力加油a啊,(o)/~

扫描二维码关注公众号,回复: 10462246 查看本文章
发布了617 篇原创文章 · 获赞 64 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/starlet_kiss/article/details/105266147