CF C. A and B and Team Training

C. A and B and Team Training
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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
Copy
2 6
output
Copy
2
input
Copy
4 5
output
Copy
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个新手,可以1个老手2个新手组成一队,也可以2个老手1个新手组成一队,问最多能组成多少支队伍?

解题思路:

枚举,我们假定先是1个老手和2个新手组成一队,那么我们就枚举选了0个老手到n个老手的情况,比如说选了i个老手(也就是选了2*i个新手),首先默认已经能组成i支队伍,那么剩余的老手个数是n-i,剩余的新手个数是m-2*i,如果剩余的新手个数小于0,说明已经这种组队方式新手是不够用的,结束(选更多老手就更不够了)。否则本回合组成的队伍数还要加上选了2个老手1个新手的情况(从剩余的人找),然后更新答案。

#include<iostream>
using namespace std;
int main(){
	int n,m,ans=0;
	cin>>n>>m;
	for(int i=0;i<=n;i++){
		int cur=i;
		int leftn=n-i;
		int leftm=m-2*i;
		if(leftm>=0){
			cur+=min(leftn/2,leftm);
			ans=max(cur,ans);
		}
		else break;
	}
	cout<<ans;
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_37609825/article/details/80191180