Cod sursa(job #3240943)

Utilizator mihaigeorgescuGeorgescu Mihai mihaigeorgescu Data 24 august 2024 16:27:45
Problema Secv Scor 40
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.95 kb
#include <fstream>
#include <map>
using namespace std;
ifstream fcin("secv.in");
ofstream fout("secv.out");
int n,v[10001],st,dr,ok,maxx,u[10001],vf;
map < int , int > m;
int main()
{
    fcin>>n;
    for(int i=1; i<=n; i++)
    {
        fcin>>v[i];
        m[v[i]]=1;
        maxx=max(maxx,v[i]);
    }
    int last=1;
    for(auto & i : m)
    {
        vf++;
        u[vf]=i.first;
        for(int j=last; j<=n; j++)
        {
            if(v[j]==i.first)
            {
                dr=j;
                last=j+1;
                i.second=2;
                break;
            }
        }
    }
    last=n;
    for(int i=vf; i>=1; i--)
    {
        for(int j=last; j>=1; j--)
        {
            if(v[j]==u[i])
            {
                st=j;
                last=j-1;
                break;
            }
        }
    }
    if(m[maxx]!=2)
        fout<<-1;
    else
        fout<<dr-st+1;
    return 0;
}