Cod sursa(job #2084937)
Utilizator | Data | 9 decembrie 2017 13:15:22 | |
---|---|---|---|
Problema | Elementul majoritar | Scor | 70 |
Compilator | cpp | Status | done |
Runda | Arhiva educationala | Marime | 0.64 kb |
#include <iostream>
#include <fstream>
using namespace std;
int main(){
ifstream f_in("elmaj.in");
int n;
f_in>>n;
int *p = &n;
int a[*p];
for (int i=0; i<n; i++){
f_in >> a[i];
}
f_in.close();
int L = 1, res = a[0];
for (int i=0; i<n-1; i++){
if (a[i] != a[i+1]) L--; else L++;
if (L == 0) {
res = a[i+1];
L = 1;
}
}
int k = 0;
for (int i=0; i<n; i++){
if (a[i] == res) k++;
}
ofstream f_out("elmaj.out");
if ( k >= (n/2)+1 ) f_out<<res<<" "<<k; else f_out<<-1;
f_out.close();
return 0;
}