前言:本人学习算法不长,在此写篇学习内容,记录一下自己的学习经历
离散化及其相关知识
离散化
离散化是一个重要内容,让我们分几个方面去说它
- 离散化是什么?
离散化是指把无限空间中有限的个体映射到有限的空间中去,在算法和数据结构中指的是将连续的输入数据映射到离散的值或者范围,从而使得处理和计算变得更高效. (映射的含义:传送门)
2.离散化的实现
离散化的实现包括以下几个步骤:
(1).对原数组排序
(2).去重
(3).通过二分进行排名映射(建议使用lower_bound())
注意事项
- 离散化时,应将查询数一并加入映射数组中排名,否则会将两个不同的数映射成相同的数。
- 模版代码
#include<bits/stdc++.h>
using namespace std;
#define maxn 1000030
int a[maxn], q[maxn], b[2 * maxn], cnt[2 * maxn];
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
b[i] = a[i];
}
int ip;
cin >> ip;
for (int i = 1; i <= ip; i++) {
cin >> q[i];
b[i + n] = q[i];
}
sort(b + 1, b + n + 1 + ip);
int nn = unique(b + 1, b + n + 1 + ip) - b - 1;
for (int i = 1; i <= n; i++) {
int x = lower_bound(b + 1, b + nn + 1, a[i]) - b - 1;
cnt[x] ++;
}
for (int i = 1; i <= ip; i++) {
int id = lower_bound(b + 1, b + nn + 1, q[i]) - b - 1;
cout << cnt[id] << "\n";
}
return 0;
}哈希表
相信各位同学看到了,离散化基本上需要离线操作。(其实也可以不离线),那当题目故意让我们在线时,我们该怎么办呢?下面,有请哈希表上场!
-
哈希表是什么
哈希表是指是根据关键码值而直接进行访问的数据结构。也就是说,它通过把关键码值映射到表中一个位置来访问记录,以加快查找的速度。 -
哈希表的实现
(1).开一个vector <pair<int, int> >数组
(2).被你开的模数取模
(3).在所创数组对应组(及你所模出的数)中,看是否已有这个数,有,就不管;无,添加。 3.模版代码
#include<bits/stdc++.h>
using namespace std;
#define mod 2333333
const int N = 50010, M = 1e6 + 10;
vector <pair<int, int> > Hash[mod+10];
void insert(int num) {
int id = num%mod;
for(auto &i : Hash[id]){
if(i.first == num){
i.second ++;
return ;
}
}
Hash[id].push_back({num, 1});
}
int query(int num){
int id = num % mod;
for(auto i : Hash[id]){
if(i.first == num){
return i.second;
}
}
return 0;
}
int main() {
int n;
cin >> n;
for(int i=1;i<=n;i++){
int temp;
cin >> temp;
insert(temp);
}
int m, last = 0;
cin >> m;
for(int i=1;i<=m;i++){
int temp;
cin >> temp;
temp^=last;
cout << query(temp) << "\n";
last = query(temp);
}
return 0;
}注意事项
- 请用vector,否则呼出内存问题
- 请用质数做为模数,以减少哈希冲突
哈希冲突
在刚刚的实现中,不难发现,有些数取模之后值一样。这种情况被称为哈希冲突。一般解决法为开数组存。但这有些浪费空间。那我们该怎么办呢?
此时,我们不难发现,冲突概率好像是—我不会打—。至于证法·····点这里。
所以,我们可以来一个大一点的模数,来不开数组,简少空间消耗。
赌就完了!
map
- map是什么
C++ STL中map是关联容器,提供一对一hash映射,可存任意类型数据。 - 用法
- 可以直接通过下标访问来进行查询或插入操作。例如 mp[“Alan”]=100。 通过向 map 中插入一个类型为 pair<Key, T> 的值可以达到插入元素的目的,例如 mp.insert(pair<string,int>(“Alan”,100));;
- erase(key) 函数会删除键为 key 的 所有 元素。返回值为删除元素的数量。 erase(pos): 删除迭代器为 pos 的元素,要求迭代器必须合法。
- erase(first,last): 删除迭代器在 范围内的所有元素。
- clear() 函数会清空整个容器。
- count(x): 返回容器内键为 x 的元素数量。复杂度为 O(\log(size)+ans)(关于容器大小对数复杂度,加上匹配个数)。
- find(x): 若容器内存在键为 x 的元素,会返回该元素的迭代器;否则返回 end()。
- lower_bound(x): 返回指向首个不小于给定键的元素的迭代器。
- upper_bound(x): 返回指向首个大于给定键的元素的迭代器。若容器内所有元素均小于或等于给定键,返回 end()。
- empty(): 返回容器是否为空。
- size(): 返回容器内元素个数。
- 模版代码
#include<bits/stdc++.h>
using namespace std;
int n, m, a[(int)1e5 + 10], oper, x, y;
map<long long, long long>ans;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cin >> n >> m;
for (int i = 1; i <= n; i++) {
cin >> a[i];
ans[a[i]] ++;
}
for (int i = 1; i <= m; i++) {
cin >> oper;
if (oper == 1) {
cin >> x;
cout << ans[x] << "\n";
} else if (oper == 2) {
cin >> x;
ans.erase(x);
} else if (oper == 3) {
cin >> x >> y;
ans[y] += ans[x];
ans.erase(x);//erase(l)时,可为任意类型。
} else {
cin >> x >> y;
auto l = ans.lower_bound(x);
auto r = ans.upper_bound(y);
ans.erase(l, r);//erase(l, r)时,要保证l和r为地址。
}
}
return 0;
}set
1.set是什么
set 是C++标准模板库(STL)中的一个关联容器,用于存储唯一且自动排序的元素集合。
2.set用法
- insert(x) 当容器中没有等价元素的时候,将元素 x 插入到 set 中。
- erase(x) 删除值为 x 的 所有 元素,返回删除元素的个数。
- erase(pos) 删除迭代器为 pos 的元素,要求迭代器必须合法。
- erase(first,last) 删除迭代器在 范围内的所有元素。
- clear() 清空 set。
- begin()/cbegin() 返回指向首元素的迭代器,其中 *begin = front。
- end()/cend() 返回指向数组尾端占位符的迭代器,注意是没有元素的。
- rbegin()/crbegin() 返回指向逆向数组的首元素的逆向迭代器,可以理解为正向容器的末元素。
- rend()/crend() 返回指向逆向数组末元素后一位置的迭代器,对应容器首的前一个位置,没有元素。
- count(x) 返回 set 内键为 x 的元素数量。
- find(x) 在 set 内存在键为 x 的元素时会返回该元素的迭代器,否则返回 end()。
- lower_bound(x) 返回指向首个不小于给定键的元素的迭代器。如果不存在这样的元素,返回 end()。
- upper_bound(x) 返回指向首个大于给定键的元素的迭代器。如果不存在这样的元素,返回 end()。
- empty() 返回容器是否为空。
- size() 返回容器内元素个数。
3.模版代码
#include<bits/stdc++.h>
using namespace std;
#define int long long
int n, x, cnt;
string s;
set<int>ans;
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> s >> x;
if(s == "Add"){
ans.insert(x - cnt);
} else if(s == "Remove"){
ans.erase(x - cnt);
} else if(s == "Pre"){
auto l = ans.lower_bound(x - cnt);
if(l == ans.begin()){
cout << -1 << "\n";
continue;
}
l --;
cout << (*l) + cnt << "\n";
} else if(s == "Suc"){
auto l = ans.upper_bound(x - cnt);
if(l == ans.end()){
cout << -1 << "\n";
continue;
}
cout << (*l) + cnt << "\n";
} else if(s == "ShiftL"){
cnt -= x;
} else {
cnt += x;
}
}
return 0;
}