Cod sursa(job #1154852)

Utilizator TimitocArdelean Andrei Timotei Timitoc Data 26 martie 2014 13:58:20
Problema Elementul majoritar Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.93 kb
#include <iostream>
#include <fstream>
#include <map>

using namespace std;

map<int, int> m;
int n, ok, a[1000005], x, k, nre;

int main()
{
    ifstream fin("elmaj.in");
    ofstream fout("elmaj.out");

    int t;
    fin >> n;
    for(int i=0; i<n; i++){
        fin >> t;
        a[i] = t;
        if(k==0){
            k = 1;
            x = a[i];
        }
        else if(a[i] == x)
            k++;
        else
            k--;
    }
    for(int i=0; i<n; i++)
        if(a[i] == x)
            nre++;
    if(nre >= n/2+1)
        fout << x << " " << nre;
    else
        fout << "-1";
   /* for(map<int, int>::iterator i=m.begin(); i!=m.end(); i++){
        if(i->second >= n/2+1){
            //printf("%d %d\n", i->first, i->second);
            fout << i->first << " " << i->second << "\n";
            ok = 1;
            break;
        }
    }
    if(!ok) printf("-1\n");*/


    return 0;
}