zoukankan      html  css  js  c++  java
  • 离散化

    没有去重的

        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;
    }
    
  • 相关阅读:
    thusc总结
    5.12总结
    5.9总结
    C语言学习之笔记
    C语言----------指针
    typedef , static和 extern
    数据库(mysql5.5)的一些基本的操作
    Java中基本数据类型占几个字节多少位
    java &和&& 以及 |和 ||之间的异同点
    拨开云雾见月明
  • 原文地址:https://www.cnblogs.com/ke-xin/p/13578626.html
Copyright © 2011-2022 走看看