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;
}