算法:离散化模板

原理

离散化是把无限空间中有限的个体映射到有限的连续区间 ,核心作用:去重 + 压缩坐标

当题目中数据范围很大,但是数据的总量不是很大,**并且我们需要用数据的值来映射数组的下标时,**我们就可以用离散化的思想先预处理一下所有的数据,使的每一个数据都映射成一个范围较小的值。

比如: 9, 99, 9999, 9999999 离散之后就变成 1, 2, 3, 4。

细节:离散化后只是让对应位置的元素有了新的下标

离散化模板

1.静态数组实现

cpp 复制代码
#include <bits/stdc++.h>

using namespace std;

#define int long long 
#define endl '\n'

const int N = 2e5 + 10; 
int n;
int a[N];
int t[N];
int pos; // 离散化后的大小
int find(int x)
{
	return lower_bound(t + 1, t + 1 + pos, x) - t; // 让下标从1开始
}

void solve()
{
	cin >> n;
	for(int i = 1; i <= n; i++)
	{
		cin >> a[i];
		t[i] = a[i];
	}
	// 排序 + 去重 
	sort(t + 1, t + 1 + n); // 排序 
	pos = unique(t + 1, t + 1 + n) - t - 1; // 去重
	for(int i = 1; i <= n; i++)
	{
		cout << find(a[i]) << endl; // 离散化后的结果 
	}
}

signed main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int _ = 1;
	cin >> _;
	while(_--)
	{
		solve();
	}
	return 0;
}

不建议用unordered_map来离散化,有时候效率低,有被卡TLE

2.动态数组实现

cpp 复制代码
#include <bits/stdc++.h>

using namespace std;

#define int long long 
#define endl '\n'

void solve()
{
	int n; cin >> n;
	vector<int> a(n + 1);
	for(int i = 1; i <= n; i++)
	{
		cin >> a[i];
	}
	vector<int> t = a;
	sort(t.begin(), t.end()); // 排序 
	t.erase(unique(t.begin(), t.end()), t.end()); // 去重 	
	int pos = t.size();
	for(int i = 1; i <= n; i++)
	{
		cout << lower_bound(t.begin(), t.end(), a[i]) - t.begin() + 1 << endl;
	}
	
}

signed main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int _ = 1;
	cin >> _;
	while(_--)
	{
		solve();
	}
	return 0;
}
相关推荐
黄金龙PLUS20 分钟前
5个1024比特大状态置换算法的设计与分析
算法·网络安全·密码学·哈希算法·同态加密
心中有你021430 分钟前
C语言埃氏筛法(素数筛选)
c语言·数据结构·算法
Omics Pro2 小时前
Cell封面|衰老生物学开源AI工具包
数据库·人工智能·算法·机器学习·自然语言处理
UIU1144 小时前
递归算法与汉诺塔
c++·学习·算法·c#·递归
用户979984807189 小时前
200 行代码写一个能跑的 AI Agent:不依赖任何框架,只靠 tool-calling 原理
算法
threerocks9 小时前
【Muse实战】X 流量作战室搭建保姆级教程 - 拥有你自己的运营团队
算法
GreenTea9 小时前
深度拆解 ScienceBuddy:如何用“双层递归自进化”构建高可靠科研 Agent Harness
前端·后端·算法
花椒技术10 小时前
2.46 秒生成 5 秒视频:拆解 H3 Max 的模型、推理栈与硬件协同
算法·音视频开发·视频编码
vivo互联网技术10 小时前
ART:妆容迁移框架,重新定义高保真妆容迁移 | ECCV 2026
人工智能·算法·图像识别
用户04414409244910 小时前
卫星信号模拟器里的四个坐标转换公式
算法