Cod sursa(job #2771969)

Utilizator SerbaP123Popescu Serban SerbaP123 Data 30 august 2021 09:49:01
Problema Secv Scor 10
Compilator cpp-64 Status done
Runda PreOni 2005 Runda 2 Clasele 9-10 Marime 1.35 kb
#include <fstream>
#include <cmath>
#include <vector>
#include <algorithm>
#include <map>
#include <string>
#include <bitset>
#define pb(a) push_back(a)
using namespace std;

ifstream cin("secv.in");
ofstream cout("secv.out");

const int nmax = 5e3;
int v[2 * nmax], aux[2 * nmax + 1], ind[2 * nmax + 1];
vector <pair <int, int> > f;
int n, p = 1;

int main(){
    cin >> n;
    for(int i = 1; i <= n; ++i){
        cin >> v[i];
        aux[i] = v[i];
    }
    sort(aux + 1, aux + n + 1);
    int cnt = 1;
    p = 1;
    for(int i = 1; i <= n; ++i){
        if(aux[i] == aux[i + 1]){
            cnt++;
        }
        else{
            f.pb(make_pair(aux[i], cnt));
            ind[p] = aux[i];
            p++;
            cnt = 1;
        }
    }
    p--;
    int st = 0;
    for(int i = 1; i <= n; ++i){
        f[v[i] - 1].second--;
        if(v[i] == f[0].first && f[1].second){
            st = i;
        }
        else if(v[i] == f[0].first && !f[1].second){
            break;
        }
    }
    int dr = 0, k = 2;
    for(int i = st + 1; i <= n; ++i){
        if(v[i] == ind[k]){
            if(k == p){
                dr = i;
                break;
            }
            k++;
        }
    }
    if(dr == 0){
        cout << -1;
    }
    else{
        cout << dr - st + 1;
    }
    return 0;
}