#include <iostream> #include <set> #include <vector> using namespace std; //const int rozmiar = 5e5 + 7; //int tab[rozmiar]; set <int> uzyte; vector <long long> pozycja; long long w = 0; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n, k; int x; pozycja.push_back(0); cin >> n >> k; for (int i = 1; i <= n; i++) { cin >> x; if (uzyte.find(x) == uzyte.end()) { pozycja.push_back(i); uzyte.insert(x); } } /* for (auto iter = pozycja.begin(); iter != pozycja.end(); iter++) cout << (*iter) << " ";*/ if (pozycja.size() < (k+1)) { cout << -1 << "\n"; return 0; } for (int akt = 1; akt <= k; akt++) { w += pozycja[akt] - akt; } cout << w << "\n"; 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 | #include <iostream> #include <set> #include <vector> using namespace std; //const int rozmiar = 5e5 + 7; //int tab[rozmiar]; set <int> uzyte; vector <long long> pozycja; long long w = 0; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n, k; int x; pozycja.push_back(0); cin >> n >> k; for (int i = 1; i <= n; i++) { cin >> x; if (uzyte.find(x) == uzyte.end()) { pozycja.push_back(i); uzyte.insert(x); } } /* for (auto iter = pozycja.begin(); iter != pozycja.end(); iter++) cout << (*iter) << " ";*/ if (pozycja.size() < (k+1)) { cout << -1 << "\n"; return 0; } for (int akt = 1; akt <= k; akt++) { w += pozycja[akt] - akt; } cout << w << "\n"; return 0; } |