#include <iostream> #include <vector> #include <algorithm> using namespace std; long ileczasu(long n, long k, vector<long> a) { bool czyjest = false; vector<long> marki; vector<long> powt; vector<long> first; long iledok; for (long i = 0; i < n; i++) { for (long j = 0; j < marki.size(); j++) { if (a[i] == marki[j]) { powt.push_back(i); czyjest = true; break; } } if (!czyjest) { marki.push_back(a[i]); first.push_back(i); } if (marki.size() == k) { iledok = i; break; } czyjest = false; } if (marki.size() < k) return -1; if (iledok+1 == k) return 0; if (powt[0] == k-1) return iledok - k+1; long s = 0; for (long i = 0; i < first.size(); i++) if (first[i] > k - 1) s += first[i] - k+1; return s+1; //return iledok - powt.size()+2; } int main() { ios_base::sync_with_stdio(false); long n, k, ai; vector<long> a; cin >> n >> k; for (long i = 0; i < n; i++) { cin >> ai; a.push_back(ai); } cout << ileczasu(n, k, a); 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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 | #include <iostream> #include <vector> #include <algorithm> using namespace std; long ileczasu(long n, long k, vector<long> a) { bool czyjest = false; vector<long> marki; vector<long> powt; vector<long> first; long iledok; for (long i = 0; i < n; i++) { for (long j = 0; j < marki.size(); j++) { if (a[i] == marki[j]) { powt.push_back(i); czyjest = true; break; } } if (!czyjest) { marki.push_back(a[i]); first.push_back(i); } if (marki.size() == k) { iledok = i; break; } czyjest = false; } if (marki.size() < k) return -1; if (iledok+1 == k) return 0; if (powt[0] == k-1) return iledok - k+1; long s = 0; for (long i = 0; i < first.size(); i++) if (first[i] > k - 1) s += first[i] - k+1; return s+1; //return iledok - powt.size()+2; } int main() { ios_base::sync_with_stdio(false); long n, k, ai; vector<long> a; cin >> n >> k; for (long i = 0; i < n; i++) { cin >> ai; a.push_back(ai); } cout << ileczasu(n, k, a); return 0; } |