题解:P15056 [UOI 2023 II Stage] Unenslaved puppy

· · 题解

题目传送门

思路

注意:压线算圈内

可以发现,如果狗到圆心的距离 \le 圆的半径,那么不管狗在哪里,都在圈内;蜜蜂同理。

那么就能很好地判断出蜜蜂能不能碰到狗了。

不过还是有一个坑:蜜蜂在圈内,但狗不在的话,蜜蜂也是碰不到狗的!

怎么算出两点之间的距离?

相信大家都上过幼儿园吧,老师~绝对没~肯定讲过欧几里德距离公式:

\sqrt{(x_2-x_1)^2+(y_2-y_1)^2}

转换成代码:

inline ll dis(ll tx,ll ty,ll fx,ll fy){
    return (tx-fx)*(tx-fx)+(ty-fy)*(ty-fy);
}

AC Code:

#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
const int N=1e3+10;
ll n,tx,ty,fx,fy,pos,r;// 记得开 long long!
inline ll dis(ll tx,ll ty,ll fx,ll fy){
    return (tx-fx)*(tx-fx)+(ty-fy)*(ty-fy);// 计算欧几里德距离
}
inline bool check(){// 判断蜜蜂能不能碰到狗
    ll btw=dis(fx,fy,pos,0ll);// 蜜蜂到圆心的距离
    ll dtw=dis(tx,ty,pos,0ll);// 狗到圆心的距离
    if(dtw<=r*r&&btw>r*r||dtw>r*r&&btw<=r*r) return 1;// 狗在圆里但蜜蜂不在 or 蜜蜂在圆里但狗不在的话蜜蜂就碰不到狗
    return 0;// 否则碰得到
}
int main(){
    ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);// 输入输出加速
    cin>>n>>tx>>ty>>fx>>fy;
    for(int i=1;i<=n;++i){
        cin>>pos>>r;
        if(check()){// 判断
            cout<<"NO\n"<<i;// 碰不到,输出 NO 和序号
            return 0;
        }
    }
    cout<<"YES";// 碰得到,输出 YES
    return 0;// 完结撒花~
}