【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;
}

posted @ 2022-01-20 16:38  zhangtingxi  阅读(49)  评论(0编辑  收藏  举报