A and B and Team Training------贪心

A and B are preparing themselves for programming contests.

An important part of preparing for a competition is sharing programming knowledge from the experienced members to those who are just beginning to deal with the contests. Therefore, during the next team training A decided to make teams so that newbies are solving problems together with experienced participants.

A believes that the optimal team of three people should consist of one experienced participant and two newbies. Thus, each experienced participant can share the experience with a large number of people.

However, B believes that the optimal team should have two experienced members plus one newbie. Thus, each newbie can gain more knowledge and experience.

As a result, A and B have decided that all the teams during the training session should belong to one of the two types described above. Furthermore, they agree that the total number of teams should be as much as possible.

There are n experienced members and m newbies on the training session. Can you calculate what maximum number of teams can be formed?

Input
The first line contains two integers n and m (0 ≤ n, m ≤ 5·105) — the number of experienced participants and newbies that are present at the training session.

Output
Print the maximum number of teams that can be formed.

Examples
Input
2 6
Output
2
Input
4 5
Output
3

Note

Let’s represent the experienced players as XP and newbies as NB.

In the first test the teams look as follows: (XP, NB, NB), (XP, NB, NB).

In the second test sample the teams look as follows: (XP, NB, NB), (XP, NB, NB), (XP, XP, NB).

题意

有n,m两队人,需要n,m两队再出人组成3人小队,有两种方式:1个n队的人和2个m的人组合,或者2个n队的人和1个m队的人组合,问最多能组合出多少小队

解题思路

很明显这是一道贪心的题目,思路比较简单,比较两个队的人数,人数较少的队伍出1个人,另一个队出2个人

代码实现:

#include<iostream>
using namespace std;
int main()
{
	int n, m;
	cin >> n >> m;
	int ans = 0;
	while (n >= 1 && m >= 2 || n >= 2 && m >= 1)
	//第一个条件是n<=m,第二个是n>m;两者满足其一即可
	{
		if (n <= m)
		{
			n--;
			m -= 2;
			ans++;
		}
		else
		{
			m--;
			n -= 2;
			ans++;
		}
	}
	cout << ans << endl;
	return 0;
}
发布了21 篇原创文章 · 获赞 25 · 访问量 2194

猜你喜欢

转载自blog.csdn.net/SDAU_LGX/article/details/104929271