跳到正文

题解

P13594『GTOI - 1A』Bath 题解

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

文章信息

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

版本

  1. v1当前

思路

可以看出,当你每一次要调水温时,都是调到 ll 到 rr 之间的,那具体调到多少呢? 我们可以设一个当前有可能的温度范围,每个时间点维护这个范围,当不存在这个范围时,就再调一次。 具体看代码。

代码

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
struct node{
	int t,x;//要记录一下各个时间点要调的水温
}a[100005];
int n,m,c,ans;
int l,r;
int b[100005];//相当于一个离散化
bool cmp(node x,node y){return x.t<y.t;}//按时间排序
signed main(){
	ios::sync_with_stdio(0);
	cin.tie(NULL),cout.tie(NULL);
	cin>>n>>c>>l>>r;
	for(int i=1;i<=n;i++) cin>>a[i].t>>a[i].x;
	sort(a+1,a+n+1,cmp);
	for(int i=1;i<=n;i++){
		if(a[i].t==a[i-1].t) b[m]+=a[i].x;//同一时间的合并到一起
		else b[++m]=a[i].x;
	}
	int cl,cr;
	cl=cr=c;//最开始可能的范围就只有最开始的温度
	for(int i=1;i<=m;i++){
		cl+=b[i];//因为加或减了这个温度,整个范围都移动一段
		cr+=b[i];
		if(cl<l) cl=l;//超过了l和r的不行
		if(cr>r) cr=r;
		if(cl>cr){//范围不存在时
			ans++;//次数加1
			cl=l,cr=r;//更新范围
		}
	}
	cout<<ans<<"\n";
	return 0;
}

谢谢观看。

原文