Advertisement
wurdalak007

Soldiers

Nov 21st, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.73 KB | None | 0 0
  1. /*В одной военной части решили построить в одну шеренгу по росту. Т.к. часть была далеко не образцовая, то солдаты часто приходили не вовремя, а то их и вовсе приходилось выгонять из шеренги за плохо начищенные сапоги. Однако солдаты в процессе прихода и ухода должны были всегда быть выстроены по росту – сначала самые высокие, а в конце – самые низкие. За расстановку солдат отвечал прапорщик, который заметил интересную особенность – все солдаты в части разного роста. Ваша задача состоит в том, чтобы помочь прапорщику правильно расставлять солдат, а именно для каждого приходящего солдата указывать, перед каким солдатом в строе он должен становится. Требуемая скорость выполнения команды - O(log n).*/
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. class AVLTree{
  7. public:
  8. AVLTree( unsigned int& key );
  9. AVLTree();
  10. ~AVLTree();
  11.  
  12. struct Tree {
  13. unsigned int position;
  14. unsigned int key;
  15. unsigned char depth;
  16. Tree* left;
  17. Tree* right;
  18.  
  19. Tree():
  20. position(0),
  21. key(0),
  22. depth(0),
  23. left(nullptr),
  24. right(nullptr)
  25. {}
  26.  
  27. Tree( unsigned int& key ):
  28. position(0),
  29. key( key ),
  30. depth( 1 ),
  31. left( nullptr ),
  32. right( nullptr )
  33. {}
  34.  
  35. };
  36.  
  37. unsigned char safe_depth( Tree* root );
  38. int imbalance( Tree* node );
  39. void Insert_height( unsigned int& key );
  40. void Remove_pos( unsigned int& pos );
  41. void update_depth( Tree*& node );
  42. void RotateLeft( Tree*& root );
  43. void RotateRight( Tree*& root );
  44. void balance( Tree*& node );
  45. void update_position( Tree*& root );
  46. void update_remove_position( Tree*& root );
  47. void deletetree( Tree*& root );
  48. void insert( Tree*& node, unsigned int& key, unsigned int& pos, unsigned int min );
  49. void remove( Tree*& root, unsigned int pos );
  50. Tree* findmin( Tree* root );
  51. Tree* removemin( Tree*& root );
  52.  
  53. private:
  54. Tree* root = nullptr;
  55. };
  56.  
  57. AVLTree::AVLTree( unsigned int& key ) {
  58. root = new Tree( key );
  59. }
  60.  
  61. AVLTree::~AVLTree(){
  62. deletetree(root);
  63. }
  64.  
  65. void AVLTree::Insert_height( unsigned int& key ){
  66. unsigned int tmp_position = 0;
  67. insert(root, key, tmp_position, 0);
  68. cout << tmp_position << " ";
  69. }
  70.  
  71. void AVLTree::Remove_pos( unsigned int& pos ) {
  72. remove(root, pos);
  73. }
  74.  
  75. unsigned char AVLTree::safe_depth( Tree* root ) {
  76. if( root == nullptr ) {
  77. return 0;
  78. } else {
  79. return root->depth;
  80. }
  81. }
  82.  
  83. void AVLTree::update_depth( Tree*& root ) {
  84. unsigned char left_depth = safe_depth(root->left);
  85. unsigned char right_depth = safe_depth(root->right);
  86.  
  87. if( left_depth > right_depth ) {
  88. root->depth = left_depth + 1;
  89. } else {
  90. root->depth = right_depth + 1;
  91. }
  92. }
  93.  
  94. int AVLTree::imbalance(Tree* node) {
  95. return(safe_depth(node->right) - safe_depth(node->left));
  96. }
  97.  
  98. void AVLTree::balance( Tree*& node ) {
  99. update_depth(node);
  100. if( imbalance(node) == 2 ) {
  101. if( imbalance(node->right) < 0 ) {
  102. RotateRight(node->right);
  103. }
  104. RotateLeft(node);
  105. }
  106. if( imbalance(node) == -2 ) {
  107. if( imbalance(node->left) > 0 ) {
  108. RotateLeft(node->left);
  109. }
  110. RotateRight(node);
  111. }
  112. return;
  113. }
  114.  
  115. void AVLTree::remove( Tree*& node, unsigned int pos) {
  116. if( node == nullptr ) {
  117. return;
  118. }
  119.  
  120. if( pos > node->position ) {
  121. remove(node->left, pos-node->position - 1);
  122. } else if( pos < node->position ) {
  123. node->position--;
  124. remove(node->right, pos);
  125. } else {
  126. Tree* left = node->left;
  127. Tree* right = node->right;
  128. unsigned int tmp = node->position;
  129. delete node;
  130.  
  131. if( right == nullptr ) {
  132. node = left;
  133. return;
  134. }
  135. Tree* min = findmin(right);
  136. min->right = removemin(right);
  137. min->position = tmp - 1;
  138. min->left = left;
  139. balance(min);
  140. node = min;
  141. }
  142. balance(node);
  143. return;
  144. }
  145.  
  146. void AVLTree::RotateLeft( Tree*& root ) {
  147. Tree* tmp = root->right;
  148. root->right= tmp->left;
  149. root->position = root->position - tmp->position - 1;
  150. tmp->left = root;
  151.  
  152. update_depth(root);
  153. update_depth(tmp);
  154. root = tmp;
  155. }
  156.  
  157. void AVLTree::RotateRight( Tree*& root) {
  158. Tree* tmp = root->left;
  159. root->left = tmp->right;
  160. tmp->right = root;
  161. if( tmp->right == nullptr ) {
  162. tmp->position = 0;
  163. } else {
  164. tmp->position += tmp->right->position + 1;
  165. }
  166. update_depth(root);
  167. update_depth(tmp);
  168. root = tmp;
  169. }
  170.  
  171. void AVLTree::insert( Tree*& node, unsigned int& key, unsigned int& pos, unsigned int min ) {
  172. if( node == nullptr ) {
  173. pos = min;
  174. node = new Tree( key );
  175. return;
  176. }
  177.  
  178. if( key >= node->key ) {
  179. node->position++;
  180. insert(node->right, key, pos, min);
  181. } else {
  182. insert(node->left, key, pos, min+node->position + 1);
  183. }
  184.  
  185. balance(node);
  186. return;
  187. }
  188.  
  189. AVLTree::Tree* AVLTree::findmin( Tree* root ) {
  190. if( root->left != nullptr ){
  191. return findmin(root->left);
  192. }
  193. return root;
  194.  
  195. }
  196.  
  197. AVLTree::Tree* AVLTree::removemin( Tree*& root ) {
  198.  
  199. if( root->left == nullptr ) {
  200. return root->right;
  201. }
  202.  
  203. root->left = removemin( root->left );
  204. balance( root );
  205. return root;
  206.  
  207. }
  208.  
  209. void AVLTree::deletetree( Tree*& root) {
  210. if( root == nullptr ) {
  211. return;
  212. }
  213. deletetree(root->left);
  214. deletetree(root->right);
  215. delete root;
  216. }
  217.  
  218. int main() {
  219. unsigned int n = 0;
  220. unsigned int height_or_pos = 0;
  221. int com = 0;
  222.  
  223. cin >> n;
  224. cin >> com >> height_or_pos;
  225. AVLTree tree(height_or_pos);
  226. cout<<"0 ";
  227.  
  228. for( int i = 1; i < n; i++ ) {
  229.  
  230. cin >> com;
  231. cin >> height_or_pos;
  232.  
  233. if( com == 2 ) {
  234. tree.Remove_pos(height_or_pos);
  235. } else {
  236. tree.Insert_height(height_or_pos);
  237. }
  238.  
  239. }
  240.  
  241. return 0;
  242. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement