#include <cstdio> using namespace std; class Tree { private: int n; int* p; int* key; int* left; int* right; public: int End; int Root; int height; Tree(int N) { n = N; left = new int[n]; right = new int[n]; key = new int[n]; p = new int[n]; Root = -1; End = 0; } ~Tree() { //delete[] left; //delete[] right; //delete[] key; //delete[] p; } int Insertion(int el) { int z = -1; //preparing end element if (End <= n) { key[End] = el; p[End] = -1; left[End] = -1; right[End] = -1; z = End; End++; } else { return 6; } int x = Root; int y = -1; int k = key[z]; int i = 0; while (x != -1) // look for father { y = x; if (k < key[y]) x = left[y]; else x = right[y]; i++; } if (i > height) height = i; p[z] = y; // y is father if (y == -1) Root = z; //if tree was empty else { if (k < key[y]) left[y] = z; else right[y] = z; } return 0; } bool Member(int x) { int i = Root; while (i != -1) { if (x < key[i]) i = left[i]; else if (x == key[i]) return true; else i = right[i]; } return false; } }; int main() { int n = -1; int k = -1; int d = 0; // desire position int ac = 0; // actual position bool first = true; int swaps = 0; scanf("%d %d", &n, &k); Tree t(n); Tree t2(n); int el = 0; for (int i = 0; i < n && t.End <= k; i++) { scanf("%d", &el); ac = t2.End; t2.Insertion(el); if (!t.Member(el)) { d = t.End; t.Insertion(el); swaps = swaps + (ac - d); } } if (t.End < k) printf("-1"); else printf("%d", swaps); 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 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 | #include <cstdio> using namespace std; class Tree { private: int n; int* p; int* key; int* left; int* right; public: int End; int Root; int height; Tree(int N) { n = N; left = new int[n]; right = new int[n]; key = new int[n]; p = new int[n]; Root = -1; End = 0; } ~Tree() { //delete[] left; //delete[] right; //delete[] key; //delete[] p; } int Insertion(int el) { int z = -1; //preparing end element if (End <= n) { key[End] = el; p[End] = -1; left[End] = -1; right[End] = -1; z = End; End++; } else { return 6; } int x = Root; int y = -1; int k = key[z]; int i = 0; while (x != -1) // look for father { y = x; if (k < key[y]) x = left[y]; else x = right[y]; i++; } if (i > height) height = i; p[z] = y; // y is father if (y == -1) Root = z; //if tree was empty else { if (k < key[y]) left[y] = z; else right[y] = z; } return 0; } bool Member(int x) { int i = Root; while (i != -1) { if (x < key[i]) i = left[i]; else if (x == key[i]) return true; else i = right[i]; } return false; } }; int main() { int n = -1; int k = -1; int d = 0; // desire position int ac = 0; // actual position bool first = true; int swaps = 0; scanf("%d %d", &n, &k); Tree t(n); Tree t2(n); int el = 0; for (int i = 0; i < n && t.End <= k; i++) { scanf("%d", &el); ac = t2.End; t2.Insertion(el); if (!t.Member(el)) { d = t.End; t.Insertion(el); swaps = swaps + (ac - d); } } if (t.End < k) printf("-1"); else printf("%d", swaps); return 0; } |