C. Air Conditioner(区间交集)
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: hi — 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 ti-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 1≤q≤500). Description of the test cases follows.
The first line of each test case contains two integers m is the initial temperature of the restaurant.
Next, 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).
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
YES NO YES NO
In the first case, Gildong can control the air conditioner to satisfy all customers in the following way:
- At heating (the temperature is 0).
- At off (the temperature is 2).
- At 1-st customer is satisfied).
- At off (the temperature is 3).
- At 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 2-nd customer's visit time.
In the second and the fourth case, Gildong has to make at least one customer unsatisfied.
思路:每个顾客都有它的温度范围,首先肯定能想到先按到店时间排个序。然后任意两个人之间存在时间差x,那么在这个时间差内变化最大为x,最小变化为-x。知道当前区间之后,我们就会发现只要当前区间和客人的区间存在交集,就能够满足。然后,就是缩小区间的范围,只有两种情况,枚举以下就可以了。 (x l y r ) 和 (l x r y) 这两种情况
#include<iostream> #include<cstdio> #include<algorithm> #include<cmath> #include<utility> #include<cstring> #include<string> #include<vector> #include<stack> #include<set> #include<map> #include<bitset> #define inf 0x3f3f3f3f using namespace std; typedef long long LL; typedef pair<int,int> pll; const int maxn= 2e5+10; const int mod =1e9+7; const double EPS = 1e-6; struct node { int ti; int l; int r; }; node a[105]; bool cmp( node x, node y) { return x.ti<y.ti; } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t; cin >> t; while(t--) { int n,m; cin >> n >> m; for(int i=1;i<=n;i++) { cin >> a[i].ti >> a[i].l >> a[i].r; } sort(a+1,a+1+n,cmp); LL x=m,y=m; int time=0; int flag=0; for(int i=1;i<=n;i++) { int d=a[i].ti-time; x-=d; y+=d; if(x<=a[i].r&&a[i].l<=y)//区间存在交集 { if(x<a[i].l) x=a[i].l; if(y>a[i].r) y=a[i].r; time=a[i].ti; } else { flag=1; break; } } if(flag) { cout << "NO" << endl; } else { cout << "YES" << endl; } } return 0; }