#include <algorithm> #include <iostream> #include <map> #include <string> #include <vector> using namespace std; int Check(const vector<int>& data, int start, int desired) { map<int, int> mapka; for(int i=start;i<desired;++i) { mapka[data[i]] += 1; if (mapka[data[i]] > 1) return i; } return desired; } void solve_ora(int all, int desired) { vector<int> data; map<int, int> mapka; int el; for(int i=0;i<all;++i) { cin >> el; mapka[el] += 1; data.push_back(el); } if (mapka.size()<desired) cout << "-1"; else { int actBest = 0; int steps = 0; int start = 1; int tmp; while((actBest=Check(data,0,desired))!=desired) { int x = actBest; while (data[x] == data[x+ 1]) x++; tmp = data[x]; data[x] = data[x+ 1]; data[x + 1] = tmp; ++steps; } cout << steps; } } int main() { int n, m; cin >> n; cin >> m; solve_ora(n,m); return 0; }
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 | #include <algorithm> #include <iostream> #include <map> #include <string> #include <vector> using namespace std; int Check(const vector<int>& data, int start, int desired) { map<int, int> mapka; for(int i=start;i<desired;++i) { mapka[data[i]] += 1; if (mapka[data[i]] > 1) return i; } return desired; } void solve_ora(int all, int desired) { vector<int> data; map<int, int> mapka; int el; for(int i=0;i<all;++i) { cin >> el; mapka[el] += 1; data.push_back(el); } if (mapka.size()<desired) cout << "-1"; else { int actBest = 0; int steps = 0; int start = 1; int tmp; while((actBest=Check(data,0,desired))!=desired) { int x = actBest; while (data[x] == data[x+ 1]) x++; tmp = data[x]; data[x] = data[x+ 1]; data[x + 1] = tmp; ++steps; } cout << steps; } } int main() { int n, m; cin >> n; cin >> m; solve_ora(n,m); return 0; } |