Cod sursa(job #2592112)

Utilizator beo1980Vlad Beu beo1980 Data 1 aprilie 2020 09:41:08
Problema Secv Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.02 kb
using namespace std;
#include <vector>
#include <iostream>
#include <fstream>
#include <cmath>
#include <algorithm>
vector<long>  sirInitial; // input array
vector<int>  sortedVector; //input array sorted
vector<int> uniqueV; // vector of unique values from input array
int n;

int main(){
    
    long val, solMax, indexMax;
    ifstream f("secv.in");
    ofstream g("secv.out");
    f>>n;
    for(int i = 0; i<n; i++){
        f>>val;
        sirInitial.push_back(val);
        sortedVector.push_back(val);

    }
    std::sort(sortedVector.begin(), sortedVector.end());
    int uniqueValues =1;
    uniqueV.push_back(sortedVector[0]);
    for(int i=1; i<n; i++)
        if(sortedVector[i]!= sortedVector[i-1])
        {
         uniqueValues++;
         uniqueV.push_back(sortedVector[i]); // construct a vector with unique values
        }
cout<<"uniqie values " <<uniqueValues<<endl;
int startIndex = -1; //index for soluton starts
int currentIndex = 0; //used to iterate throuth the arrray
int currentIndexU = 0; //used to iterate throught the unique values array 
int solLen = n+1;

while (startIndex + uniqueValues - currentIndexU <=n)   //while a solutin is still possible to be fond
{
    
    //check if sol find
    //cout<<startIndex<<" " <<currentIndex << " " << currentIndexU<<endl;
    if (currentIndexU == uniqueValues)
    {
        if (currentIndex - startIndex < solLen) solLen = currentIndex - startIndex;
        if (solLen == uniqueValues) break; //optimize... 

        startIndex++;
        currentIndex = startIndex;
        currentIndexU = 0;

    }
    if (currentIndex == n+1){
        startIndex++;
        currentIndex = startIndex;
        currentIndexU = 0;
        continue;
    }
    //check if matching value
    if (sirInitial[currentIndex] == uniqueV[currentIndexU]){
        if (startIndex == -1) startIndex = currentIndex;
        currentIndexU++;

    }
    currentIndex++;
}
    if (solLen != n+1)
       g<<solLen;
       else g<<"-1";
    return 0;
}