統計數字(NOIP2007)提高組
阿新 • • 發佈:2019-02-01
1. Problem Description
某次科研調查時得到了n個自然數,每個數均不超過1500000000(1.5*10^9)。已知不相同的數不超過10000個,現在需要統計這些自然數各自出現的次數,並按照自然數從小到大的順序輸出統計結果。
2. Input
輸入檔案count.in包含n+1行;
第一行是整數n,表示自然數的個數;
第2~n+1每行一個自然數。
3. Output
輸出檔案count.out包含m行(m為n個自然數中不相同數的個數),按照自然數從小到大的順序輸出。每行輸出兩個整數,分別是自然數和該數出現的次數,其間用一個空格隔開。
輸入樣例
8
2
4
2
4
5
100
2
100
輸出樣例
2 3
4 2
5 1
100 2
#include <iostream>
#include <cstdio>
#include <stdlib.h>
using namespace std;
const int M=200005;
struct node{
int id;
}a[M];
int n,l=0;
bool cmp(node a,node b)
{ return a.id<b.id;}
void init(){
long long t,t1;
cin>>n;
for(int i=1;i<=n;i++){
scanf ("%d",&a[i].id);
}
}
void qsort(int l1,int r){//快排
int i,j;
i=l1; j=r;
node x=a[(l1+r)/2],y;
while (i<=j){
while (cmp(a[i],x)) i++;
while (cmp(x,a[j])) j--;
if (i<=j){
y=a[i]; a[i]=a[j]; a[j]=y;
i++; j--;
}
}
if (i<r) qsort(i,r);
if (l1<j) qsort(l1,j);
}
void solve(){
qsort(1,n);
int i,j;
for(i=1;i<=n;i=j){
j=i+1;
printf("%d ",a[i].id);
while(a[j].id==a[i].id)j++;
printf("%d\n",j-i);
}
}
int main(){
init();
solve();
return 0;
}