codeforces-519C

来源:互联网 发布:前端 后端 数据库 编辑:程序博客网 时间:2024/04/29 23:23

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.

Example
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).

题意:ACM组队打比赛 有两种方案 一种是两个大佬带一个菜鸡 还有一种是一个大佬带两个菜鸡 ,告诉你大佬和菜鸡的数量问你最多可以组多少队
思路:神踏马3分。。我看了看我可爱的队友的代码一行输入一行计算一行输出。。。心态有点崩2333还是讲讲我谜一样的三分。。三分的条件是有两个单调区间,一个递增一个递减,这里我们三分第一种方案的数量,那么总数量必定是随着第一种方案的数量 总数先增后减 符合三分要求

#include<iostream>#include<algorithm>#include<vector>#include<queue>#include<vector>#include<cmath>#include<cstdio>#include<cstring>#include<string>#include<stack>#include<map>using namespace std;//thanks to pyf ...#define INF 0x3f3f3f3f#define CLR(x,y) memset(x,y,sizeof(x))typedef pair<int,int> PII;typedef long long ll;const int N = 1e5+5;int cal(int tar,int n,int m) // tar = 1EX + 2NB{//  if(n<tar || m/2 < tar)//      return 0;    return tar+min((n-tar)/2,m-2*tar);}int three_div(int n,int m){    int l = 0,r = min(n,m/2);    int ans = 0;    int mid , mmid;    while(l<=r)    {        int mid = (l+r)/2;        int mmid = (mid+r+1)/2;//贼玄学。。为了避免mid和mmid重合        int res1 = cal(mid,n,m);        int res2 = cal(mmid,n,m);        ans = max(ans,max(res1,res2));//      cout << mid << " " << mmid << " " << res1 << " " << res2 << endl;        if(res1<=res2)         {            l = mid+1;        }        else             r = mmid-1;    }    return ans;}int main(){    int n,m;    while(cin >> n >> m)    {        int ans ;        if(n==2*m||m==2*n)            ans = min(n,m);        else            ans = three_div(n,m);        cout << ans << endl;    }}
0 0
原创粉丝点击