• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

yongchaoD

  • 博客园
  • 联系
  • 订阅
  • 管理

公告

View Post

C. Lazy Narek(二分)

题目来源:https://codeforces.com/contest/2005/problem/C
//
题意:n长的数轴,给出m个老师的位置,每一次老师可以停留到当前位置,或者左右移动一个单元。给出q个同学的位置,每个同学每次可以和老师一样是否选择移动,问每个同学,老师抓到该同学的最小次数。
//
思路:一眼二分,注意的是用upper_bound(),如果容器全是小于等于查找元素,返回a.end(),end()不指向任何元素。如果全是大于查找的元素,返回a.begin(),指向第一个元素。
//
代码:

点击查看代码
// Problem: B2. The Strict Teacher (Hard Version)
// Contest: Codeforces - Codeforces Round 972 (Div. 2)
// URL: https://codeforces.com/contest/2005/problem/B2
// Memory Limit: 256 MB
// Time Limit: 1500 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include<bits/stdc++.h>
#define int long long
using namespace std;

void solve(){
	int n,m,q;
	cin>>n>>m>>q;
	vector<int>a(m);
	for(auto &i:a){cin>>i;}
	sort(a.begin(),a.end());
	while(q--){
		int p;cin>>p;
		auto it=upper_bound(a.begin(),a.end(),p);
		if(it==a.begin()){//在最左边的位置
			cout<<*it-1<<endl;
		}
		else if(it==a.end()){//在最右边的位置
			cout<<n-(*--it)<<endl;
		}
		else{//在中间
			cout<<(*it-(*(--it)))/2<<endl;
		}
	}
}

signed main()
{
	int t=1;
	cin>>t;
	while(t--){
		solve();
	}
}

posted on 2024-09-18 17:20  yongchaoD  阅读(25)  评论(0)    收藏  举报

刷新页面返回顶部
 
博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3