bzoj3007 拯救小云公主(二分答案+并查集)

本文介绍了一种结合二分查找与并查集的数据结构算法实现,通过判断在特定半径条件下,是否能从起点(1,1)到达终点(n,m),避免经过一系列圆形障碍物。使用并查集来维护连通状态,并采用二分法优化搜索效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

二分答案以后就是不能经过若干圆,能否从(1,1)走到(n,m)
只要(1,m),(n,1)不连通即可。
并查集维护一波即可。
复杂度O(n2logw)O(n2logw)

#include <bits/stdc++.h>
using namespace std;
#define inf 0x3f3f3f3f
#define ll long long
#define N 3010
#define eps 1e-3
inline char gc(){
    static char buf[1<<16],*S,*T;
    if(T==S){T=(S=buf)+fread(buf,1,1<<16,stdin);if(T==S) return EOF;}
    return *S++;
}
inline int read(){
    int x=0,f=1;char ch=gc();
    while(ch<'0'||ch>'9'){if(ch=='-') f=-1;ch=gc();}
    while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=gc();
    return x*f;
}
int n,a,b,fa[N];
struct Point{
    int x,y;
}c[N];
inline int find(int x){return x==fa[x]?x:fa[x]=find(fa[x]);}
inline void merge(int x,int y){
    int xx=find(x),yy=find(y);
    if(xx!=yy) fa[xx]=yy;
}
inline double sqr(double x){return x*x;}
inline double dis(int i,int j){
    return sqr(c[i].x-c[j].x)+sqr(c[i].y-c[j].y);
}
inline bool jud(double r){
    for(int i=0;i<=n+1;++i) fa[i]=i; 
    for(int i=1;i<=n;++i){
        if(c[i].x-r<=1||c[i].y+r>=b) merge(0,i);
        if(c[i].x+r>=a||c[i].y-r<=1) merge(i,n+1);
        for(int j=1;j<i;++j)
            if(dis(i,j)<=sqr(2*r)) merge(i,j);
    }return find(0)!=find(n+1);
}
int main(){
//  freopen("a.in","r",stdin);
    n=read();a=read();b=read();
    for(int i=1;i<=n;++i) c[i].x=read(),c[i].y=read();
    double l=0,r=min(a,b);
    while(r-l>eps){
        double mid=(r+l)/2;
        if(jud(mid)) l=mid;
        else r=mid;
    }printf("%.2lf\n",l);
    return 0;
}
要在uniapp生成二维码保存到本地,可以按照以下步骤进行操作: 1. 首先,使用weapp.qrcode生成二维码。这个库可以用于生成二维码的数据。 2. 然后,使用uni.canvasToTempFilePath方法将生成二维码转换为临时图片文件。 3. 使用canvas绘制二维码。 4. 接下来,使用uni.canvasToTempFilePath方法再次生成图片文件。 5. 最后,使用uni.saveImageToPhotosAlbum方法将图片保存到本地相册。 具体代码示例如下: ```javascript // 1. 使用weapp.qrcode生成二维码 const qrcode = require('weapp.qrcode'); const qrData = 'your qrcode data'; const qrcodeImg = qrcode.createQrCodeImg(qrData); // 2. 使用uni.canvasToTempFilePath生成临时图片文件 uni.canvasToTempFilePath({ canvasId: 'canvas', // canvas的id success: function (res) { const tempFilePath = res.tempFilePath; // 3. 使用canvas绘制二维码 const ctx = uni.createCanvasContext('canvas'); ctx.drawImage(qrcodeImg, 0, 0, 200, 200); // 绘制二维码图片 ctx.draw(false, function () { // 4. 使用uni.canvasToTempFilePath生成图片文件 uni.canvasToTempFilePath({ canvasId: 'canvas', success: function (res2) { const qrCodeFilePath = res2.tempFilePath; // 5. 使用uni.saveImageToPhotosAlbum保存到本地 uni.saveImageToPhotosAlbum({ filePath: qrCodeFilePath, success: function () { console.log('保存成功'); }, fail: function (err) { console.log('保存失败', err); } }); }, fail: function (err2) { console.log('生成二维码图片文件失败', err2); } }); }); }, fail: function (err3) { console.log('生成临时图片文件失败', err3); } }); ``` 通过以上步骤,你就可以在uniapp生成二维码保存到本地相册了。<span class="em">1</span><span class="em">2</span><span class="em">3</span><span class="em">4</span>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值