Cod sursa(job #2084934)

Utilizator darkcat013Noroc Viorel darkcat013 Data 9 decembrie 2017 13:10:13
Problema Elementul majoritar Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.42 kb
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

ifstream fin;
ofstream fout;
int main()
{
    int n,i,value,hero,life=0,max=0;
    vector<int> element;
    fin.open("elmaj.in");
    fin>>n;
    for(i=0;i<n;i++)
    {
        if(i==0)
        {
            fin>>value;
            element.push_back(value);
            hero=element[i];
            life++;
            max++;
            cout<<hero<<" "<<life<<" "<<max<<"\n";
        }
        else if(i>0)
        {
            fin>>value;
            element.push_back(value);
            cout<<element[i]<<"\n";
            if(element[i]!=hero)
            {
                life--;
                cout<<hero<<" "<<life<<" "<<max<<"\n";
                if(life==0) 
                {
                    hero=element[i];
                    life++;
                    max=1;
                    cout<<hero<<" "<<life<<" "<<max<<"\n";
                }
                // else if(life!=0)
                // {
                    
                // }
            }
            else if(element[i]==hero)
            {
                life++;
                max++;
                cout<<hero<<" "<<life<<" "<<max<<"\n";
            }
        }
    }
    fin.close();
    fout.open("elmaj.out");
    cout<<max;
    if(max>=n/2-1) fout<<hero<<" "<<max;
    else fout<<"-1";
    fout.close();
    return 0;
}