题解:P13594 『GTOI - 1A』Bath
题目传送门
前言
赛场上由于 WA 了很多次,所以口胡的一个做法也就顺理成章地通过了。
思路
由于这题有每个人使用水龙头的时刻
然后可以发现,题目要求每个人来时水温都要控制在
容易发现,我们并不需要知道调节水温需要调到多少,我们只要知道调节次数即可。
考虑极端情况,由于最多可以在
然后就可以用线性
然后就能用这个口胡的做法通过本题。
代码
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=1e5+5;
int n,s,l,r,b[N],ans;
struct node{
int t,x;
}a[N];
bool cmp(node x,node y){return x.t<y.t;}//按时间排序
signed main(){
scanf("%lld%lld%lld%lld",&n,&s,&l,&r);
for(int i=1;i<=n;i++) scanf("%lld%lld",&a[i].t,&a[i].x);
sort(a+1,a+n+1,cmp);
int last=0,cnt=0;
a[0]={-1,0};
for(int i=1;i<=n;i++){//合并同一时刻
if(a[i].t==a[last].t) b[cnt]+=a[i].x;
else b[++cnt]=a[i].x,last=i;
}
int tot=s-l,p=r-l;
for(int i=1;i<=cnt;i++){//线性计算
tot+=b[i];
if(tot>p||tot<-p) ans++,tot=0;
}printf("%lld",ans);
return 0;
}
撒花!