位运算,双指针,二分,排序算法

ops/2025/2/22 18:16:36/

文章目录

  • 位运算
    • 二进制中1的个数
    • 题解
    • 代码
    • 我们需要0
    • 题解
    • 代码
  • 排序
    • 模版排序1
    • 题解
    • 代码
    • 模版排序2
    • 题解
    • 代码
    • 模版排序3
    • 题解
    • 代码
  • 双指针
    • 最长连续不重复子序列
    • 题解
    • 代码
  • 二分
    • 查找
    • 题解
    • 代码

位运算

在这里插入图片描述
在这里插入图片描述

1. bitset< 16 >将十进制数转为16位的二进制数

int x = 25;
cout << bitset<16>(x) << endl;

二进制中1的个数

在这里插入图片描述

题解

1. 就是每个数与上1判断低位是否是1,是1就加,否则不加,判断完后,这个数右移1位,再判断,直到这个数变为0为止

代码

// 3  011 & 001  1 count++
//        >> 001 & 001 1 count++
#include<iostream>
#include<vector>
using namespace std;int main()
{int n;cin >> n;vector<int> v(n);for (int i = 0; i < n; i++)cin >> v[i];for (int i = 0; i < n; i++){int count = 0;int val = v[i];while (val){if (val & 1)count++;val /= 2;}v[i] = count;}for (int i = 0; i < n; i++)cout << v[i] << " ";return 0;
}

我们需要0

在这里插入图片描述

题解

1. 利用了x ^ x = 0和0 ^ x = x这两个性质

代码

// 1 ^ 2  ^ 3 ^ x ^ x ^ x  
// 1 ^ 2 ^ 3 ^ x = 0
// 1 ^ 2 ^ 3 ^ x ^ x == 0 ^ x
// 1 ^ 2 ^ 3 == x#include<iostream>
#include<vector>
using namespace std;int main()
{int t, n;cin >> t;int k = 0;int x = 0;while (t--){cin >> n;vector<int> v(n+1);int sum = 0;for (int i = 1; i <= n; i++)cin >> v[i];for (int i = 1; i <= n; i++)sum ^= v[i];cout << sum << '\n';}return 0;
}

排序

在这里插入图片描述

1. 使用unique之前要确保数组是有序的,有序的才能确保所有元素都是唯一的
2.unique会把重复的元素移动到数组的末尾,最后返回第一个重复元素的迭代器

模版排序1

在这里插入图片描述

题解

1. 先排序,然后用unique进行返回第一个重复元素的迭代器,最后用erase删除这些重复元素

代码

#include<iostream>
#include<vector>
#include<algorithm>using namespace std;int main()
{int n;cin >> n;vector<int> v(n);for(int i = 0;i < n;i++)cin >> v[i];sort(v.begin(),v.end());v.erase(unique(v.begin(),v.end()),v.end());for(auto x : v)cout << x << " ";return 0;
}

模版排序2

在这里插入图片描述

题解

1. 自己写一个排序的逻辑,自定义类型的排序
2.升序的,逆置完之后就可以变为降序的了

代码

#include<iostream>
#include<vector>
#include<algorithm>using namespace std;const int N = 2e5 + 10;
struct Book
{int a;int b;int c;bool operator<(const Book& v) const{if (a == v.a && b == v.b) return c < v.c;if (a == v.a) return b < v.b;return a < v.a;}}u[N];int main()
{int n = 0;cin >> n;for (int i = 0; i < n; i++)cin >> u[i].a >> u[i].b >> u[i].c;sort(u, u + n);reverse(u, u + n);for (int i = 0; i < n; i++)cout << u[i].a << " " << u[i].b << " " << u[i].c << '\n';return 0;
}

模版排序3

在这里插入图片描述

题解

1. 这题用了桶排序的思想
2.记录出现数字的次数,然后用次数控制循环,输出[ ]里的数就是存进去的数,可以按顺序输出了

代码

// 桶排序#include<iostream>
#include<vector>
#include<algorithm>using namespace std;const int N = 2e5 + 10;
int v[N];int main()
{int n = 0;cin >> n;for(int i = 1;i <= n;i++){int x;cin >> x;v[x]++;}for(int i = 0;i <= 2e5;i++){// v[i] 是出现的次数for(int j = 0;j < v[i];j++){// i是出现的数字cout << i << " ";}}cout << '\n';return 0;
}

双指针

1. 一快一慢(快慢指针)
2.区间内维护某个东西(滑动窗口)

在这里插入图片描述

最长连续不重复子序列

在这里插入图片描述

题解

1. 开始的时候i = 1,j = 0
2.j向右走的条件j+1下标的数不在数组中
3. 如果出现重复的数,更新i,让i向后走
4. 代码中使用桶的思想解决

在这里插入图片描述

代码

#include<iostream>
#include<vector>
#include<algorithm>const int N = 1e5 + 10;
using namespace std;
int a[N],b[N];void solve()
{int n = 0;cin >> n;for(int i = 1;i <= n;i++) cin >> a[i];int ans = 0;// 初始化桶数组for(int i = 1;i <= n;i++) b[a[i]] = 0;for(int i = 1,j = 0;i <= n;i++){// j < n并且后一个数不在桶数组中while(j < n && !b[a[j+1]]) b[a[++j]]++;// 更新最大长度ans = max(ans,j - i + 1);// i之后要++,让i往后走一个,把之前在桶数组中的数删除b[a[i]]--;}cout << ans << '\n';
}int main()
{int T = 0;cin >> T;while(T--){solve();}return 0;
}

二分

1. 二分的步骤
在这里插入图片描述
在这里插入图片描述

查找

在这里插入图片描述

题解

1. 保证了l始终小于r, l + 1 == r
2.这样保证了a[l] < x, a[r] >= x,后续可以判断a[r] == x

代码

#include<iostream>
#include<vector>
#include<algorithm>using ll = long long;
const int N = 2e5 + 10;
using namespace std;
int a[N];void solve()
{int n = 0, q = 0;cin >> n >> q;for (int i = 1; i <= n; i++) cin >> a[i];while (q--){int x;cin >> x;int l = 0, r = n;while (l + 1 != r){int mid = l + (r - l) / 2;if (a[mid] < x) l = mid;else r = mid;}if (a[r] == x) cout << r << " ";else cout << -1 << " ";}
}int main()
{solve();return 0;
}

http://www.ppmy.cn/ops/160026.html

相关文章

Python+appium实现自动化测试

目录 一、工具与环境准备 二、开始测试 1、插上手机&#xff0c;打开usb调试&#xff0c;选中文件传输&#xff0c;我这里用华为手机为例 2、启动Appium Server GUI​编辑 3、启动 Inspector Session 4、录制脚本 使用Python和Appium进行自动化测试是一种常见的移动应用…

Linux:进程间通信(一.初识进程间通信、匿名管道与命名管道、共享内存)

目录 1.认识进程间通信 2.管道 2.1匿名管道 2.2pipe()函数 —创建匿名管道 2.3匿名管道的四种情况 2.4管道的特征 3.基于管道的进程池设计 4.命名管道 4.1引入与性质 4.2命令行创建 4.3程序中创建命名管道 写个小项目 项目规划 PipeClient.cpp PipeServe.cpp 5.…

DeepSeek VS ChatGPT-速度、准确性和成本

撰写本文时马斯克刚刚发布了聊天机器人Grok2&#xff0c;10万张算卡体现了马斯克的财大气粗。近年来&#xff0c;人工智能模型取得了长足的发展&#xff0c;每个模型都力求在速度、准确性和成本效率方面超越其他模型。在本文中&#xff0c;我将深入研究比较中美在AI的焦点模型上…

实现一个专注应用-后端开发(一)-搭建

搭建后端服务 搭建服务拆分下用户服务 增加公共库通用模块 运行一下接入数据库安装Prisma增加prisma库 redis增加redis服务 搭建服务 使用nestjs来做 这里是nestjs的网站Nestjs 安装 nest npm i -g nestjs/cli创建一个项目 并在开发工具打开 nest new todonest new xx 是新…

蓝桥杯备考:二分算法之木材加工

P2440 木材加工 - 洛谷 这种题我们就是把答案枚举出来&#xff0c;然后对答案进行二分&#xff0c;然后再进行判断 比如我们这道题&#xff0c;我们枚举切割的长度&#xff0c;然后由于切割长度越长切割段数越少 切割长度越短&#xff0c;切割段数越多的性质&#xff0c;我们…

解决gradio无法通过ip访问

目录 1.介绍 2.原因 3.解决方法一&#xff1a;修改share 4.解决方法二&#xff1a;设置本地ip 5.结果 6.附加 1.介绍 先来看看遇到的情况&#xff1a; 使用gradio作了一个图像识别的面板&#xff0c;可以通过localhost:7860访问&#xff0c;但是没法通过IP:7860访问&…

deepseek帮我设计物理量采集单片机口保护电路方案

deepseek帮我设计物理量采集单片机口保护电路方案 首先&#xff0c;我给他一个比较常见的问题&#xff0c;让他帮我设计。 问题&#xff1a;现在我想设计这样一个电路&#xff0c;因为我的模拟量采集上拉5V&#xff0c;外面不接传感器的话这个5V会直接到单片机口上&#xff0c…

什么叫不可变数据结构?

不可变数据结构(Immutable Data Structures)是指一旦创建之后,其内容就不能被修改的数据结构。这意味着任何对不可变数据结构的“修改”操作实际上都会返回一个新的数据结构,而原始数据结构保持不变。 一、不可变数据结构的核心特点 不可变性:一旦创建后,数据结构的内容…