题目: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;
}