【每日一题】CF1304 C. Air Conditioner | 模拟 | 简单

题目内容

原题链接

初始空调温度为 m m m ,按照时间非降序依次进来 n n n 个客人,第 i i i 个客人在到店那一时刻 t i t_i ti 的满意温度为 [ l o w i , h i g h i ] [low_i,high_i] [lowi,highi] ,每分钟可以将空调温度升高 1 1 1 或减少 1 1 1 或不变,问是否可以让这 n n n 个客人在到店时刻的温度都满意。

数据范围

  • 2 ≤ n ≤ 100 2\leq n\leq 100 2n100
  • − 1 0 9 ≤ m , l o w i , h i g h i ≤ 1 0 9 -10^9\leq m,low_i,high_i\leq 10^9 109m,lowi,highi109
  • 1 ≤ t i ≤ 1 0 9 1\leq t_i\leq 10^9 1ti109

题解

考虑我们达到时间 t i t_i ti 的时候,可以达到的 [ l o w i , h i g h i ] [low_i,high_i] [lowi,highi] 的哪部分区间,一定是一段连续的区间。

然后从 t i t_i ti t i + 1 t_{i+1} ti+1 最多可以将温度升高或减少 t i + 1 − t i t_{i+1}-t_i ti+1ti,即 [ − ( t i + 1 − t i ) , t i + 1 − t i ] [-(t_{i+1}-t_i),t_{i+1}-t_i] [(ti+1ti),ti+1ti]

看每个人的合适温度区间在其到店时刻是否可以达到,如果存在一个不能达到,则为 NO

这里需要注意一点,可能存在多个人在同一时刻到店,我们需要将温度调整成所有人的满意温度,可以预处理出所有同一时刻到店的人的满意温度区间交集。

时间复杂度: O ( n ) O(n) O(n)

代码

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

void solve() {
    int n, m;
    cin >> n >> m;

    vector<array<int, 3>> vec(n);
    for (int i = 0; i < n; ++i) {
        cin >> vec[i][0] >> vec[i][1] >> vec[i][2];
    }

    int g = 0;
    for (int i = 0; i < n; ++i) {
        int l = vec[i][1], r = vec[i][2];
        int j = i + 1;
        while (j < n && vec[j][0] == vec[i][0]) {
            l = max(l, vec[j][1]);
            r = min(r, vec[j][2]);
            j += 1;
        }
        if (l > r) {
            cout << "NO\n";
            return;
        }

        vec[g++] = {vec[i][0], l, r};
        i = j - 1;
    }

    n = g;
    
    int l = m, r = m;
    int t = 0;
    for (int i = 0; i < n; ++i) {
        // 前一个状态可以在 [l, r]
        int cht = vec[i][0] - t;
        int next_r = min(vec[i][2], r + cht);
        int next_l = max(vec[i][1], l - cht);
        if (next_l > next_r) {
            cout << "NO\n";
            return;
        }
        l = next_l;
        r = next_r;
        t = vec[i][0];
    }

    cout << "YES\n";
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int T = 1;
    cin >> T;
    while (T--) {
        solve();
    }

    return 0;
}
  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据目要求,我们需要设计一个系统来描述家电制造商和它们所制造的电器。我们可以先列出以下几个类: 1. 家电制造商(Manufacturer):该类用于描述家电制造商,包括制造商的名称(name)、所生产的电器列表(products)等属性,以及向产品列表中添加电器(addProduct)、从产品列表中删除电器(deleteProduct)等方法。 2. 电器(Appliance):该类用于描述电器,包括电器的名称(name)、品牌(brand)、型号(model)、价格(price)等属性,以及打开电器(turnOn)、关闭电器(turnOff)等方法。 3. 电视机(Television):该类继承自电器类,包括电视机的屏幕尺寸(screenSize)、分辨率(resolution)等属性,以及调节音量(adjustVolume)、切换频道(changeChannel)等方法。 4. 空调(AirConditioner):该类继承自电器类,包括空调的制冷能力(coolingCapacity)、制热能力(heatingCapacity)等属性,以及调节温度(adjustTemperature)、调节风速(adjustWindSpeed)等方法。 5. 冰箱(Refrigerator):该类继承自电器类,包括冰箱的容积(volume)、制冷方式(coolingMethod)等属性,以及设置温度(setTemperature)、打开冰箱门(openDoor)等方法。 下面是代码实现: ``` class Manufacturer { private String name; private List<Appliance> products; public Manufacturer(String name) { this.name = name; products = new ArrayList<>(); } public void addProduct(Appliance appliance) { products.add(appliance); } public void deleteProduct(Appliance appliance) { products.remove(appliance); } } class Appliance { protected String name; protected String brand; protected String model; protected double price; protected boolean isOn; public Appliance(String name, String brand, String model, double price) { this.name = name; this.brand = brand; this.model = model; this.price = price; isOn = false; } public void turnOn() { isOn = true; System.out.println(name + " is turned on."); } public void turnOff() { isOn = false; System.out.println(name + " is turned off."); } } class Television extends Appliance { private double screenSize; private String resolution; public Television(String name, String brand, String model, double price, double screenSize, String resolution) { super(name, brand, model, price); this.screenSize = screenSize; this.resolution = resolution; } public void adjustVolume(int volumeLevel) { System.out.println("Volume level is adjusted to " + volumeLevel); } public void changeChannel(int channelNumber) { System.out.println("Channel is switched to " + channelNumber); } } class AirConditioner extends Appliance { private int coolingCapacity; private int heatingCapacity; public AirConditioner(String name, String brand, String model, double price, int coolingCapacity, int heatingCapacity) { super(name, brand, model, price); this.coolingCapacity = coolingCapacity; this.heatingCapacity = heatingCapacity; } public void adjustTemperature(int temperature) { System.out.println("Temperature is adjusted to " + temperature); } public void adjustWindSpeed(int windSpeedLevel) { System.out.println("Wind speed is adjusted to " + windSpeedLevel); } } class Refrigerator extends Appliance { private int volume; private String coolingMethod; public Refrigerator(String name, String brand, String model, double price, int volume, String coolingMethod) { super(name, brand, model, price); this.volume = volume; this.coolingMethod = coolingMethod; } public void setTemperature(int temperature) { System.out.println("Temperature is set to " + temperature); } public void openDoor() { System.out.println("Door is opened."); } } ``` 下面是几个相关问

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值