Cod sursa(job #1130707)

Utilizator AndreiDumitrescuAndrei Dumitrescu AndreiDumitrescu Data 28 februarie 2014 15:03:16
Problema Elementul majoritar Scor 80
Compilator cpp Status done
Runda Arhiva educationala Marime 0.95 kb
#include <fstream>

using namespace std;
int v[2000001];

int main()
{
    ifstream f("elmaj.in");
    ofstream g("elmaj.out");

    int n , i , contor = 1, b = -1 , j;
    f >> n ;
    for(i = 1; i <= n ; ++i)
        f >> v[i];
    for(i = 1; i < n/2 ; ++i)
    {
        if(v[i] != -1)
        {
            for(j = i + 1 ; j <= n ; ++j)
                {
                    if(v[i] == v[j])
                        {
                            contor++;
                            v[j] = -1 ;
                        }
                    if(j == n / 2 && contor + n / 2 <(n / 2 + 1) )
                        j = n + 1;
                }
            if(contor >= (n / 2 + 1))
            {
                g << v[i] << " " << contor ;
                b = 0 ;
                contor = 1;
                i = n + 1 ;
            }
            else
                contor = 1;
        }
    }
    if(b == -1)
        g << b ;

}