Air Conditioner
题面翻译
-
一个餐馆中有个空调,每分钟可以选择上调 $1$ 个单位的温度或下调 $1$ 个单位的温度,当然你也可以选择不变,初始的温度为 $m$ 。
-
有 $n$ 个食客,每个食客会在 $t_i$ 时间点到达,他所能适应的最低温度是 $l_i$ ,最高温度是 $h_i$ ,他只会在 $t_i$ 时刻逗留。
-
如果温度不在食客的适应范围内,他就会不舒服,请你判断,空调能否使得 $n$ 位来就餐的食客都感到舒服。
-
本题多组数据,数据组数不大于 $500$。
-
$1\le n\le 100$,$-10^9\le m,l_i,h_i\le 10^9$,$1\le t_i\le 10^9$。
-
translate by @ShineEternal。
题目描述
Gildong owns a bulgogi restaurant. The restaurant has a lot of customers, so many of them like to make a reservation before visiting it.
Gildong tries so hard to satisfy the customers that he even memorized all customers’ preferred temperature ranges! Looking through the reservation list, he wants to satisfy all customers by controlling the temperature of the restaurant.
The restaurant has an air conditioner that has 3 states: off, heating, and cooling. When it’s off, the restaurant’s temperature remains the same. When it’s heating, the temperature increases by 1 in one minute. Lastly, when it’s cooling, the temperature decreases by 1 in one minute. Gildong can change the state as many times as he wants, at any integer minutes. The air conditioner is off initially.
Each customer is characterized by three values: $ t_i $ — the time (in minutes) when the $ i $ -th customer visits the restaurant, $ l_i $ — the lower bound of their preferred temperature range, and $ h_i $ — the upper bound of their preferred temperature range.
A customer is satisfied if the temperature is within the preferred range at the instant they visit the restaurant. Formally, the $ i $ -th customer is satisfied if and only if the temperature is between $ l_i $ and $ h_i $ (inclusive) in the $ t_i $ -th minute.
Given the initial temperature, the list of reserved customers’ visit times and their preferred temperature ranges, you’re going to help him find if it’s possible to satisfy all customers.
输入格式
Each test contains one or more test cases. The first line contains the number of test cases $ q $ ( $ 1 \le q \le 500 $ ). Description of the test cases follows.
The first line of each test case contains two integers $ n $ and $ m $ ( $ 1 \le n \le 100 $ , $ -10^9 \le m \le 10^9 $ ), where $ n $ is the number of reserved customers and $ m $ is the initial temperature of the restaurant.
Next, $ n $ lines follow. The $ i $ -th line of them contains three integers $ t_i $ , $ l_i $ , and $ h_i $ ( $ 1 \le t_i \le 10^9 $ , $ -10^9 \le l_i \le h_i \le 10^9 $ ), where $ t_i $ is the time when the $ i $ -th customer visits, $ l_i $ is the lower bound of their preferred temperature range, and $ h_i $ is the upper bound of their preferred temperature range. The preferred temperature ranges are inclusive.
The customers are given in non-decreasing order of their visit time, and the current time is $ 0 $ .
输出格式
For each test case, print “YES” if it is possible to satisfy all customers. Otherwise, print “NO”.
You can print each letter in any case (upper or lower).
样例输入 #1
4
3 0
5 1 2
7 3 5
10 -1 0
2 12
5 7 10
10 16 20
3 -100
100 0 0
100 -50 50
200 100 100
1 100
99 -100 0
样例输出 #1
YES
NO
YES
NO
提示
In the first case, Gildong can control the air conditioner to satisfy all customers in the following way:
- At $ 0 $ -th minute, change the state to heating (the temperature is 0).
- At $ 2 $ -nd minute, change the state to off (the temperature is 2).
- At $ 5 $ -th minute, change the state to heating (the temperature is 2, the $ 1 $ -st customer is satisfied).
- At $ 6 $ -th minute, change the state to off (the temperature is 3).
- At $ 7 $ -th minute, change the state to cooling (the temperature is 3, the $ 2 $ -nd customer is satisfied).
- At $ 10 $ -th minute, the temperature will be 0, which satisfies the last customer.
In the third case, Gildong can change the state to heating at $ 0 $ -th minute and leave it be. Then all customers will be satisfied. Note that the $ 1 $ -st customer’s visit time equals the $ 2 $ -nd customer’s visit time.
In the second and the fourth case, Gildong has to make at least one customer unsatisfied.
算法1
(暴力枚举) $O(n^2)$
0.没想到的地方
1.如何控制最佳温度?
该时间能够表示的所有温度范围和顾客能接受的温度取交集
简要代码
//初始状态
int l=m,r=m;
//经过t时间后
l=l-t; //最低温度
r=r-t; //最高温度
cin>>a>>b; //[a,b] - 顾客能接受的温度范围
//范围中的最优值
l = max(a,l);
r = min(b,r);
2.什么时候是不可以?
当l>r,表示空集 ,此时为不可行的
温度的变化范围
1.初始状态:time = 0; [L,R] = [m,m]; - 区间里只有一个数
2.经过时间t后,[l-time][l+time];
3.时间t的计算:
从第i-1位到i位,时间间隔为 time=t[i]-t[i-1]
;
C++ 代码
#include <bits/stdc++.h>
using namespace std;
const int N = 150;
int T,n,m;
struct people {
int t,l,r;
} p[N];
int main() {
cin>>T;
while(T--) {
cin>>n>>m;
for(int i=1; i<=n; i++) {
cin>>p[i].t>>p[i].l>>p[i].r;
}
int a=m,b=m; //起初[a,b] ->[m,m] 只有一数-起始温度
bool flag = 1;
for(int i=1; i<=n; i++) {
int time = p[i].t - p[i-1].t;
// cout<<"time: "<<time<<endl;
//计算出所有可能的范围 [l-t][r+t]
a-=time;
b+=time;
// cout<<"all:"<<endl;
// cout<<"a= "<<a<<",b= "<<b<<endl;
//与当前顾客的温度取最优值 - 使得变化范围小
a=max(a,p[i].l);
b=min(b,p[i].r);
// cout<<"after:"<<endl;
// cout<<"a= "<<a<<",b= "<<b<<endl;
if(a > b) { //表示空集
flag=0;
break;
}
}
// cout<<endl<<endl;
if(flag) cout<<"Yes"<<endl;
else cout<<"No"<<endl;
}
return 0;
}