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次就从小到大的排好了~
- #include <iostream>
- #include <algorithm>
- using namespace std;
- int heap[100000],hsize=0,a[100000];
- inline void dfs(int x)
- {
- if(x>=hsize)return;
- if(2*x+1<hsize)cout<<heap[2*x+1]<<' ';
- if(2*x+2<hsize)cout<<heap[2*x+2]<<' ';
- dfs(2*x+1);
- dfs(2*x+2);
- }
- inline void insert(int n)
- {
- int p,c=hsize;
- heap[hsize++]=n;
- p=(c-1)/2;
- while(c)
- {
- if(heap[p]>heap[c])
- {
- heap[p]^=heap[c];
- heap[c]^=heap[p];
- heap[p]^=heap[c];
- c=p;
- p=(c-1)/2;
- }
- else return;
- }
- }
- inline void update(int p)
- {
- int mt,l,r;
- l=2*p+1;
- r=2*p+2;
- if(l>=hsize&&r>=hsize)return;
- if(r>=hsize)mt=l;else mt=heap[l]<heap[r]?l:r;
- if(heap[p]>heap[mt]){
- heap[p]^=heap[mt];
- heap[mt]^=heap[p];
- heap[p]^=heap[mt];
- update(mt);
- }
-
- }
- inline int del()
- {
- int ret=heap[0];
- hsize--;
- heap[0]^=heap[hsize];
- heap[hsize]^=heap[0];
- heap[0]^=heap[hsize];
- update(0);
- return ret;
- }
- int main()
- {
- int i,t,n,tcnt,ans,val;
- freopen("D://in.txt","r",stdin);
- while(scanf("%d",&t)!=EOF)
- {
- for(i=hsize=0;i<t;i++)
- {
- scanf("%d",&n);
- insert(n);
- }
- ans=1;tcnt=1;
- for(i=0;i<t;i++)a[i]=del();
- val=a[0];
- for(i=0;i<t-1;i++)
- {
- if(a[i]==a[i+1])
- tcnt++;
- else
- {
- if(tcnt>ans){ans=tcnt;val=a[i-1];}
- tcnt=1;
- }
- }
- if(tcnt>ans){ans=tcnt;val=a[t-1];}
- printf("%d/n",val);
- }
- return 0;
- }