Cod sursa(job #1755145)

Utilizator bogdanluncasubogdan bogdanluncasu Data 9 septembrie 2016 14:55:50
Problema Elementul majoritar Scor 60
Compilator cpp Status done
Runda Arhiva educationala Marime 0.59 kb
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
vector<int> a;
int b[1000002],n,maxv,maxc=-1,size;
int main(){
	freopen("elmaj.in", "r", stdin);
	freopen("elmaj.out", "w", stdout);
	int e,x;
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
    	scanf("%d",&x);
    	e=find(a.begin(),a.end(),x) - a.begin();
    	if(e==size){
    		a.push_back(x);
			if(maxc<0){maxc=1,maxv=x;}
			size++;
    	}else{
			b[e]++;
			if(b[e]>maxc){
				maxc=b[e];
				maxv=x;
			}
		}
			

    }
	if(maxc>=n/2)
		printf("%d %d",maxv,maxc+1);
	else printf("-1");
}