离散化

没有去重的

    for(int i=1;i<=n;i++) p[i]=read(),b[i]=i;
    sort(b+1,b+n+1,cmp);
    for(int i=1;i<=n;i++) p[b[i]]=i;
int len=0;
int len=0,flag=0;
for(int i=1;i<=n;i++){
    scanf("%lld%lld%d",&e[i].l,&e[i].r,&e[i].w);
    b[++len]=e[i].l;b[++len]=e[i].r;
}
sort(b+1,b+1+len);int m=unique(b+1,b+1+len)-b-1;
for(int i=1;i<=n;i++){
    e[i].l=lower_bound(b+1,b+1+m,e[i].l)-b;
    e[i].r=lower_bound(b+1,b+1+m,e[i].r)-b;
}




#include <cstdio>
#include <algorithm>

using namespace std;
int n;
int a[1000005];
int b[1000005]; // 离散化后的数字数组
int c[1000005]; // 临时的数组

int main(){
	scanf("%d",&n);
	for(int i=1;i<=n;i++) scanf("%d",&a[i]);
		// 1312312 23131 312321 23131 2323312
	// 1. 排序
	// 2. 去重
	// 3. 二分

	for(int i=1;i<=n;i++) c[i]=a[i];
		// 23131 23131  312321 1312312    2323312
	sort(c+1,c+n+1);
	// 想用c数组的下标 来代替原始的数字
	// 1 2 2 3 3 3
	// unique  1 2 3 2 3 3
	int tot = unique(c+1,c+n+1)-(c+1);
	for(int i=1;i<=n;i++){
		b[i] = lower_bound(c+1,c+tot+1, a[i])-c;
	}
	for(int i=1;i<=n;i++) printf("%d ",b[i]);
	// lower_bound(begin, end, x) STL
	// 
	// 返回
	// lower_bound, upper_bound
	// lower_bound 求出 >=x 的第一个数字的位置
	// upper_bound 求出 >x 的第一个数字的位置

	return 0;
}
posted @ 2020-08-28 17:17  ke_xin  阅读(19)  评论(0编辑  收藏  举报