Problem Description
Snuke loves puzzles.
Today, he is working on a puzzle using
S
- andc
-shaped pieces. In this puzzle, you can combine twoc
-shaped pieces into oneS
-shaped piece, as shown in the figure below:
Snuke decided to create as many
Scc
groups as possible by putting together oneS
-shaped piece and twoc
-shaped pieces.Find the maximum number of
Scc
groups that can be created when Snuke has NNS
-shaped pieces and MMc
-shaped pieces.Constraints
- 1≤N,M≤10121≤N,M≤1012
Input
The input is given from Standard Input in the following format:
N M
Output
Print the answer.
Example
Sample Input 1
1 6
Sample Output 1
2
Two Scc groups can be created as follows:Combine two c-shaped pieces into one S-shaped piece
Create two Scc groups, each from one S-shaped piece and two c-shaped piecesSample Input 2
12345 678901
Sample Output 2
175897
题意:给出 n 个 s,m 个 c,已知两个 c 可以组成一个 s,问根据所给的 s 和 c 最多能组成多少个 scc
思路:
按题意模拟即可
一个 s 需要两个 c,那么 n 个 s 需要 2*n 个 c,且 4 个 c 可组成一个 scc,因此根据 2*n、m 的大小关系进行讨论:
- n*2<=m 时:说明现有的 s 均能被选用,c 有多余,那么有 n 组 scc,此后计算剩余 c 的个数,除以 4 累加即可
- n*2>m 时:说明现有的 c 均能被选用,s 有多余,那么有 m/2 组 scc
Source Program
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#define EPS 1e-9
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LL long long
const int MOD = 1E9+7;
const int N = 100000+5;
const int dx[] = {0,0,-1,1,-1,-1,1,1};
const int dy[] = {-1,1,0,0,-1,1,-1,1};
using namespace std;
int main(){
LL n,m;
scanf("%lld%lld",&n,&m);
LL res=0;
if(n*2<=m){
res+=n;
m-=n*2;
if(m>=4)
res+=m/4;
}
else{
res+=m/2;
}
printf("%lld\n",res);
return 0;
}