Cod sursa(job #1658785)

Utilizator maenstru56Peteleaza Darius maenstru56 Data 21 martie 2016 19:51:16
Problema Elementul majoritar Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.92 kb
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <time.h>

using namespace std;

ifstream in("elmaj.in");
ofstream out("elmaj.out");

long long v[99999999],n,el,ct,nr,i,j,val;
bool p[1000000];

int main()
{
    in>>n;

    nr=(n/2)+1;

    for(i=1;i<=n;i++)
        in>>v[i];

    srand(time(NULL));

    while(true) {

        ct = 0;
        bool ch = true;

        for(int i = 1; i <= n; i++) {
            if(p[i] == 0) {
                ch = false;
                break;
            }
        }

        if(ch)
            break;

        while(true) {
            val = rand()%n+1;
            if(p[val] == 0)
                break;
        }

        for(j=1;j<=n;j++)
            if(v[val]==v[j])
                ct++;

        if(ct>=nr){
            out << v[val] << " " << ct;
            return 0;
        }
    }

    out << "-1";

    return 0;
}