P8720 [蓝桥杯 2020 省 B2] 平面切分--set、pair

news/2025/3/3 19:36:30/

P8720 [蓝桥杯 2020 省 B2] 平面切分--set、pair

      • 题目
  • 分析
    • 一、pair
    • 1.1pair与vector的区别
    • 1.2 两者使用场景
    • 两者组合使用
  • 二、set
    • 2.1核心特点
    • 2.2set的基本操作
    • 2.3 set vs unordered_set
    • 示例:统计唯一单词数
      • 代码

题目

在这里插入图片描述

分析

大佬写的很明白,看这儿

我讲讲其中用到的知识点吧!

一、pair

pair是 C++ 中的一个模板类,用于存储两个值(可以是不同类型)。

1、基本用法

#include <iostream>
#include <utility> // pair 的头文件
using namespace std;int main() {pair<int, string> student = {18, "小明"}; // 存储年龄和姓名cout << "年龄: " << student.first << endl;   // 输出 18cout << "姓名: " << student.second << endl;  // 输出 小明return 0;
}

2、核心特性:
pair 中的两个值可以是不同类型(如 int 和 string)。
通过 .first 和 .second 访问两个值。

3、pair 的常见用途

1)存储关联数据
例如,存储一个点的坐标 (x, y)

pair<double, double> point = {3.14, 2.71};

2)函数返回两个值

pair<bool, int> checkValue(int x) {if (x > 0) return {true, x};else return {false, 0};
}

1.1pair与vector的区别

在这里插入图片描述

1.2 两者使用场景

对pair:

  1. 坐标 (x, y)。
  2. 一个学生的年龄和姓名 (int, string)

对vector:

需要存储一组同类型数据,例如:
学生成绩列表 [90, 85, 95]。
一组字符串 [“apple”, “banana”, “cherry”]。

两者组合使用

vector<pair<double, double>> points;
points.push_back({1.0, 2.0}); // 添加点 (1.0, 2.0)
points.push_back({3.0, 4.0}); // 添加点 (3.0, 4.0)// 遍历所有点
for (auto p : points) {cout << "x: " << p.first << ", y: " << p.second << endl;
}

二、set

set是 C++ 中的一个重要容器,用于存储一组唯一且有序的元素。set 是 C++ 中的一个重要容器,用于存储一组唯一且有序的元素。它的核心特性是自动去重自动排序,非常适合处理需要唯一性和顺序性的数据。

2.1核心特点

在这里插入图片描述

2.2set的基本操作

(1) 创建 set

#include <set>
using namespace std;set<int> s1;                // 默认升序排列的整数集合
set<string> s2;             // 字符串集合
set<pair<int, int>> s3;     // 存储 pair 的集合

(2) 插入元素

s1.insert(3);       // 插入元素 3
s1.insert(1);       // 插入元素 1
s1.insert(2);       // 插入元素 2
s1.insert(3);       // 重复插入 3,会被自动忽略
// 此时 s1 = {1, 2, 3}

(3) 遍历 set

//若set<pair<int,int>> s1;
//for(auto &i:s1) cout<<num;
for (auto num : s1) {cout << num << " ";   // 输出 1 2 3
}

(4) 查找元素

auto it = s1.find(2);
if (it != s1.end()) {cout << "元素 2 存在!" << endl;
}

(5) 删除元素

s1.erase(2);        // 删除元素 2
s1.erase(s1.begin()); // 删除第一个元素(即 1)

2.3 set vs unordered_set

在这里插入图片描述

示例:统计唯一单词数

#include <iostream>
#include <set>
using namespace std;int main() {set<string> uniqueWords;string word;while (cin >> word) {  // 输入单词,按 Ctrl+Z (Windows) 或 Ctrl+D (Mac) 结束uniqueWords.insert(word);}cout << "唯一单词数: " << uniqueWords.size() << endl;return 0;
}
//输入:apple banana apple cherry banana
//输出:唯一单词数: 3

代码

#include <iostream>
#include <vector>
#include <set>
#include <string>
#include <algorithm>
#include <math.h>
#include <queue>
#include <climits>  // 包含INT_MAX常量
#include <cctype>
using namespace std;
int n;
typedef pair<long double, long double> DL;
set<DL> f;
DL a[1010];int main() {cin >> n;for (int i = 0; i < n; i++) {int k, b;cin >> k >> b;f.insert({k, b});}int cnt = 0;for (auto &i : f) {a[cnt++] = i;}int ans = 1;for (int i = 0; i < cnt; i++) {set<DL> d;for (int j = 0; j < i; j++) {long double k1 = a[i].first;long double b1 = a[i].second;long double k2 = a[j].first;long double b2 = a[j].second;if (k1 == k2)continue;//注意,交点的x,y值是浮点型,别定义成int了!!!long double x = (b2 - b1) / (k1 - k2);long double y = k1 * x + b1;d.insert({x, y});}ans += d.size() + 1;}cout << ans << endl;return 0;
}

http://www.ppmy.cn/news/1576358.html

相关文章

C#贪心算法

贪心算法&#xff1a;生活与代码中的 “最优选择大师” 在生活里&#xff0c;我们常常面临各种选择&#xff0c;都希望能做出最有利的决策。比如在超市大促销时&#xff0c;面对琳琅满目的商品&#xff0c;你总想用有限的预算买到价值最高的东西。贪心算法&#xff0c;就像是一…

大白话css第四章学习的高阶实践与前沿探索

大白话css第四章学习的高阶实践与前沿探索 1. 深入响应式设计 解释&#xff1a;响应式设计就是让网页在各种设备上&#xff0c;像手机、平板、电脑等&#xff0c;都能好看又好用。之前可能简单用用媒体查询&#xff0c;现在要更深入&#xff0c;考虑各种不同屏幕尺寸和设备特…

Ubuntu 22.04 安装Nvidia驱动加速deepseek

一键安装22.04 nvidia 驱动 nvidia 官网下载驱动我的环境是NVIDIA RTX A5000nvidia 文档参考没有安装驱动之前确认自己的型号 lspci | grep -i vga &#xff08;如数字2231&#xff09; 参考docker 支持nvidia &#xff0c;注释了需要的取消注释即可 42行-92行一定要重启服务器…

蓝桥杯2024年真题java B组 【H.拼十字】

蓝桥杯2024年真题java B组 【H.拼十字】 原题链接&#xff1a;拼十字 思路&#xff1a; 使用树状数组或线段树解决。 先将输入的信息存入到一个n行3列的数组中&#xff0c;将信息排序&#xff0c;按照长度小到大&#xff0c;长相同时&#xff0c;宽度小到大 排序。 建立三个…

安全见闻5,6

人工智能篇 人工智能目前处于高数发展阶段,所涉及的安全问题也很多 ai所收集的数据有泄露的风险(数据安全) ai进行工作的时候可能因为收集的恶意信息而产生错误(对抗攻击) ai模型被逆向窃取的风险,涉及到知识产权被侵犯的问题 ai被用作与恶意网络攻击的风险 同时要搞好ai…

命令行方式安装KFS同步KES到KADB

部署背景及环境 使用命令行方式同步KES的数据至KADB 操作系统 [mppadminmdw ~]$ uname -a Linux mdw 4.19.90-24.4.v2101.ky10.aarch64 #1 SMP Mon May 24 14:45:37 CST 2021 aarch64 aarch64 aarch64 GNU/Linux KFS版本 KingbaseFlySync-V002R002C004PS002-replicator.t…

线上服务器的文件下载到本地Windows电脑

将线上服务器的文件下载到本地Windows电脑&#xff0c;可以根据具体情况选择以下方法&#xff1a; 方法一&#xff1a;使用远程桌面连接&#xff08;推荐&#xff09; 开启远程桌面功能 确保服务器已启用远程桌面&#xff08;RDP&#xff09;服务&#xff0c;默认端口为3389。检…

【前端知识】Vue2.x与3.x之间的区别以及升级过程需要关注的地方

文章目录 Vue 2.x 与 Vue 3.x**Vue 2.x 与 Vue 3.x 的区别详细说明****1. 核心特性与性能****2. API 变化****3. 新增特性****4. 工具链与生态系统** **从 Vue 2 升级到 Vue 3 的注意事项****1. 检查依赖库兼容性****2. 修改代码以适配 Vue 3 的 API****3. 处理废弃功能****4. …