#include <cstdio> #define MAX_N 500001 int first_a[MAX_N]; int kolejnosc[MAX_N]; int main() { int n, k; scanf("%d %d", &n, &k); int ai; int x = 1; for (int i = 1; i <= n; i++) { scanf("%d", &ai); if (first_a[ai] == 0) { first_a[ai] = i; kolejnosc[x++] = ai; } } /* for (int i = 1; i <= 3; i++) { printf("%d ", kolejnosc[i]); } printf("\n"); for (int i = 1; i <= 3; i++) { printf("%d ", first_a[i]); } */ long long int result = 0L; if (kolejnosc[k] == 0) { result = -1; } else { for (int i = 1; i <= k; i++) { result += first_a[kolejnosc[i]] - i; } } printf("%lld", 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 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | #include <cstdio> #define MAX_N 500001 int first_a[MAX_N]; int kolejnosc[MAX_N]; int main() { int n, k; scanf("%d %d", &n, &k); int ai; int x = 1; for (int i = 1; i <= n; i++) { scanf("%d", &ai); if (first_a[ai] == 0) { first_a[ai] = i; kolejnosc[x++] = ai; } } /* for (int i = 1; i <= 3; i++) { printf("%d ", kolejnosc[i]); } printf("\n"); for (int i = 1; i <= 3; i++) { printf("%d ", first_a[i]); } */ long long int result = 0L; if (kolejnosc[k] == 0) { result = -1; } else { for (int i = 1; i <= k; i++) { result += first_a[kolejnosc[i]] - i; } } printf("%lld", result); return 0; } |