Educational Codeforces Round 134 (Rated for Div. 2)B-C

B:

题目:传送门

思路:显然,若不存在遮挡的话,从左上角到右下角,最短路径是一定的,由于题目产生的遮挡是一块近似菱形的区域,不会产生七拐八拐的回路,所以只要能联通,都一定可以以理论最短的路径到达。所以只需要判断不连通的情况。上文已经说了遮挡区域是一块菱形,所以实际上只需要判断这块菱形区域截断了矩形的那几条边界,显然一共16中情况,自己手模一下就知道是哪些,具体见代码

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
const int inf=1e9+9;
const ll INF=1e18l;
/*
priority_queue<int> big_heep;                 
priority_queue<int,vector<int>,greater<int> > small_heep;       
struct cmp{//if return true,it means that the number in the left was smaller than the right one
	    bool  operator ()  (int  a,int  b){
	   } 
};  
 */

void work(){
	int n,m,sx,sy,d;
	cin>>n>>m>>sx>>sy>>d;
	if (sx-d<=1 and sx+d>=n){
		cout<<-1<<"\n";
		return;
	}
	if (sy-d<=1 and sy+d>=m){
		cout<<-1<<"\n";
		return;
	}
	if (sx-d<=1 and sy-d<=1){
		cout<<-1<<"\n";
		return;
	}
	if (sx+d>=n and sy+d>=m){
		cout<<-1<<"\n";
		return;
	}
	cout<<n+m-2<<"\n";
}
int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	ll t;
	cin>>t;
	while (t--){
		
		work();
	}
	return 0;
}

C:

题目:传送门

思路:对于最小值,因为d必须是非负数,故第一个大于等于a[i]的b[i]与a[i]的差即为最小值(考虑大于小于都不更优),最大值反过来即可。

代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
const int inf=1e9+9;
const ll INF=1e18l;
/*
priority_queue<int> big_heep;                 
priority_queue<int,vector<int>,greater<int> > small_heep;       
struct cmp{//if return true,it means that the number in the left was smaller than the right one
	    bool  operator ()  (int  a,int  b){
	   } 
};  
 */
ll a[300005];
ll b[300005];

void work(){
	int n;
	cin>>n;
	for (int i=1;i<=n;i++) cin>>a[i];
	for (int i=1;i<=n;i++) cin>>b[i];
	for (int i=1;i<=n;i++){
		ll x=lower_bound(b+1,b+n+1,a[i])-b;
		cout<<b[x]-a[i]<<" ";
	}
	cout<<"\n";
	for (int i=1;i<=n;i++){
		int l,r;
		l=1;
		r=n;
		while(l<r){
			int mid=(l+r)/2;
			if (a[mid]>b[i]) r=mid;
			else l=mid;
			if (r-l==1){
				if (a[r]<=b[i]) l=r;
				else r=l;
			}
		}
		cout<<b[l]-a[i]<<" ";
	}
	cout<<"\n";
	
}
int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	ll t;
	cin>>t;
	while (t--){
		
		work();
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值