2007年分区联赛提高组之一 统计数字

来源:互联网 发布:cf卡数据恢复多少钱 编辑:程序博客网 时间:2024/05/09 04:15

2007年分区联赛提高组之一 统计数字

Time Limit:1000MS  Memory Limit:256000K
Total Submit:304 Accepted:129

Description

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

Input

  输入包含n+1行;
  第一行是整数n,表示自然数的个数;
  第2~n+1每行一个自然数。

Output

  输出包含m行(m为n个自然数中不相同数的个数),按照自然数从小到大的顺序输出。每行输出两个整数,分别是自然数和该数出现的次数,其间用一个空格隔开。

Sample Input

8242451002100

Sample Output

2 34 25 1100 2

Hint

40%的数据满足:1<=n<=1000

80%的数据满足:1<=n<=50000

100%的数据满足:1<=n<=200000,每个数均不超过1500 000 000(1.5*109)  


解法:把每个数放进hash表对应位置并记录这个数出现过几次


  • const  maxn=1000007;var  h:array[0..maxn,1..2] of longint;  a:array[0..maxn] of longint;  i,j,n,tmp:longint;function locate(x:longint):longint;var  i:longint;begin  i:=x mod maxn;  while (h[i,1]<>0) and (h[i,1]<>x) do    i:=(i+1) mod maxn;  locate:=i;end;procedure qsort(l,r:longint);var  i,j,k:longint;begin  if l>=r then exit;  i:=l;j:=r;  k:=a[(l+r) div 2];  repeat    while a[i]<k do inc(i);    while a[j]>k do dec(j);    if i<=j then      begin        a[0]:=a[i];        a[i]:=a[j];        a[j]:=a[0];        inc(i);        dec(j);      end;  until i>j;  qsort(l,j);qsort(i,r);end;begin  readln(n);  for i:=1 to n do    readln(a[i]);  qsort(1,n);  for i:=1 to n do    begin      tmp:=locate(a[i]);      h[tmp,1]:=a[i];      inc(h[tmp,2]);    end;  for i:=1 to n do    if a[i]<>a[i+1] then      writeln(h[locate(a[i]),1],' ',h[locate(a[i]),2])end.


0 0
原创粉丝点击