11853 - Paintball (UVA)

news/2024/7/15 19:07:18 标签: 图论, 算法

题目链接如下:

Online Judge

这道题挺可惜,我思路其实就差了一点点没想出来,还是看了uva 11853 paintball(好题)——yhx_yhx. live-CSDN博客 这里的文字部分才最终写出来。

dfs版本代码如下:

#include <cstdio>
#include <vector>
#include <set>
#include <cmath>
#include <algorithm>
const int maxx = 1001;
const double eps = 1e-7;
// #define debug

struct oppo{
    double x, y, radius;
};
int n;
bool vis[maxx];
double x, y, radius, yLeft, yRight;
std::vector<oppo> vec;
std::set<int> up;

bool intersect(int a, int b){
    double dis1 = (vec[a].radius + vec[b].radius) * (vec[a].radius + vec[b].radius);
    double dis2 = (vec[a].x - vec[b].x) * (vec[a].x - vec[b].x) + (vec[a].y - vec[b].y) * (vec[a].y - vec[b].y);
    if (dis1 > dis2 - eps){
        return true;
    }
    return false;
}

bool dfs(int k){
    vis[k] = true;
    if (vec[k].y < vec[k].radius + eps){
        return false;
    }
    if (vec[k].x < vec[k].radius + eps){
        yLeft = std::min(yLeft, vec[k].y - sqrt(vec[k].radius * vec[k].radius - vec[k].x * vec[k].x));
    }
    if (vec[k].x > 1000 - vec[k].radius - eps){
        yRight = std::min(yRight, vec[k].y - sqrt(vec[k].radius * vec[k].radius - (1000 - vec[k].x) * (1000 - vec[k].x)));
    }
    for (int i = 0; i < n; ++i){
        if (i != k && !vis[i] && intersect(i, k)){
            if (!dfs(i)){
                return false;
            }
        }
    }
    return true;
}

void judge(){
    yLeft = yRight = 1000.0;
    for (auto it = up.begin(); it != up.end(); ++it){
        std::fill(vis, vis + n, false);
        if (!dfs(*it)){
            printf("IMPOSSIBLE\n");
            return;
        }
    }
    printf("0.00 %.2f 1000.00 %.2f\n", yLeft, yRight);
}

int main(){
    #ifdef debug
    freopen("0.txt", "r", stdin);
    freopen("1.txt", "w", stdout);
    #endif
    while (scanf("%d", &n) == 1){
        vec.resize(n);
        up.clear();
        for (int i = 0; i < n; ++i){
            scanf("%lf %lf %lf", &x, &y, &radius);
            vec[i].x = x;
            vec[i].y = y;
            vec[i].radius = radius;
            if (y > 1000 - radius - eps){
                up.insert(i);
            }
        }
        judge();
    }
    #ifdef debug
    fclose(stdin);
    fclose(stdout);
    #endif
    return 0;
}

我自己最开始的想法是用连通集来做,也能AC:

#include <cstdio>
#include <vector>
#include <set>
#include <cmath>
const int maxx = 1001;
const double eps = 1e-7;
// #define debug

struct oppo{
    double x, y, radius;
};
int n, temp;
int fa[maxx];
double x, y, radius, yLeft, yRight;
std::vector<oppo> vec;
std::set<int> up, down, left, right;

bool intersect(int a, int b){
    double dis1 = (vec[a].radius + vec[b].radius) * (vec[a].radius + vec[b].radius);
    double dis2 = (vec[a].x - vec[b].x) * (vec[a].x - vec[b].x) + (vec[a].y - vec[b].y) * (vec[a].y - vec[b].y);
    if (dis1 > dis2 - eps){
        return true;
    }
    return false;
}

int findFather(int a){
    while (fa[a] != a){
        a = fa[a];
    }
    return a;
}

void Union(int a, int b){
    int faA = findFather(a);
    int faB = findFather(b);
    fa[faA] = faB;
}

bool divide(){
    std::set<int> top;
    for (auto it = up.begin(); it != up.end(); ++it){
        top.insert(fa[*it]);
    }
    for (auto it = down.begin(); it != down.end(); ++it){
        if (top.find(fa[*it]) != top.end()){
            return true;
        }
    }
    return false;
}

void findPath(){
    yLeft = yRight = 1000.0;
    for (auto it = up.begin(); it != up.end(); ++it){
        temp = fa[*it];
        for (auto it1 = left.begin(); it1 != left.end(); ++it1){
            if (fa[*it1] == temp){
                yLeft = std::min(yLeft, vec[*it1].y - sqrt(vec[*it1].radius * vec[*it1].radius - vec[*it1].x * vec[*it1].x));
            }
        }
        for (auto it2 = right.begin(); it2 != right.end(); ++it2){
            if (fa[*it2] == temp){
                yRight = std::min(yRight, vec[*it2].y - sqrt(vec[*it2].radius * vec[*it2].radius - (1000 - vec[*it2].x) * (1000 - vec[*it2].x)));
            }
        }
    }
    printf("0.00 %.2f 1000.00 %.2f\n", yLeft, yRight);
}

int main(){
    #ifdef debug
    freopen("0.txt", "r", stdin);
    freopen("1.txt", "w", stdout);
    #endif
    while (scanf("%d", &n) == 1){
        vec.resize(n);
        up.clear();
        down.clear();
        left.clear();
        right.clear();
        for (int i = 0; i < n; ++i){
            fa[i] = i;
        }
        for (int i = 0; i < n; ++i){
            scanf("%lf %lf %lf", &x, &y, &radius);
            vec[i].x = x;
            vec[i].y = y;
            vec[i].radius = radius;
            if (x < radius + eps){
                left.insert(i);
            }
            if (x > 1000 - radius - eps){
                right.insert(i);
            }
            if (y > 1000 - radius - eps){
                up.insert(i);
            }
            if (y < radius + eps){
                down.insert(i);
            }
            for (int j = 0; j < i; ++j){
                if (intersect(i, j)){
                    Union(i, j);
                }
            }
        }
        for (int i = 0; i < n; ++i){
            fa[i] = findFather(i);
        }
        if (divide()){
            printf("IMPOSSIBLE\n");
        } else {
            findPath();
        }
    }
    #ifdef debug
    fclose(stdin);
    fclose(stdout);
    #endif
    return 0;
}


http://www.niftyadmin.cn/n/5316088.html

相关文章

hive sql 和 spark sql的区别

Hive SQL 和 Spark SQL 都是用于在大数据环境中处理结构化数据的工具&#xff0c;但它们有一些关键的区别&#xff1a; 底层计算引擎&#xff1a; Hive SQL&#xff1a;Hive 是建立在 Hadoop 生态系统之上的&#xff0c;使用 MapReduce 作为底层计算引擎。因此&#xff0c;它的…

数据结构之单调栈、单调队列

今天学习了单调栈还有单调队列的概念和使用&#xff0c;接下来我将对其定义并配合几道习题进行讲解&#xff1a; 首先先来复习一下栈与队列&#xff1a; 然后我们来看一下单调栈的定义&#xff1a; 单调栈中的元素从栈底到栈顶的元素的大小是按照单调递增或者单调递减的关系进…

Go语言开发IDE介绍

Go语言开发的集成开发环境&#xff08;IDE&#xff09;主要包括以下几种&#xff1a; Goland - 由 JetBrains 公司专门为 Go 语言开发而设计的专业 IDE&#xff0c;提供智能代码补全、深入代码分析、高级调试工具、强大的导航与搜索功能以及与版本控制系统&#xff08;VCS&…

【Kafka-3.x-教程】-【五】Kafka-监控-Eagle

【Kafka-3.x-教程】专栏&#xff1a; 【Kafka-3.x-教程】-【一】Kafka 概述、Kafka 快速入门 【Kafka-3.x-教程】-【二】Kafka-生产者-Producer 【Kafka-3.x-教程】-【三】Kafka-Broker、Kafka-Kraft 【Kafka-3.x-教程】-【四】Kafka-消费者-Consumer 【Kafka-3.x-教程】-【五…

Map与JSONObject区别

相同点&#xff1a; 都可以存key-value&#xff1b;key是唯一的,如果key重复了会覆盖前面的 不同点&#xff1a; &#xff08;1&#xff09;JSONObject 不可以存空&#xff0c;Map可以存空。 &#xff08;2&#xff09;Map由jdk提供&#xff0c;JsonObject需要第三方jar包提供。…

springboot医院信管系统源码和论文

随着信息技术和网络技术的飞速发展&#xff0c;人类已进入全新信息化时代&#xff0c;传统管理技术已无法高效&#xff0c;便捷地管理信息。为了迎合时代需求&#xff0c;优化管理效率&#xff0c;各种各样的管理系统应运而生&#xff0c;各行各业相继进入信息管理时代&#xf…

【论文阅读】Deep Graph Infomax

目录 0、基本信息1、研究动机2、创新点2.1、核心思想&#xff1a;2.2、思想推导&#xff1a; 3、准备3.1、符号3.2、互信息3.3、JS散度3.4、Deep InfoMax方法3.5、判别器&#xff1a;f-GAN估计散度 4、具体实现4.1、局部-全局互信息最大化4.2、理论动机 5、实验设置5.1、直推式…

一文详解向量数据库Milvus Cloud动态 Schema

在数据库中&#xff0c;Schema 常有&#xff0c;而动态 Schema 不常有。 例如&#xff0c;SQL 数据库有预定义的 Schema&#xff0c;但这些 Schema 通常都不能修改&#xff0c;用户只有在创建时才能定义 Schema。Schema 的作用是告诉数据库使用者所希望的表结构&#xff0c;确…