Cod sursa(job #2479733)
| Utilizator | Data | 24 octombrie 2019 13:48:12 | |
|---|---|---|---|
| Problema | Elementul majoritar | Scor | 100 |
| Compilator | cpp-64 | Status | done |
| Runda | Arhiva educationala | Marime | 0.57 kb |
#include<bits/stdc++.h>
using namespace std;
long long n,v[1000001],contor;
int main()
{
ifstream f("elmaj.in");
ofstream g("elmaj.out");
f>>n;
f >> v[1];
int x = v[1];
int nr = 1;
for(int i=2;i<=n;i++){
f>>v[i];
if(v[i]!=x)
nr--;
else
nr++;
if(nr<0){
x=v[i];
nr=1;
}
}
if(nr>0)
for(int i=1;i<=n;i++)
if(v[i]==x)
contor++;
if(contor>=n/2+1)
g<<x<<" "<<contor;
else
g<<"-1";
}
