离散化---洛谷1097

题目:https://www.luogu.org/problemnew/show/P1097

某次科研调查时得到了nn个自然数,每个数均不超过1500000000(1.5 \times 10^9)1500000000(1.5×109)。已知不相同的数不超过1000010000个,现在需要统计这些自然数各自出现的次数,并按照自然数从小到大的顺序输出统计结果。

 

很显然要用离散化。。。

1.将数组a排序

注意:sort(a+1,a+n+1);

2.去重

注意:

tot=unique(t+1,t+1+n)-t-1;

3.取出下标

tmp=lower_bound(t+1,t+1+tot,a[i])-t;

代码:

#include <cstdio>
#include <algorithm>
#include <cstring>

using namespace std;

const int maxS=200000;
int a[maxS+5],t[maxS+5],h[maxS+5],k[maxS+5];

int main()
{
	int i,n,tot,tmp;
	
	freopen("a.txt","r",stdin);
	
	scanf("%d",&n);
	for (i=1;i<=n;i++)
	{
		scanf("%d",&a[i]);
		t[i]=a[i];
	}
	
	sort(t+1,t+n+1);//排序 
	
	tot=unique(t+1,t+1+n)-t-1;//去重
	
	memset(h,0,sizeof(h));
	for (i=1;i<=n;i++)
	{
		tmp=lower_bound(t+1,t+1+tot,a[i])-t;
		k[tmp]=a[i];
		h[tmp]++;
	}
	for (i=1;i<=tot;i++)
	{
		printf("%d %d\n",k[i],h[i]);
	}
	
	return 0;
}

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值