题目描述
题目链接:codeforces A. Red and Blue Beans
You have r red and b blue beans. You’d like to distribute them among several (maybe, one) packets in such a way that each packet:
- has at least one red bean (or the number of red beans ri≥1);
- has at least one blue bean (or the number of blue beans bi≥1);
- the number of red and blue beans should differ in no more than d (or |ri−bi|≤d) 红豆和蓝豆的数量相差不超过d
Can you distribute all beans?
Input
The first line contains the single integer t (1≤t≤1000) — the number of test cases.
The first and only line of each test case contains three integers r, b, and d (1≤r,b≤109; 0≤d≤109) — the number of red and blue beans and the maximum absolute difference in each packet.
Output
For each test case, if you can distribute all beans, print YES. Otherwise, print NO.
You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES are all recognized as positive answer).
Example
Input
4
1 1 0
2 7 3
6 1 4
5 4 0
output
YES
YES
NO
NO
Note
In the first test case, you can form one packet with 1 red and 1 blue bean. The absolute difference |1−1|=0≤d.
In the second test case, you can form two packets: 1 red and 4 blue beans in the first packet and 1 red and 3 blue beans in the second one.
In the third test case, since b=1, you can form only one packet with 6 red and 1 blue beans. The absolute difference |6−1|=5>d.
In the fourth test case, since d=0 so each packet should contain the same number of red and blue beans, but r≠b. 在第四个测试用例中,由于d=0,所以每个包应该包含相同数量的红豆和蓝豆,但是r≠b。
题意
将r个红色豆子和b个蓝色豆子放入若干个袋子中。合法的放置方式满足以下三个条件:<1>每个袋子中至少有一个红色豆子<2>每个袋子中至少有一个红色豆子<3>每个豆子中红色豆子数和蓝色豆子数之差的绝对值要小于d。测试数据有t组,每组给出三个数字r,b,d(意义同上述)。问r,b,d能否在某种方案下合法地放入袋子。能放入则输出YES,否则输出NO。
分析
1.最容易使放置合法的方法是:尽量使每个袋子中红色豆子和蓝色袋子差值最小。由于每个袋子中都必须同时有红色豆子和蓝色豆子,所以把袋子数取成红色豆子数和蓝色豆子数中较小的一个(记为n),也就是先把数量少的一种豆子都先放入袋子,每个袋子中都只放一个,则共有n个袋子。确定了袋子个数为n个,则此时每个袋子中含有1个某种颜色豆子。若此时合法的最大差值为d,则每一个袋子中都还可以放入d个另一种颜色的豆子。容易想到在合法的情况下,另一种颜色豆子数最多为n*(d+1)。若超过n*(d+1)个,把多余的豆子放到前n个袋子中,则与条件<3>冲突,重新放一个袋子则与条件<1>或<2>冲突。若另一种颜色的豆子不足n*(d+1)个,将这些豆子放入袋子,最大差值必定可以小于d。由于另一种颜色的豆子数大于等于n,则可以使每个袋子中都有两种颜色的豆子。
2.特殊情况:r==b时,可以使差值d为0,必定合法。差值为d为0且r不等于b则必定不合法。
C++ 代码
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long LL;
int main(){
int t;
LL r,b,d;
cin>>t;
while(t--){
cin>>r>>b>>d;
if(r==b){
cout<<"YES"<<endl;
continue;
}
else if(d==0&&r!=b){
cout<<"NO"<<endl;
continue;
}
else{
int m=max(r,b);
int n=min(r,b);
if(m<=n*(1+d)) cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
}
return 0;
}
java代码
import java.util.*;
import java.math.*;
public class Main {
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
int t;
while(cin.hasNext() ){
t=cin.nextInt();
for(int i=1;i<=t;i++){
long r,b,d;
r=cin.nextLong();
b=cin.nextLong();
d=cin.nextLong();
if(r==b){
System.out.println("YES");
continue;
}
else if(d==0&&r!=b){
System.out.println("NO");
continue;
}
else{
long m=Math.max(r,b);
long n=Math.min(r,b);
if(m<=n*(1+d)) System.out.println("YES");
else System.out.println("NO");
}
}
}
}
}