1. 程式人生 > 其它 >CF1304C Air Conditioner 題解

CF1304C Air Conditioner 題解

洛谷題目地址

CF題目地址

思路:看到資料範圍,首先想到這個演算法上界是 O(TNlogV)

考慮最簡單的情況:n=2 時,兩者時間差為t

讓顧客2滿足要求,則需要顧客1的當前溫度M_1加上 t 或減去 t 在[L_2,R_2]範圍內

那麼很顯然,我們並不需要考慮M_1具體是多少,只要求出它的左右邊界即可

對於顧客3,也是可以這樣考慮顧客2,依次類推,設計出一個演算法:

設定邊界 L,R 是當前第 i 個人可以調節到的溫度,則 [L-t,R+t] 是第 i+1 個人能調節到的溫度

判斷一下其與 [L_i+1,R_i+1] 的交集是否為空集即可。

然後再一次取得 L,R 的範圍

程式碼如下:

 1
#include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 using namespace std; 5 const int maxn = 105; 6 int n,m; 7 struct cust { 8 int l,r,t; 9 cust() { 10 l = r = t = 0; 11 } 12 bool operator < (const cust& p)const { 13 return t < p.t;
14 } 15 }a[maxn]; 16 void work() { 17 scanf("%d%d",&n,&m); 18 for(int i = 1;i <= n;++ i)scanf("%d%d%d",&a[i].t,&a[i].l,&a[i].r); 19 sort(a + 1 , a + 1 + n); 20 int L = m,R = m; 21 for(int i = 1;i <= n;++ i) { 22 int val = a[i].t - a[i - 1].t;
23 L -= val; 24 R += val; 25 if(L > a[i].r||R < a[i].l) { 26 puts("NO"); 27 return ; 28 } 29 L = max(L , a[i].l); 30 R = min(R , a[i].r); 31 } 32 puts("YES"); 33 return ; 34 } 35 int main() { 36 int T; 37 scanf("%d",&T); 38 while(T --)work(); 39 return 0; 40 }
View Code