专栏文章
题解:P11916 [PA 2025] 学区房 / Szkoła
P11916题解参与者 2已保存评论 1
文章操作
快速查看文章及其快照的属性,并进行相关操作。
- 当前评论
- 1 条
- 当前快照
- 1 份
- 快照标识符
- @mipssth4
- 此快照首次捕获于
- 2025/12/03 17:22 3 个月前
- 此快照最后确认于
- 2025/12/03 17:22 3 个月前
思路
我们先和并区间(就是将 和 合并成 ),然后就看每一个区间的两端(对于所有区间 我们看 和 的 ),最后记录答案。
注意:要注意建筑编号为 , 可能小于1, 可能大于 。
代码
CPP#include<bits/stdc++.h>
using namespace std;
long long n,m,t,ans=1e18,ans2,top;
pair<long long,long long>a[1001];
int main(){
scanf("%lld%lld%lld",&n,&m,&t);
for(int i=1;i<=m;i++){
scanf("%lld%lld",&a[i].first,&a[i].second);
}
sort(a+1,a+m+1);
top=0;
for(int i=1;i<=m;i++){
if(i==1||a[i].first!=a[top].second+1){
a[++top]=a[i];
}
else{
a[top].second=a[i].second;
}
}
for(int i=1;i<=top;i++){
long long x=a[i].first,y=a[i].second;
x--,y++;
if(x>=1&&abs(x-t)<ans){
ans=abs(x-t),ans2=x;
}
if(y<=n&&abs(y-t)<ans){
ans=abs(y-t),ans2=y;
}
if(x>=1&&abs(x-t)==ans){
ans2=min(ans2,x);
}
if(y<=n&&abs(y-t)==ans){
ans2=min(ans2,y);
}
}
printf("%lld",ans2);
}
相关推荐
评论
共 1 条评论,欢迎与作者交流。
正在加载评论...