Pagini recente » Cod sursa (job #2339597) | Cod sursa (job #523422) | Cod sursa (job #1024900) | Cod sursa (job #447358) | Cod sursa (job #2341201)
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, x, bucket;
int cand, fr;
vector<int> lista;
FILE *fin = fopen("elmaj.in", "r");
fscanf(fin, "%d", &n);
bucket = 0;
for(int i = 0; i < n; ++i) {
fscanf(fin, "%d", &x);
if(lista.empty())
lista.push_back(x);
else if(lista.back() == x)
bucket++;
else {
lista.push_back(x);
if(bucket > 0) {
bucket--;
lista.push_back(lista[lista.size() - 2]);
}
}
}
fclose(fin);
fr = bucket;
cand = lista.back();
while(!lista.empty()) {
if(lista.back() == cand) {
fr++;
lista.pop_back();
if(!lista.empty())
lista.pop_back();
else
bucket++;
} else {
lista.pop_back();
if(bucket > 0)
bucket--;
}
}
FILE *fout = fopen("elmaj.out", "w");
if(bucket > 0)
fprintf(fout, "%d %d", cand, fr);
else
fprintf(fout, "-1");
fclose(fout);
return 0;
}