#include <iostream> #include <set> using namespace std; int main() { int n, k, setBottles = 0, bottleBrand; set<int> setBottlesBrands; long long result = 0; cin >> n >> k; for (int position = 0; position < n; position++) { cin >> bottleBrand; if (setBottles < k && setBottlesBrands.find(bottleBrand) == setBottlesBrands.end()) { result += position - setBottles; setBottles++; setBottlesBrands.insert(bottleBrand); } } if (setBottles < k) cout << -1; else cout << result; 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 | #include <iostream> #include <set> using namespace std; int main() { int n, k, setBottles = 0, bottleBrand; set<int> setBottlesBrands; long long result = 0; cin >> n >> k; for (int position = 0; position < n; position++) { cin >> bottleBrand; if (setBottles < k && setBottlesBrands.find(bottleBrand) == setBottlesBrands.end()) { result += position - setBottles; setBottles++; setBottlesBrands.insert(bottleBrand); } } if (setBottles < k) cout << -1; else cout << result; return 0; } |