【SSL】1063统计数字
Time Limit:1000MS
Memory Limit:256000K
Description
某次科研调查时得到了n个自然数,每个数均不超过1500000000(1.5*109)。已知不相同的数不超过10000个,现在需要统计这些自然数各自出现的次数,并按照自然数从小到大的顺序输出统计结果。
Input
输入包含n+1行;
第一行是整数n,表示自然数的个数;
第2~n+1每行一个自然数。
Output
输出包含m行(m为n个自然数中不相同数的个数),按照自然数从小到大的顺序输出。每行输出两个整数,分别是自然数和该数出现的次数,其间用一个空格隔开。
Sample Input
8
2
4
2
4
5
100
2
100
Sample Output
2 3
4 2
5 1
100 2
Hint
40%的数据满足:1<=n<=1000
80%的数据满足:1<=n<=50000
100%的数据满足:1<=n<=200000,每个数均不超过1500 000 000(1.5*109)
思路
用hash储存。
快排。
代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
struct h
{
long long cs,s;
}hash[15010];
long long n,len=15000;
long long HASH(long long x)//hash函数
{
return x%10007;
}
long long locate(long long x)//x定位
{
long long i,w=HASH(x);
for(i=0;i<len&&hash[(w+i)%len].s!=-1&&hash[(w+i)%len].s!=x;i++);
return (w+i)%len;
}
void insert(long long x)//插入x
{
long long w=locate(x);
hash[w].s=x;
hash[w].cs++;
return;
}
bool cmp(h t1,h t2)
{
return t1.s<t2.s;
}
int main()
{
long long i,x;
scanf("%lld",&n);
for(i=1;i<=15000;i++)
{
hash[i].cs=0;
hash[i].s=-1;
}
for(i=1;i<=n;i++)
{
scanf("%lld",&x);
insert(x);
}
sort(hash,hash+15005,cmp);//排序
for(i=0;i<=15005;i++)
if(hash[i].cs>0)
printf("%lld %lld\n",hash[i].s,hash[i].cs);
return 0;
}