Cod sursa(job #658004)
Utilizator | Data | 7 ianuarie 2012 19:19:24 | |
---|---|---|---|
Problema | Elementul majoritar | Scor | 100 |
Compilator | cpp | Status | done |
Runda | Arhiva educationala | Marime | 0.53 kb |
#include <iostream>
#include <fstream>
#include <algorithm>
#include <ctime>
#include <cstdlib>
#define DK 10
#define DN 1000005
using namespace std;
int n,v[DN];
int main()
{
srand(time(NULL));
ifstream f("elmaj.in");
ofstream g("elmaj.out");
f>>n;
for(int i=1; i<=n; ++i) f>>v[i];
for(int j=1; j<=DK; ++j) {
int el=v[rand()%n+1],nr=0;
for(int i=1; i<=n; ++i) if(el==v[i]) ++nr;
if(2*nr>n) {
g<<el<<' '<<nr;
return 0;
}
}
g<<"-1";
return 0;
}