Cod sursa(job #1096525)
Utilizator | Andrei Prahoveanu BlueStrut | Data | 2 februarie 2014 11:44:50 |
---|---|---|---|
Problema | Elementul majoritar | Scor | 100 |
Compilator | cpp | Status | done |
Runda | Arhiva educationala | Marime | 0.54 kb |
#include<cstdio>
using namespace std;
int i, n, nr, a[1000003], cand;
int main(){
freopen("elmaj.in","r",stdin);
freopen("elmaj.out","w",stdout);
scanf("%d", &n); cand=-1; nr=0;
for (i=1;i<=n;i++) {
scanf("%d", &a[i]);
if (nr==0) {nr=1; cand=a[i];} else
if (a[i]==cand) nr++; else nr--;
}
if (cand==-1) printf("%d\n", cand); else {
nr=0;
for (i=1;i<=n;i++) if (a[i]==cand) nr++;
if (nr>=n/2+1) printf("%d %d\n", cand, nr); else printf("-1\n");
}
return 0;
}