Pagini recente » Cod sursa (job #818530) | Cod sursa (job #822246) | Cod sursa (job #1807406) | Cod sursa (job #2250734) | Cod sursa (job #2592112)
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;
}