小朋友学Codeforces(2):Round 454 DIV 2, A

本文解析了907A.Masha和熊问题的两种解法,介绍了如何根据四个人(熊爸爸、熊妈妈、熊宝宝和Masha)的体积找到三辆车的合适大小,确保每个人都能钻进并满意于分配到的车辆。

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

题目

907A. Masha and Bears

题意

人的体积为VV,车的大小为sizesize,人能钻进车的条件是V≤sizeV≤size,人对车满意的条件是2V≥size2V≥size.
现知道
熊爸爸能钻进最大的车并且满意
熊妈妈能钻进中等的车并且满意
熊宝宝能钻进最小的车并且满意
Masha能钻进最小的车并且只对它满意
给定四人的体积(保证V1>V2>V3),要求给出三辆车的大小。

解法(一)

思路:
假设三辆车大小分别为a,b,c,则有
V1≤a≤2V1//熊爸爸
V2≤b≤2V2//熊妈妈
V3≤c≤2V3//熊宝宝
V4≤c≤2V4//Masha
2V4

#include <bits/stdc++.h>
using namespace std;

int main() 
{
    int x1, x2, x3, x4;
    scanf("%d %d %d %d", &x1, &x2, &x3, &x4);

    int ans1 = 2 * x1;
    int ans2 = 2 * x2;
    int ans3;

    if (x3 == x4) 
    {
        ans3 = x3;
    }
    else if (x3 > x4) 
    {
        if (x3 <= 2 * x4) 
        {
            ans3 = x3;
        }
        else 
        {
            ans3 = -1;
        }
    }
    else 
    {
        if (x4 <= 2 * x3) 
        {
            ans3 = x4;
        }
        else 
        {
            ans3 = -1;
        }
    }

    if (ans3 == -1) 
    {
        puts("-1");
    }
    else 
    {
        if (2 * x4 < ans2) 
        {
            printf("%d\n%d\n%d\n", ans1, ans2, ans3);
        }
        else 
        {
            puts("-1");
        }
    }

    return 0;
}

运行结果:

50 30 10 10
100
60
10

解法(二)

思路:
每辆车对熊来说有下面三种可能:
不能进去的车V>C;
能进去但不喜欢的车V<2V

#include <stdio.h>

#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)>(b)?(b):(a))

int main() 
{
    int father, mother, son, masha;
    int car1, car2, car3;
    scanf("%d %d %d %d", &father, &mother, &son, &masha);
    bool flag = false;

    for (car3 = max(son, masha); car3 <= min(2 * son, 2 * masha) && false == flag; ++car3) 
    {
        for (car2 = max(mother, max(car3 + 1, 2 * masha + 1)); car2 <= 2 * mother && false == flag; ++car2) 
        {
            for (car1 = max(father, max(car2 + 1, 2 * masha + 1)); car1 <= 2 * father && false == flag; ++car1) 
            {
                    flag = true;
                    printf("%d\n%d\n%d", car1, car2, car3);
            }
        }
    }

    if (flag == false)
    {
        printf("-1");
    }

    return 0;
}

运行结果:

50 30 10 10
50
30 
10



更多内容请关注微信公众号
wechat.jpg

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值