#include <iostream> #include <algorithm> using namespace std; const int maxn = 500000; bool to[maxn], sukces = false; int n, k, marka; long long int w = 0; int main() { for (auto &b:to) { b = false; } cin >> n >> k; for (int i = 0, j = 0; i < n; i++) { cin >> marka; if (false == to[marka-1]){ to[marka-1] = true; w += i; w -= j; if (k == ++j) { sukces = true; break; } } } if (true == sukces)cout<<w; 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 32 33 34 | #include <iostream> #include <algorithm> using namespace std; const int maxn = 500000; bool to[maxn], sukces = false; int n, k, marka; long long int w = 0; int main() { for (auto &b:to) { b = false; } cin >> n >> k; for (int i = 0, j = 0; i < n; i++) { cin >> marka; if (false == to[marka-1]){ to[marka-1] = true; w += i; w -= j; if (k == ++j) { sukces = true; break; } } } if (true == sukces)cout<<w; else cout << "-1"; } |