Educational Codeforces Round 89 (Rated for Div. 2) A-Shovels and Swords(思维贪心)

地址:http://codeforces.com/contest/1366/problem/A

题意:给出a,b的数目

a  b

1a+2b->得一个奖励

2a+1b->得一个奖励

求最大奖励数

解析:

规定:a<=b

肯定对大的来讲,尽量让它先-2。

那么如果得a个奖励,那么消耗2*a个b,如果b>=2*a,这个时候,直接输出a

否则:b<2*a:这个时候,不仅仅是a要消耗1,b也是有机会消耗1的。

假设两种物品分别合成了x,y个

那么消耗了2*x+y个a

x+2*y个b

那么有:2*x+y<=a,x+2*y<=b,相加得:x+y<=(a+b)/3,即为答案。

关于这个结论,还可以这么解释:

b每次消耗2,a每次消耗1,当b<=a时,轮到a每次消耗2,b每次消耗1,但是无论怎么样,每次消耗的都是(a+b)-3

当剩余(a+b)<3时,无论如何不能再合成了,也算是%3的余数了。所以答案一定是:(a+b)/3

#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
const int maxn=8e3+10;
typedef long long ll;
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        ll a,b;
        cin>>a>>b;
        if(a>b)
            swap(a,b);
        if(b>=2*a)
            cout<<a<<endl;
        else
            cout<<(a+b)/3<<endl;
    }    
}

 

posted @ 2020-06-12 19:57  liyexin  阅读(206)  评论(0编辑  收藏  举报