Air condition(贪心)(维护区间)

题面翻译

  • 一个餐馆中有个空调,每分钟可以选择上调 11 个单位的温度或下调 11 个单位的温度,当然你也可以选择不变,初始的温度为 �m 。
  • 有 �n 个食客,每个食客会在 ��ti​ 时间点到达,他所能适应的最低温度是 ��li​ ,最高温度是 ℎ�hi​ ,他只会在 ��ti​ 时刻逗留。
  • 如果温度不在食客的适应范围内,他就会不舒服,请你判断,空调能否使得 �n 位来就餐的食客都感到舒服。
  • 本题多组数据,数据组数不大于 500500。
  • 1≤�≤1001≤n≤100,−109≤�,��,ℎ�≤109−109≤m,li​,hi​≤109,1≤��≤1091≤ti​≤109。

题目描述

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: ��ti​ — the time (in minutes) when the �i -th customer visits the restaurant, ��li​ — the lower bound of their preferred temperature range, and ℎ�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 �i -th customer is satisfied if and only if the temperature is between ��li​ and ℎ�hi​ (inclusive) in 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 �q ( 1≤�≤5001≤q≤500 ). Description of the test cases follows.

The first line of each test case contains two integers �n and �m ( 1≤�≤1001≤n≤100 , −109≤�≤109−109≤m≤109 ), 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 ��ti​ , ��li​ , and ℎ�hi​ ( 1≤��≤1091≤ti​≤109 , −109≤��≤ℎ�≤109−109≤li​≤hi​≤109 ), where ��ti​ is the time when the �i -th customer visits, ��li​ is the lower bound of their preferred temperature range, and ℎ�hi​ 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 00 .

输出格式

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

维护区间就行,r选小值,l选大值 

#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 110;
long long t[N], low[N], high[N],len[N];
int main() {
	int q, n=0;
	long long l,r,m;
	cin >> q;
	for (int i = 1; i <= q; i++) {
		cin >> n >> m;
		for (int j = 1; j <= n; j++) {
			cin >> t[j] >> low[j] >> high[j];
		};
		int flag = 1;
		for (int j = 1; j <= n; j++)len[j] = t[j] - t[j - 1];
		//for (int j = 1; j <= n; j++)cout << len[j] << " ";
		//cout << endl;
		l = m - len[1];
		r = m + len[1];
		len[1] = 0;
		for (int j = 1; j <= n; j++) {
			//cout << l << " " << r << endl;
			if ((l - len[j]) > high[j] || (r + len[j]) < low[j]) {
				//cout << l - len[j] << " " << r + len[j] << " " << l << " " << r<<" " << low[j] << " " << high[j] << endl;
				flag = 0;
				break;
			}
			else {
				l -= len[j];
				r += len[j];
				l = max(l, low[j]);
				r = min(r, high[j]);
				
			};
		}; 
		if (flag == 0) {
			cout << "NO" << endl;
		}
		else {
			cout << "YES" << endl;
		};
	};
	return 0;
};

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值