Cod sursa(job #2657576)

Utilizator AlexNicuNicu Alexandru AlexNicu Data 11 octombrie 2020 08:38:17
Problema Elementul majoritar Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.74 kb
#include <fstream>

using namespace std;
ifstream cin ( "elmaj.in" );
ofstream cout ( "elmaj.out" );
int v[1000001];
int main()
{
    int n, i, candidat, voturi;
    cin >> n;
    for ( i = 1; i <= n; i++ ) {
      cin >> v[i];
    }
    candidat = -1;
    voturi = 0;
    for ( i = 1; i <= n; i++ ) {
      if ( voturi == 0 ) {
         candidat = v[i];
         voturi = 1;
      }
      else if ( candidat == v[i] )
         voturi++;
      else if ( candidat != v[i] )
        voturi--;
    }
    voturi = 0;
    for ( i = 1; i <= n; i++ ) {
      if ( candidat == v[i] )
        voturi++;
    }
    if ( voturi >= n / 2 + 1 )
      cout << candidat << " " << voturi;
    else
      cout << "-1";
    return 0;
}