【每日一题丨2025年5.12~5.18】排序相关题

在这里插入图片描述

个人主页:Guiat
归属专栏:每日一题

在这里插入图片描述

文章目录

  • 1. 【5.12】P1068 [NOIP 2009 普及组] 分数线划定
  • 2. 【5.13】P5143 攀爬者
  • 3. 【5.14】P12366 [蓝桥杯 2022 省 Python B] 数位排序
  • 4. 【5.15】P10901 [蓝桥杯 2024 省 C] 封闭图形个数
  • 5.【5.16】P12165 [蓝桥杯 2025 省 C/Java A] 最短距离
  • 6. 【5.17】P12186 [蓝桥杯 2025 省 Python A/研究生组] 最大数字
  • 7. 【5.18】P1012 [NOIP 1998 提高组] 拼数

正文

1. 【5.12】P1068 [NOIP 2009 普及组] 分数线划定

题目链接:https://www.luogu.com.cn/problem/P1068

【分析】
模拟排序题,复杂一点的是处理排序后的选手,按照要求输出答案。

【AC_Code】

#include <iostream>
#include <algorithm>
#include <cmath>
#define IOS ios :: sync_with_stdio(0); cin.tie(0); cout.tie(0);using namespace std;struct str { int n, s; } p[5010]; int cnt;bool cmp(str x, str y)
{if (x.s == y.s) return x.n < y.n;return x.s > y.s;
}void solve()
{int n, m; cin >> n >> m;for (int i = 0; i < n; i ++) { cin >> p[i].n >> p[i].s; }sort(p, p + n, cmp);int S = floor(m * 1.5);int score_line = p[S-1].s;  // 分数线是第S个人的分数for (int i = 0; i < n; i ++){if (p[i].s >= score_line) cnt ++; else break;}cout << score_line << ' ' << cnt << '\n';for (int i = 0; i < cnt; i ++) cout << p[i].n << ' ' << p[i].s << '\n';
}int main()
{IOS int _ = 1;   // cin >> _;while (_ --) solve();return 0;
}

2. 【5.13】P5143 攀爬者

题目链接:https://www.luogu.com.cn/problem/P5143

【分析】

简单模拟排序题,这类题往往需要写一个结构体数组和一个比较函数,剩下的只需按题意模拟即可。

【AC_Code】

#include <iostream>
#include <algorithm>
#include <cmath>
#include <iomanip>
#define IOS ios :: sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define fix(x) setprecision(x) << fixedusing namespace std;struct str{ int x, y, z; } s[50010]; double ans;bool cmp(str a, str b) { return a.z < b.z; }void solve()
{int n; cin >> n;for (int i = 0; i < n; i ++) cin >> s[i].x >> s[i].y >> s[i].z;sort(s, s + n, cmp);for (int i = 0; i < n - 1; i ++){ans += sqrt(pow(s[i].x - s[i + 1].x, 2) + pow(s[i].y - s[i + 1].y, 2) + pow(s[i].z - s[i + 1].z, 2));}cout << fix(3) << ans << '\n';
}int main()
{IOS int _ = 1;   // cin >> _;while (_ --) solve();return 0;
}

3. 【5.14】P12366 [蓝桥杯 2022 省 Python B] 数位排序

题目链接:https://www.luogu.com.cn/problem/P12366

【分析】
无需用到结构体。

【AC_Code】

#include <iostream>
#include <algorithm>
#define IOS ios :: sync_with_stdio(0); cin.tie(0); cout.tie(0);using namespace std;const int N = 1e6 + 10; int a[N], ans;int getSum(int num)
{int sum = 0;while (num) { sum += num % 10; num /= 10; }return sum;
}bool cmp(int a, int b)
{if (getSum(a) < getSum(b)) return true;if (getSum(a) == getSum(b) && a < b) return true;return false;
}void solve()
{int n, m; cin >> n >> m;for (int i = 1; i <= n; i ++) { a[i] = i; }sort(a + 1, a + 1 + n, cmp);cout << a[m] << '\n';
}int main()
{IOS int _ = 1;   // cin >> _;while (_ --) solve();return 0;
}

4. 【5.15】P10901 [蓝桥杯 2024 省 C] 封闭图形个数

题目链接:https://www.luogu.com.cn/problem/P10901

【分析】
构建一个封闭图形个数数组,发现下标刚好是要处理的单个数字,后面就很好写了。

【AC_Code】

#include <iostream>
#include <algorithm>
#define IOS ios :: sync_with_stdio(0); cin.tie(0); cout.tie(0);using namespace std;const int N = 2e5 + 10; int a[N], num[] = { 1, 0, 0, 0, 1, 0, 1, 0, 2, 1 };int cnt(int NUM)
{int CNT = 0;while (NUM) { CNT += num[NUM % 10]; NUM /= 10; }return CNT;
}bool cmp(int a, int b)
{if (cnt(a) < cnt(b)) return true;if (cnt(a) == cnt(b) && a < b) return true;return false;
}void solve()
{int n; cin >> n; for (int i = 0; i < n; i ++) cin >> a[i];sort(a, a + n, cmp);for (int i = 0; i < n; i ++) cout << a[i] << " \n"[i == n - 1];
}int main()
{IOS int _ = 1;   // cin >> _;while (_ --) solve();return 0;
}

5.【5.16】P12165 [蓝桥杯 2025 省 C/Java A] 最短距离

题目链接:https://www.luogu.com.cn/problem/P12165

【分析】
考察最朴素的贪心,可以用数学归纳法证明,比较高大上,这里不再赘述,可自行AI,最简单实用的可以通过举反例证明其合理性。

【AC_Code】

#include <iostream>
#include <algorithm>
#include <cmath>
#define IOS ios :: sync_with_stdio(0); cin.tie(0); cout.tie(0);using namespace std;
using ll = long long;const int N = 5e4 + 10; int a[N], b[N]; ll ans;void solve()
{int n; cin >> n;for (int i = 0; i < n; i ++) cin >> a[i];for (int i = 0; i < n; i ++) cin >> b[i];sort(a, a + n); sort(b, b + n);for (int i = 0; i < n; i ++) ans += abs(a[i] - b[i]);cout << ans << '\n';
}int main()
{IOS int _ = 1;   // cin >> _;while (_ --) solve();return 0;
}

6. 【5.17】P12186 [蓝桥杯 2025 省 Python A/研究生组] 最大数字

题目链接:https://www.luogu.com.cn/problem/P12186

【分析】
这题还没理解,参考题解代码。

【AC_Code】

#include <bits/stdc++.h>
using namespace std;
using ull = int;// 表示大整数的结构体,使用向量存储每一位数字
struct BigInt {vector<ull> digits;
};// 初始化大整数为 0
void initBigInt(BigInt& num) {num.digits = {0};
}// 将大整数乘以 2
void mult2(BigInt& num) {ull carry = 0;for (int i = 0; i < num.digits.size(); ++i) {ull product = num.digits[i] * 2 + carry;num.digits[i] = product % 10;carry = product / 10;}if (carry != 0) {num.digits.push_back(carry);}
}// 将大整数加 1
void add1(BigInt& num) {ull carry = 1;for (int i = 0; i < num.digits.size() && carry > 0; ++i) {ull sum = num.digits[i] + carry;num.digits[i] = sum % 10;carry = sum / 10;}if (carry > 0) {num.digits.push_back(carry);}
}// 输出大整数
void output(const BigInt& num) {int i = num.digits.size() - 1;// 跳过前导零while (i >= 0 && num.digits[i] == 0) {--i;}// 如果所有位都是零,输出 0if (i < 0) {cout << "0";return;}// 从高位到低位输出while (i >= 0) {cout << num.digits[i];--i;}
}// 将数字向量转换为二进制字符串
string toBinaryString(const vector<ull>& nums) {string bs;for (ull num : nums) {if (num == 0) {bs += "0";} else {string temp;while (num > 0) {temp = (num % 2 == 0 ? "0" : "1") + temp;num /= 2;}bs += temp;}}return bs;
}// 将二进制字符串转换为大整数
BigInt trans(const vector<ull>& nums) {string bs = toBinaryString(nums);BigInt res;initBigInt(res);for (char bit : bs) {mult2(res);if (bit == '1') {add1(res);}}return res;
}int main() {ull x;cin >> x;vector<ull> nums(x);for (int i = 0; i < x; i++) {nums[i] = i + 1;}// 自定义排序sort(nums.begin(), nums.end(), [](ull a, ull b) {ull bitA = log2(a) + 1;ull bitB = log2(b) + 1;ull tempA = (a << bitB) + b;ull tempB = (b << bitA) + a;return tempA > tempB;});BigInt bs = trans(nums);output(bs);return 0;
}

7. 【5.18】P1012 [NOIP 1998 提高组] 拼数

题目链接:https://www.luogu.com.cn/problem/P1012

【分析】
这题凭感觉写的,发现自己还不会证明QAQ。

【AC_Code】

#include <iostream>
#include <algorithm>
#define IOS ios :: sync_with_stdio(0); cin.tie(0); cout.tie(0);using namespace std;string s[25];bool cmp(string a, string b) { return a + b > b + a; }void solve()
{int n; cin >> n;for (int i = 0; i < n; i ++) cin >> s[i];sort(s, s + n, cmp);for (int i = 0; i < n; i ++) cout << s[i]; cout << '\n'; 
}int main()
{IOS int _ = 1;   // cin >> _;while (_ --) solve();return 0;
}

结语
感谢您的阅读!期待您的一键三连!欢迎指正!

在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/906602.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

522UART是什么

UART&#xff08;Universal Asynchronous Receiver/Transmitter&#xff0c;通用异步收发传输器&#xff09;是一种常见的串行通信协议&#xff0c;用于设备间的低速数据传输。它采用异步通信方式&#xff0c;无需时钟信号同步&#xff0c;仅需两根数据线&#xff08;TX发送、R…

养生攻略:五步打造健康生活

一、饮食&#xff1a;均衡膳食&#xff0c;轻养为先 遵循 “4321 饮食法则”&#xff1a;每餐 4 拳蔬菜、3 拳主食&#xff08;杂粮占 1/2&#xff09;、2 拳蛋白质、1 勺健康脂肪。早餐推荐菠菜鸡蛋卷饼配无糖豆浆&#xff0c;午餐选择糙米饭、清蒸鲈鱼、蒜蓉西兰花&#xff…

Ubuntu+Docker+内网穿透:保姆级教程实现安卓开发环境远程部署

文章目录 前言1. 虚拟化环境检查2. Android 模拟器部署3. Ubuntu安装Cpolar4. 配置公网地址5. 远程访问小结 6. 固定Cpolar公网地址7. 固定地址访问 前言 本文将详细介绍一种创新性的云开发架构&#xff1a;基于Ubuntu系统构建Android仿真容器环境&#xff0c;并集成安全隧道技…

第六届电子通讯与人工智能国际学术会议(ICECAI 2025)

在数字化浪潮中&#xff0c;电子通讯与人工智能的融合正悄然重塑世界的运行逻辑。技术基础的共生关系是这场变革的核心——电子通讯如同“信息高速公路”&#xff0c;通过5G等高速传输技术&#xff0c;将海量数据实时输送至AI系统&#xff0c;使其能够像人类神经系统般快速响应…

解决 Tailwind CSS 代码冗余问题

解决 Tailwind CSS 代码冗余问题 Tailwind CSS 确实可能导致 HTML 类名过长和冗余的问题&#xff0c;以下是几种有效的解决方案&#xff1a; 1. 使用 apply 指令提取重复样式 /* 在CSS文件中 */ .btn {apply px-4 py-2 rounded-md font-medium; }.card {apply p-6 bg-white …

pytorch-lightning1.7.0 old init 错误解决

在 /usr/local/anaconda3/envs/pcd117/lib/python3.9/site-packages/pytorch_lightning/utilities/data.py 500多行处修改下面这个方法 contextmanager def _replace_init_method(base_cls: Type, store_explicit_arg: Optional[str] None) -> Generator[None, None, No…

List介绍

什么是List 在集合框架中&#xff0c;List是一个接口&#xff0c;继承自Collection Collection也是一个接口&#xff0c;该接口中规范了后序容器中常用的一些方法 Iterable也是一个接口&#xff0c;表示实现该接口的类是可以逐个元素进行遍历的&#xff0c;具体如下&#xff1…

STM32之模数转换器(ADC)

一、模数转换的原理与应用 一般在电子线路中&#xff0c;信号分为两种&#xff1a;模拟信号 数字信号&#xff0c;大多数传感器采集的都是模拟信号&#xff0c;比如温度、湿度、压力....... &#xff0c;传感器把采集的模拟信号转为数字信号&#xff0c;再转交给计算机进行处…

luckysheet的使用——17.将表格作为pdf下载到本地

luckysheet源码里面自带有打印按钮&#xff0c;但是功能是无法使用的&#xff0c;所以我把该功能重写了一遍 1.在menuButton.js文件中找到源码打印按钮的触发事件&#xff1a; $("#luckysheet-icon-print").click(function () {}2.使用自己写的挂载方法 window.pr…

云原生+大数据

虚拟化&#xff1a; 虚拟化&#xff0c;是指通过虚拟化技术将一台计算机虚拟为多台逻辑计算机。在一台计算机上同时运行多个逻辑计算机&#xff0c;每个逻辑计算机可运行不同的操作系统&#xff0c;并且应用程序都可以在相互独立的空间内运行而互不影响&#xff0c;从而显著提…

微信小游戏流量主广告自动化浏览功能案例5

功能需求&#xff1a; 支持APP单行文本框输入1个小程序链接&#xff0c;在“文件传输助手”界面发送小程序链接并进入。 主要有“文章列表首页”和“文章内容”页面。每个页面支持点击弹窗广告、槽位广告、视频广告入口、视频广告内第三方广告。 弹窗广告、槽位广告、视频广…

线上 Linux 环境 MySQL 磁盘 IO 高负载深度排查与性能优化实战

目录 一、线上告警 二、问题诊断 1. 系统层面排查 2. 数据库层面分析 三、参数调优 1. sync_binlog 参数优化 2. innodb_flush_log_at_trx_commit 参数调整 四、其他优化建议 1. 日志文件位置调整 2. 生产环境核心参数配置模板 3. 突发 IO 高负载应急响应方案 五、…

【git进阶】git rebase(变基)

文章目录 合并分支提交信息修改合并提交记录时间问题1时间问题2时间问题3git rebase有很多用武之地,我一一道来 合并分支 当多人协作同一个分支时,在提交我们自己版本之前,我们会先用git pull获取远端最新的版本。但是 git pull = git fetch + git mergegit merge是一个非…

上位机知识篇---keil IDE操作

文章目录 前言文件操作按键新建打开保存保存所有编辑操作按键撤销恢复复制粘贴剪切全选查找书签操作按键添加书签跳转到上一个书签跳转到下一个书签清空所有书签编译操作按键编译当前文件构建目标文件重新构建调试操作按键进入调试模式复位全速运行停止运行单步调试逐行调试跳出…

代码随想录算法训练营 Day49 图论Ⅰ 深度优先与广度优先

图论 基础 图的概念 图的概念 概念清单有向图 (a)无向图 (b)有向/无向如图 a 所示每条边有指向如图 b 所示每条边没有箭头指向权值每条边的权值每条边的权值度-有几条边连到该节点 (eg V 2 V_2 V2​ 度为 3)入度/出度出度&#xff1a;从该节点出发的边个数入度&#xff1a;…

Windows在PowerShell或CMD运行 curl 命令报错 解决办法 (zx)

解决 zx 8.x 不稳定的问题 在 Windows 上使用 zx 8.x 时可能会遇到不稳定的问题&#xff0c;建议降级到 7.x 版本。可以通过以下命令进行降级&#xff1a; pnpm remove zx pnpm add zx7 -D解决 PowerShell 不识别 curl 参数的问题 在 Windows 上&#xff0c;PowerShell 默认将…

WPF···

设置启动页 默认最后一个窗口关闭,程序退出,可以设置 修改窗体的icon图标 修改项目exe图标 双击项目名会看到代码 其他 在A窗体点击按钮打开B窗体,在B窗体设置WindowStartupLocation=“CenterOwner” 在A窗体的代码设置 B.Owner = this; B.Show(); B窗体生成在A窗体中间…

【IP101】图像修复技术精解:从传统插值到深度生成修复的算法演进

图像修复技术详解 &#x1f3a8; 图像修复就像是数字世界的"修复匠"&#xff01;通过各种"修复技术"&#xff0c;我们可以让受损的图像重获新生&#xff0c;就像修复匠修复破损的艺术品一样。让我们一起来探索这个神奇的图像"修复工作室"吧&…

Qt enabled + geometry 属性(2)

文章目录 enabled属性可用与禁用的概念API接口代码演示 阐述说明1. 先简单描述下要如何演示出上面两个接口的效果&#xff08;思路&#xff09;2. 事先规范按钮对象的命名3. 定义两个按钮对象的槽函数 动图演示效果4. widget.cpp geometry属性预备知识API接口上下左右移动 ta…

Linux下 使用 SSH 完成 Git 绑定 GitHub

文章目录 1、检查 SSH2、生成 SSH key3、添加 SSH key4、验证绑定是否成功 1、检查 SSH Git Bash 中输入ssh命令&#xff0c;查看本机是否安装 SSH&#xff1a; 2、生成 SSH key &#xff08;1&#xff09;输入 ssh-keygen -t rsa 命令&#xff0c;表示我们指定 RSA 算法生…