Cod sursa(job #1130755)

Utilizator AndreiDumitrescuAndrei Dumitrescu AndreiDumitrescu Data 28 februarie 2014 15:26:26
Problema Elementul majoritar Scor 80
Compilator cpp Status done
Runda Arhiva educationala Marime 1.28 kb
#include <stdio.h>

using namespace std;
int v[2000001];

int main()
{
    freopen("elmaj.in","r",stdin);
    freopen("elmaj.out","w",stdout);


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

}