Cod sursa(job #1084856)

Utilizator lilian_ciobanuLilian Ciobanu lilian_ciobanu Data 16 ianuarie 2014 20:25:55
Problema Elementul majoritar Scor 90
Compilator cpp Status done
Runda Arhiva educationala Marime 0.94 kb
#include<iostream>
#include<fstream>
#include<algorithm>
#include<string>
#include<vector>
#include<map>

using namespace std;

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

int main(){
    int n,i,x;
    int m,r=-1;
    map<int , int> a;
    map<int ,int>::iterator it;
    string s;

    f>>n;
    getline(f,s);
    getline(f,s);

    m=s.length();
    int nr=0;

    for(i=0; i<=m; ++i){
        if(s[i]==' ' || i==m){
            if(a.find(nr)->first>0)
                a.insert(pair<int ,int>(nr,0));
            a[nr]++;
           // g<<nr<<" ";
            nr=0;
        }
        else{
            nr*=10;
            nr+=s[i]-'0';
        }
    }

    m=n/2;

    for(it=a.begin(); it!=a.end(); ++it){
        if(it->second > m) {
            r=it->first;
            x=it->second;
            break;
        }
    }

    if(r!=-1)
        g<<r<<" "<<x;
    else
        g<<"-1";


return 0;
}