离散化模板

 
 
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=10000000;
int a[maxn];
int b[maxn];
int n;
int main()
{
    int m=0;
    cin>>n;//元素个数
    for(int i=1;i<=n;++i)
    {
    scanf("%d",&a[i]);
    b[++m]=a[i];//b[]作为离散化的数组
    }
    sort(b+1,b+1+m);//将b数组排序,因为是从b[1]开始存储的,所以要b+1→保证大小
    m=unique(b+1,b+1+m)-b-1;//去重操作,返回不同元素的个数→保证出现次数
    for(int i=1;i<=n;++i)
    a[i]=lower_bound(b+1,b+1+m,a[i])-b; //cout<<lower_bound(b+1,b+1+m,a[i])-b<<" ";
    return 0;
}

例题:51Nod1686,待更新

问题导向:

记录数字出现次数

离散化原因:

值太大,作为数组下标存不下。

只与相对大小有关。

区间比数字大小 小。如n为1e5,数字大小为1e32

转自:https://blog.csdn.net/Cirspring/article/details/51836776


猜你喜欢

转载自blog.csdn.net/zjyang12345/article/details/80407090