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

【CF1304C 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: $ 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.

  • 一個餐館中有個空調,每分鐘可以選擇上調 \(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\)

思路

我們定義一開始的溫度上限為 \([m,m]\)

先對 \(t_i\) 進行排序,假設差為 \(d_i\),則此時溫度的範圍為 \([l-d_i, r+d_i]\)

如果不存在交集,則輸出 NO

然後我們更新當前可控的溫度範圍,繼續計算範圍。

總結

這題一開始想的是dp,想了很多種方法似乎都不行。

看了題解之後恍然大悟,對於這類區間上下界的問題,可以考慮一下當前可以的區間最低和最高集取交集。

Code

// Problem: CF1304C Air Conditioner
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/CF1304C
// Memory Limit: 250 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include<bits/stdc++.h>
using namespace std;
//#define int long long
inline int read(){int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;
ch=getchar();}while(ch>='0'&&ch<='9'){x=(x<<1)+
(x<<3)+(ch^48);ch=getchar();}return x*f;}
#define N 110
//#define M
//#define mo
struct node
{
	int t, l, r; 
}a[N]; 
int n, m, i, j, k; 
int l, r, t; 

bool cmp(node x, node y)
{
	return x.t<y.t; 
}

signed main()
{
//	freopen("tiaoshi.in","r",stdin);
//	freopen("tiaoshi.out","w",stdout);
	t=read(); 
	while(t--)
	{
		n=read(); m=read(); 
		for(i=1; i<=n; ++i)
		{
			a[i].t=read(); 
			a[i].l=read(); a[i].r=read(); 
		}
		sort(a+1, a+n+1, cmp); 
		l=r=m; 
		for(i=1; i<=n; ++i)
		{
			l-=(a[i].t-a[i-1].t); 
			r+=(a[i].t-a[i-1].t); 
			if(l>a[i].r || r<a[i].l) break; 
			l=max(l, a[i].l); r=min(r, a[i].r); 
		}
		printf(i<=n ? "NO\n" : "YES\n"); 
	}
	return 0;
}

作者: zhangtingxi 轉載請註明出處: https://www.cnblogs.com/zhangtingxi/p/15826957.html