离散化初步

离散化初步:
离散化对于处理数据值较大,数组无法存储,而值又与所求结果关系不大时有妙用
这里利用系统标准模板库STL实现,考场上建议用STL,手写会很麻烦的。。。。。。。。。

1.sort(a,a+n)排序。。。。这个不用说

2.unique(a,a+n)去重,返回最后那个完成去重的点往后一个位置,好像是地址,所以减一个a;

3.lower-boud(a,a+n,x)返回a[0]-a[n-1]中第一个>=x的地址,若无,返回最末端的值,即a[n];

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>//STL要用此头文件 
using namespace std;
int sub[10000],n;
int a[10000];
int main(){
	scanf("%d",&n);
	for(int i=0;i<n;++i)scanf("%d",&sub[i]),a[i]=sub[i];
	sort(sub,sub+n);
	int size=unique(sub,sub+n)-sub;
	for(int i=0;i<n;i++)
	a[i]=lower_bound(sub,sub+size,a[i])-sub+1;
	for(int i=0;i<n;++i)cout<<a[i]<<' ';
	return 0;
}
附离散化应用题NOI2016区间(还要用线段树。。。。。)http://www.lydsy.com/JudgeOnline/problem.php?id=4653

原文地址:https://www.cnblogs.com/zzmmm/p/6501178.html