#include <iostream> #include <cstring> int main() { int k, n, tmp; std::cin >> n >> k; bool cw[n]; memset(cw, 0, sizeof(cw)); int sub = 0, num = 0; unsigned long long secs = 0; for(int i=0;i<n;i++) { std::cin >> tmp; if(!cw[tmp-1]) { cw[tmp-1] = true; secs += i - sub++; num++; if(num == k) break; } } if(num != k) std::cout << -1; else std::cout << secs; 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 | #include <iostream> #include <cstring> int main() { int k, n, tmp; std::cin >> n >> k; bool cw[n]; memset(cw, 0, sizeof(cw)); int sub = 0, num = 0; unsigned long long secs = 0; for(int i=0;i<n;i++) { std::cin >> tmp; if(!cw[tmp-1]) { cw[tmp-1] = true; secs += i - sub++; num++; if(num == k) break; } } if(num != k) std::cout << -1; else std::cout << secs; return 0; } |