Cod sursa(job #2771966)

Utilizator SerbaP123Popescu Serban SerbaP123 Data 30 august 2021 09:30:20
Problema Secv Scor 0
Compilator cpp-64 Status done
Runda PreOni 2005 Runda 2 Clasele 9-10 Marime 1.32 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];
vector <pair <int, int> > f, aux1;
int n, p = 1;

void refacere(){
    f.clear();
    for(int i = 0; i < aux1.size(); ++i){
        f.pb(make_pair(aux1[i].first, aux1[i].second));
    }
}

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;
    for(int i = 1; i <= n; ++i){
        if(aux[i] == aux[i + 1]){
            cnt++;
        }
        else{
            f.pb(make_pair(aux[i], cnt));
            aux1.pb(make_pair(aux[i], cnt));
            cnt = 1;
        }
    }
    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;
        }
    }
    int dr = 0, k = 1;
    for(int i = st + 1; i <= n; ++i){
        if(v[i] == f[k].first){
            if(k == f.size() - 1){
                dr = i;
                break;
            }
            k++;
        }
    }
    cout << dr - st + 1;
    return 0;
}