跳到正文

题解

题解:P13594 『GTOI - 1A』Bath

chenxi797约 2 分钟
原文
文章信息与版本

文章信息

发布
2025年8月6日
收录
2025年12月12日
更新
2026年11月6日
篇幅
约 2 分钟
获赞
4
题目
P13594

版本

  1. v1当前

P13594 『GTOI - 1A』Bath 题解

题目大意

小 H 的洗澡水初始温度为 ss 度,他能够接受的洗澡水温度在 ll 度到 rr 度之间。

对水温的影响有 nn 组,第 ii 组在 tit_i 时刻将水温升高 xix_i 度。

小 H 可以在任意时刻将水温调至任意度,他希望能够接受任意时刻的水温,求调水温的最少次数。同一个时刻对水温的影响被认为是同时发生的。

思路

枚举每一时刻的水温。不难发现调水温时调至的水温不好确定,但是可接受范围的大小是一定的。

所以可以不断调整当前的水温范围大小,超过可接受的范围大小就将 ans+1ans + 1。

代码

C++
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e5 + 5;
struct bath { int t,x;} a[N];
bool cmp(bath x,bath y) { return x.t < y.t;}
signed main()
{
	int n,s,l,r;
	cin >> n >> s >> l >> r;
	for (int i = 1;i <= n;i++) cin >> a[i].t >> a[i].x;
	int k = r - l + 1,ans = 0;
	sort(a + 1, a + n + 1,cmp);
	for (int i = 1;i <= n;i++)
	{
		if (a[i].t == a[i + 1].t) // 相同时间合并
		{
			a[i + 1].x += a[i].x;
			continue;
		}
		s += a[i].x; // 目前水温
		if (!ans && (s < l || s > r)) // 没调过水温并且目前水温超过可接受范围
		{
			ans++,l = r = s;
			continue;
		}
		l = min(l,s),r = max(r,s); // 更新
		if (r - l + 1 > k) // 更新之后可接受范围超过了原来的,需要再调一次
			ans++,l = r = s;
	}
	cout << ans;
	return 0;
}