FOJ 1582 众数问题

来源:互联网 发布:股市模拟软件 编辑:程序博客网 时间:2024/06/06 13:14
众数问题

Time Limit:1sMemory limit:32MAccepted Submit:226Total Submit:926

给定含有n个元素的多重集合S,每个元素在S中出现的次数称为该元素的重数。多重集S中重数最大的元素称为众数。

例如,S={1,2,2,2,3,5}。

多重集S的众数是2,其重数为3。

数据输入

输入包括多组数据,请处理到EOF结束。

每组数据,以一个n(1<=n<=100,000)开始,接下n行,每行有一个数字(-231~231)。

数据输出

对于每组输入数据,输出一行一个数字,表示众数。如果存在多个解,只需输出值最小的众数即可。

样例输入

6
1
2
2
2
3
5
3
-1
-1
-1

样例输出

2
-1

Original: FOJ月赛-2008年4月


第一次用堆排A题的说,不过我写的慢死了...

每次找到最小的找N次就从小到大的排好了~

  1. #include <iostream>
  2. #include <algorithm>
  3. using namespace std;
  4. int heap[100000],hsize=0,a[100000];
  5. //root is 0
  6. inline void dfs(int x)
  7. {
  8.     if(x>=hsize)return;
  9.     if(2*x+1<hsize)cout<<heap[2*x+1]<<' ';
  10.     if(2*x+2<hsize)cout<<heap[2*x+2]<<' ';
  11.     dfs(2*x+1);
  12.     dfs(2*x+2);
  13. }
  14. inline void insert(int n)
  15. {
  16.     int p,c=hsize;
  17.     heap[hsize++]=n;
  18.     p=(c-1)/2;
  19.     while(c)
  20.         {
  21.             if(heap[p]>heap[c])
  22.                 {
  23.                     heap[p]^=heap[c];
  24.                     heap[c]^=heap[p];
  25.                     heap[p]^=heap[c];
  26.                     c=p;
  27.                     p=(c-1)/2;
  28.                 }
  29.             else return;
  30.         }
  31. }
  32. inline void update(int p)
  33. {
  34.     int mt,l,r;
  35.     l=2*p+1;
  36.     r=2*p+2;
  37.     if(l>=hsize&&r>=hsize)return;
  38.     if(r>=hsize)mt=l;else mt=heap[l]<heap[r]?l:r;
  39.     if(heap[p]>heap[mt]){
  40.     heap[p]^=heap[mt];
  41.     heap[mt]^=heap[p];
  42.     heap[p]^=heap[mt];
  43.     update(mt);
  44.     }
  45.     /*if((t=(p-1)/2)>=0&&heap[t]>heap[p])
  46.         {
  47.             heap[p]^=heap[t];
  48.             heap[t]^=heap[p];
  49.             heap[p]^=heap[t];
  50.             update(t);
  51.         }*/
  52. }
  53. inline int del()
  54. {
  55.     int ret=heap[0];
  56.     hsize--;
  57.     heap[0]^=heap[hsize];
  58.     heap[hsize]^=heap[0];
  59.     heap[0]^=heap[hsize];
  60.     update(0);
  61.     return ret;
  62. }
  63. int main()
  64. {
  65.     int i,t,n,tcnt,ans,val;
  66.     freopen("D://in.txt","r",stdin);
  67.     while(scanf("%d",&t)!=EOF)
  68.         {
  69.             for(i=hsize=0;i<t;i++)
  70.                 {
  71.                     scanf("%d",&n);
  72.                     insert(n);
  73.                 }
  74.             ans=1;tcnt=1;
  75.             for(i=0;i<t;i++)a[i]=del();
  76.             val=a[0];
  77.             for(i=0;i<t-1;i++)
  78.                 {
  79.                     if(a[i]==a[i+1])
  80.                         tcnt++;
  81.                     else
  82.                         {
  83.                             if(tcnt>ans){ans=tcnt;val=a[i-1];}
  84.                             tcnt=1;
  85.                         }
  86.                 }
  87.             if(tcnt>ans){ans=tcnt;val=a[t-1];}
  88.             printf("%d/n",val);
  89.         }
  90.     return 0;
  91. }

原创粉丝点击