codeforces-519C

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;
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章