离散化模板

//离散化方法一: 
#include<bits/stdc++.h>
using namespace std;
const int maxn=100000+100;
int a[maxn],b[maxn],c[maxn];
int main ()
{
    int n,m=0;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&a[i]);
        b[++m]=a[i];
    }
    sort(b+1,b+1+m);
    m=unique(b+1,b+1+m)-b-1;
    for(int i=1;i<=n;i++)
    {
        c[i]=lower_bound(b+1,b+1+m,a[i])-b;
        cout<<c[i]<<" ";//c[i]存的是离散化后的结果 
    }
    cout<<endl;
} 
//坐标离散化  或者数组离散化 
#include<bits/stdc++.h>
using namespace std;
const int maxn=100000+100;
struct node
{
    int x,id;
}point[maxn*2+100];
int cmp(node aa,node bb)
{
    return aa.x<bb.x;
}
int cmmp(node aa,node bb)
{
    if(aa.id==bb.id)
        return aa.x<bb.x;
    return aa.id<bb.id;
}
int main ()
{
    int n;
    cin>>n;
    int id=1;
    for(int i=1;i<=2*n;i+=2)
    {
        scanf("%d %d",&point[i].x,&point[i+1].x);
        point[i].id=id;
        point[i+1].id=id++;
    }
    sort(point+1,point+1+2*n,cmp);
    int pre=-1,tot=0;
    for(int i=1;i<=2*n;i++)
    {
        if(pre==point[i].x)
        {
            point[i].x=tot;
        }
        else
        {
            pre=point[i].x;
            point[i].x=++tot;

        }
    }
    sort(point+1,point+1+2*n,cmmp);
    for(int i=1;i<=2*n;i+=2)
    {
        printf("%d %d\n",point[i].x,point[i+1].x);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值