1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <iostream>
using namespace std;

int main(){

    int n,k; cin >> n >> k ;
    int tabNumb[n];int brands[n];
    int brandsC = 0 ;

    for(int i = 0; i != n ;i++){
        cin >> tabNumb[i];
        bool same = false;
        for(int a = 0; a != i ; a++){
            if(tabNumb[i] == tabNumb[a]){
                same = true ;
            }
        }
        if (same == false) {
            brandsC ++ ;
            brands[brandsC-1] = tabNumb[i];
        }
    }
    if(k > brandsC || n < k ){
        cout << "-1" << endl;
        return(0);
    }
    int saved[k]; int savedC = 1; int operation = 0;
    saved[0] = tabNumb[0];
    for(int a = 0 ; a != k-1; a++){
        while(true){
            bool isSaved = false;
            for(int b = 0 ; b != savedC ;b++){
                if(tabNumb[a+1] == saved[b]){
                    isSaved = true;
                }
            }

            if(isSaved == true){
                int z = a+1;
                bool nextSaved = true;

                while(nextSaved == true){
                    for(int b = 0 ; b != savedC ;b++){
                        if(tabNumb[z+1] == saved[b] ){
                            z++;
                        }else {
                            nextSaved = false;
                        }
                    }
                }

                int temp = tabNumb[z];
                tabNumb[z] = tabNumb[z+1];
                tabNumb[z+1] = temp;
                operation++;
                } else {
                savedC++;
                saved[savedC]= tabNumb[a+1];
                break;
            }
        }
    }
    cout << operation <<endl  ;
}