[算法]离散化

实现方法

补一个STL的离散化。
先排序,再unique一下,然后lower_bound找出每个数再离散后序列里对应的位置。

代码

#include <cstdio>
#include <algorithm>

using namespace std;

namespace fast_IO{
    const int IN_LEN = 10000000, OUT_LEN = 10000000;
    char ibuf[IN_LEN], obuf[OUT_LEN], *ih = ibuf + IN_LEN, *oh = obuf, *lastin = ibuf + IN_LEN, *lastout = obuf + OUT_LEN - 1;
    inline char getchar_(){return (ih == lastin) && (lastin = (ih = ibuf) + fread(ibuf, 1, IN_LEN, stdin), ih == lastin) ? EOF : *ih++;}
    inline void putchar_(const char x){if(oh == lastout) fwrite(obuf, 1, oh - obuf, stdout), oh = obuf; *oh ++= x;}
    inline void flush(){fwrite(obuf, 1, oh - obuf, stdout);}
    int read(){
        int x = 0; int zf = 1; char ch = ' ';
        while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar_();
        if (ch == '-') zf = -1, ch = getchar_();
        while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar_(); return x * zf;
    }
    void write(int x){
        if (x < 0) putchar_('-'), x = -x;
        if (x > 9) write(x / 10);
        putchar_(x % 10 + '0');
    }
}

using namespace fast_IO;

const int MAXN = 100005;
int a[MAXN], b[MAXN];
 
int main(){
    int n = read(); 
    for(int i = 0; i < n; i++)
        a[i] = b[i] = read();
    sort(b, b + n);
    int m = unique(b, b + n) - b;
    for(int i = 0; i < n; i++)
        a[i] = lower_bound(b, b + m, a[i]) - b;
    return 0;
}
posted @ 2019-07-17 14:43  LinZhengmin  阅读(228)  评论(0编辑  收藏  举报

Contact with me