思维+贪心,CF 1210B - Marcin and Training Camp

news/2024/9/28 6:28:52 标签: 算法, 动态规划, 数学建模

目录

一、题目

1、题目描述

2、输入输出

2.1输入

2.2输出

3、原题链接

二、解题报告

1、思路分析

2、复杂度

3、代码详解


一、题目

1、题目描述

2、输入输出

2.1输入

2.2输出

3、原题链接

1210B - Marcin and Training Camp


二、解题报告

1、思路分析

考虑一个数 x 不会比集合内所有数都强,可以注意到什么?

一定有一个数 y 满足 x & y = x

否则,x 和 每个数相比总有对方没有的,那么就没办法了

所以,我们先将所有 出现次数大于1 的数拿进集合(升序排序,去重即可)

然后遍历剩下的数字 x,如果集合中存在某个数字y 满足 x & y = x,我们就把x 拿进集合

2、复杂度

时间复杂度: O(nlogn)空间复杂度:O(n)

3、代码详解

 ​
#include <bits/stdc++.h>

// #define DEBUG

using u32 = unsigned;
using i64 = long long;
using u64 = unsigned long long;

constexpr int P = 1E9 + 7;
constexpr int inf32 = 1E9 + 7;
constexpr i64 inf64 = 1E18 + 7;

void solve() {
    int n;
    std::cin >> n;
    
    std::vector<i64> a(n);
    std::vector<int> b(n);
    for (int i = 0; i < n; ++ i) 
        std::cin >> a[i];    
    for (int i = 0; i < n; ++ i)
        std::cin >> b[i];

    i64 sum = 0;
    std::vector<int> id(n);
    std::iota(id.begin(), id.end(), 0);

    std::ranges::sort(id, [&a](int i, int j) -> bool {
        return a[i] < a[j];
    });

    std::vector<int> st, buf;

    for (int i = 0, j = 0; i < n; ) {
        i64 tot = 0;
        while (j < n && a[id[i]] == a[id[j]])
            tot += b[id[j ++]];
        if (j - i > 1) {
            sum += tot;
            st.insert(st.end(), id.begin() + i, id.begin() + j);
        }
        else {
            buf.insert(buf.end(), id.begin() + i, id.begin() + j);
        }
        i = j;
    }

    for (int i : buf) {
        bool ok = false;
        for (int j : st) {
            if ((a[i] & a[j]) == a[i]) {
                ok = true;
                break;
            }
        }
        if (ok) {
            st.push_back(i);
            sum += b[i];
        }
    }

    std::cout << sum;
}

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);

#ifdef DEBUG
    int cur = clock();
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#endif

    int t = 1;
    // std::cin >> t;

    while (t--) {
        solve();
    }
#ifdef DEBUG
    std::cerr << "run-time: " << clock() - cur << '\n';
#endif
    return 0;
}


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

相关文章

为什么不用tensorflow而用opencv

使用 OpenCV 而不是 TensorFlow 进行图像处理和计算机视觉任务的原因取决于特定需求和场景。以下是一些常见的考虑因素&#xff0c;帮助您理解选择 OpenCV 的情况&#xff1a; 1. 图像处理 vs. 深度学习 OpenCV&#xff1a;主要用于传统的图像处理和计算机视觉任务&#xff0…

sysbench 命令:跨平台的基准测试工具

一、命令简介 sysbench 是一个跨平台的基准测试工具&#xff0c;用于评估系统性能&#xff0c;包括 CPU、内存、文件 I/O、数据库等性能。 ‍ 比较同类测试工具 bench.sh 在上文 bench.sh&#xff1a;Linux 服务器基准测试中介绍了 bench.sh 一键测试脚本&#xff0c;它对…

若依开源系统多数据源整合clickhouse数据库详细步骤

1.添加依赖【pom.xml文件】 <!-- clickhouse数据源依赖--><dependency><groupId>ru.yandex.clickhouse</groupId>

试用Debian12.7和Ubuntu24.4小札

Debian GNU/Linux 12 (bookworm)和Ubuntu 24.04.1 LTS是现阶段&#xff08;2024年9月26日&#xff09;两个发行版的最新版本。Ubuntu Server版本默认就不带桌面&#xff08;ubuntu-24.04-live-server-amd64.iso&#xff09;&#xff0c;这个默认就是最小化安装&#xff08;安装…

Android系统:系统架构

文章目录 分层设计分块设计总结 分层设计 自上而下分为&#xff1a;应用层、应用架构层、系统运行层、硬件抽象层、Linux内核层 应用层 封装一系列系统App 应用架构层(Framework) 封装一系列运行App需要的Java框架 提供App开发需要的API 系统运行层(Libraries) 封装一系…

亲身体验Llama 3.1:开源模型的部署与应用之旅

文章目录 1 Llama 3.1系列的诞生2 大型模型的未来发展3 使用教程4 Llama 3.1在客户服务中的运用 1 Llama 3.1系列的诞生 在人工智能的浪潮中&#xff0c;大型语言模型&#xff08;LLM&#xff09;正以其独特的魅力和潜力&#xff0c;成为深度学习领域的一颗耀眼明星。 这些模…

ide使用技巧与插件推荐

在使用IntelliJ IDEA&#xff08;简称IDEA&#xff09;这类集成开发环境&#xff08;IDE&#xff09;时&#xff0c;掌握一些高效的使用技巧和安装合适的插件可以显著提升开发效率。以下将从IDEA的使用技巧和插件推荐两个方面进行详细阐述&#xff0c;内容不少于2000字。 一、…

Python:lambda 函数详解 以及使用

一、lambda 语法 lambda 函数的语法只包含一个语句&#xff0c;表现形式如下&#xff1a; lambda [arg1 [,arg2,.....argn]]:expression 其中&#xff0c;lambda 是 Python 预留的关键字&#xff0c;[arg…] 和 expression 由用户自定义。 具体如下: [arg…] 是参数列表&#…