Cod sursa(job #2771971)

Utilizator SerbaP123Popescu Serban SerbaP123 Data 30 august 2021 09:52:30
Problema Secv Scor 30
Compilator cpp-64 Status done
Runda PreOni 2005 Runda 2 Clasele 9-10 Marime 1.01 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[nmax + 1], aux[nmax + 1], ind[nmax + 1];
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);
    for(int i = 1; i <= n; ++i){
        if(aux[i] != aux[i + 1]){
            ind[p] = aux[i];
            p++;
        }
    }
    p--;
    int k = 1, st = 0, dr = 0;
    bool gasit1 = false, gasit2 = false;
    for(int i = 1; i <= n; ++i){
        if(v[i] == ind[k]){
            if(k == 1){
                st = i;
            }
            if(k == p){
                dr = i;
            }
            k++;
        }
    }
    if(dr != 0){
        cout << dr - st + 1;
    }
    else{
        cout << -1;
    }
    return 0;
}