Pagini recente » Cod sursa (job #184040) | Cod sursa (job #2785965) | Cod sursa (job #2270871) | Cod sursa (job #512820) | Cod sursa (job #3164865)
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream input("elmaj.in");
ofstream output("elmaj.out");
int a[1000000], b[1000000], dbmaj=0, i, N, temp;
input>>N;
for(i=0; i<N; i++){
input>>a[i];
b[i]=a[i];
}
int bal=0, jobb=1;
while(jobb<N){
if(a[bal]!=a[jobb]){
a[bal]=-1;
a[jobb]=-1;
if(bal==0){
jobb=jobb+2;
bal=jobb-1;
} else {
while(a[bal]==-1 && bal>0){
bal=bal-1;
}
if(bal==0){
jobb=jobb+2;
bal=jobb-1;
}
else{
jobb=jobb+1;
}
}
} else {
jobb=jobb+1;
bal=jobb-1;
}
}
if(bal<N) temp=a[bal];
else temp=a[bal-1];
for(i=0; i<N; i++){
if(b[i]==temp) dbmaj++;
}
if(dbmaj>=N/2+1) output<<temp<<" "<<dbmaj;
else output<<-1;
return 0;
}