codeforces A. Red and Blue Beans(数学思维)

这是一个关于编程竞赛的问题,任务是将一定数量的红色和蓝色豆子分装到若干个包裹中,每个包裹内必须同时包含红蓝两种豆子,并且豆子数量的差值不超过特定限制。当给定条件满足时,输出"YES",否则输出"NO"。题目提供C++和Java两种语言的解决方案,并分析了如何判断是否能够合法分配豆子的策略。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目描述

题目链接: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");

                }
            }


        }
    }

}

 
### Codeforces Div.2 比赛难度介绍 Codeforces Div.2 比赛主要面向的是具有基础编程技能到中级水平的选手。这类比赛通常吸引了大量来自全球不同背景的参赛者,包括大学生、高中生以及一些专业人士。 #### 参加资格 为了参加 Div.2 比赛,选手的评级应不超过 2099 分[^1]。这意味着该级别的竞赛适合那些已经掌握了一定算法知识并能熟练运用至少一种编程语言的人群参与挑战。 #### 题目设置 每场 Div.2 比赛一般会提供五至七道题目,在某些特殊情况下可能会更多或更少。这些题目按照预计解决难度递增排列: - **简单题(A, B 类型)**: 主要测试基本的数据结构操作和常见算法的应用能力;例如数组处理、字符串匹配等。 - **中等偏难题(C, D 类型)**: 开始涉及较为复杂的逻辑推理能力和特定领域内的高级技巧;比如图论中的最短路径计算或是动态规划入门应用实例。 - **高难度题(E及以上类型)**: 对于这些问题,则更加侧重考察深入理解复杂概念的能力,并能够灵活组合多种方法来解决问题;这往往需要较强的创造力与丰富的实践经验支持。 对于新手来说,建议先专注于理解和练习前几类较容易的问题,随着经验积累和技术提升再逐步尝试更高层次的任务。 ```cpp // 示例代码展示如何判断一个数是否为偶数 #include <iostream> using namespace std; bool is_even(int num){ return num % 2 == 0; } int main(){ int number = 4; // 测试数据 if(is_even(number)){ cout << "The given number is even."; }else{ cout << "The given number is odd."; } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值