#include <iostream> #include <vector> using namespace std; int main() { int n=3, k=2; cin >> n >> k; vector<int>a; for (int i = 0; i < n; i++) { int m; cin >> m; a.push_back(m); } vector<int>byly; int t = 0; byly.push_back(a[0]); for (int i = 1, x = 1; x < k && i < n; i++) { int j = 0; while (j < byly.size() && a[i] != byly[j]) j++; if (j < byly.size()) continue; byly.push_back(a[i]); t += i-x; x++; } if (byly.size() == k) cout << t; else cout << -1; }
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 | #include <iostream> #include <vector> using namespace std; int main() { int n=3, k=2; cin >> n >> k; vector<int>a; for (int i = 0; i < n; i++) { int m; cin >> m; a.push_back(m); } vector<int>byly; int t = 0; byly.push_back(a[0]); for (int i = 1, x = 1; x < k && i < n; i++) { int j = 0; while (j < byly.size() && a[i] != byly[j]) j++; if (j < byly.size()) continue; byly.push_back(a[i]); t += i-x; x++; } if (byly.size() == k) cout << t; else cout << -1; } |