题解:P13594 『GTOI - 1A』Bath
P13594 『GTOI - 1A』Bath 题解
题目大意
小 H 的洗澡水初始温度为
对水温的影响有
小 H 可以在任意时刻将水温调至任意度,他希望能够接受任意时刻的水温,求调水温的最少次数。同一个时刻对水温的影响被认为是同时发生的。
思路
枚举每一时刻的水温。不难发现调水温时调至的水温不好确定,但是可接受范围的大小是一定的。
所以可以不断调整当前的水温范围大小,超过可接受的范围大小就将
代码
#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;
}