vadimk772336

с ручкой более менее работает

Nov 1st, 2021
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.52 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <fstream>
  4. using namespace std;
  5.  
  6. struct heap_elements;
  7.  
  8. struct list_segments
  9. {
  10. int l;
  11. int r;
  12. int status;
  13. struct list_segments* next;
  14. struct list_segments* prev;
  15. int to_heap;
  16. int heapID;
  17. };
  18.  
  19. struct heap_elements
  20. {
  21. int l;
  22. int r;
  23. int pos;
  24. list_segments* to_list;
  25. };
  26.  
  27. bool operator<(const heap_elements& a, const heap_elements& b)
  28. {
  29.  
  30. if ((a.r - a.l < b.r - b.l))
  31. return true;
  32. if ((a.r - a.l == b.r - b.l) & (a.l > b.l))
  33. return true;
  34.  
  35. return false;
  36. }
  37.  
  38. class Heap
  39. {
  40. std::vector<struct heap_elements> h;
  41. std::vector<int> ID_to_HeapIdx;
  42. int heap_size;
  43.  
  44. public:
  45. Heap();
  46. void siftup(int ID, std::vector<int>& HeapIdx_to_ID);
  47. void siftdown(int ID, std::vector<int>& HeapIdx_to_ID);
  48. void add(int ID, std::vector<int>& HeapIdx_to_ID, int l, int r, list_segments* to_list);
  49. void delete_vertex(int ID, std::vector<int>& HeapIdx_to_ID);
  50. void out();
  51. bool isempty();
  52. heap_elements* get_head();
  53. };
  54.  
  55. Heap::Heap()
  56. {
  57. std::vector<struct heap_elements> h;
  58. heap_size = 0;
  59. std::vector<int> ID_to_HeapIdx;
  60. }
  61.  
  62. heap_elements* Heap::get_head()
  63. {
  64. return &h[0];
  65. }
  66.  
  67. bool Heap::isempty()
  68. {
  69. if (heap_size == 0)
  70. return true;
  71. return false;
  72. }
  73.  
  74. void Heap::siftup(int ID, std::vector<int>& HeapIdx_to_ID)
  75. {
  76. int curr, parent;
  77. int tmp;
  78. curr = heap_size - 1;
  79. parent = (curr - 1) / 2;
  80. while (parent >= 0 && curr > 0)
  81. {
  82. if (h[parent] < h[curr])
  83. {
  84. heap_elements buff = h[curr];
  85. h[curr] = h[parent];
  86. h[parent] = buff;
  87.  
  88.  
  89. tmp = HeapIdx_to_ID[parent];
  90. HeapIdx_to_ID[parent] = HeapIdx_to_ID[curr];
  91. HeapIdx_to_ID[curr] = tmp;
  92.  
  93.  
  94. tmp = ID_to_HeapIdx[HeapIdx_to_ID[parent]];
  95. ID_to_HeapIdx[HeapIdx_to_ID[parent]] = ID_to_HeapIdx[HeapIdx_to_ID[curr]];
  96. ID_to_HeapIdx[HeapIdx_to_ID[curr]] = tmp;
  97.  
  98. h[curr].pos = curr;
  99. h[parent].pos = parent;
  100.  
  101. h[curr].to_list->to_heap = curr;
  102. h[parent].to_list->to_heap = parent;
  103. }
  104. curr = parent;
  105. parent = (curr - 1) / 2;
  106. }
  107. }
  108.  
  109. void Heap::siftdown(int ID, std::vector<int>& HeapIdx_to_ID)
  110. {
  111.  
  112. cout << "IN" << endl;
  113. int parent, max_child;
  114. int tmp;
  115. int curr = ID_to_HeapIdx[ID];
  116. //ID_to_HeapIdx[ID] = -100;
  117.  
  118. cout << "curr = " << curr << endl;
  119.  
  120. cout << "HeapIdx_to_ID = ";
  121. for (int j=0;j<HeapIdx_to_ID.size(); j++)
  122. cout << HeapIdx_to_ID[j] << " " ;
  123. cout << endl;
  124.  
  125. //int curr = pos;
  126. int child_l = 2 * curr + 1;
  127. int child_r = 2 * curr + 2;
  128.  
  129. if (h[child_r] < h[child_l])
  130. max_child = child_l;
  131. else
  132. max_child = child_r;
  133.  
  134. while (child_l < heap_size)
  135. {
  136. if (child_l == heap_size - 1)
  137. max_child = child_l;
  138. else if (h[child_r] < h[child_l])
  139. max_child = child_l;
  140. else
  141. max_child = child_r;
  142.  
  143. if (h[curr] < h[max_child])
  144. {
  145. heap_elements buff = h[curr];
  146. h[curr] = h[max_child];
  147. h[max_child] = buff;
  148.  
  149.  
  150. tmp = HeapIdx_to_ID[max_child];
  151. HeapIdx_to_ID[max_child] = HeapIdx_to_ID[curr];
  152. HeapIdx_to_ID[curr] = tmp;
  153.  
  154. tmp = ID_to_HeapIdx[HeapIdx_to_ID[max_child]];
  155. ID_to_HeapIdx[HeapIdx_to_ID[max_child]] = ID_to_HeapIdx[HeapIdx_to_ID[curr]];
  156. ID_to_HeapIdx[HeapIdx_to_ID[curr]] = tmp;
  157.  
  158. h[curr].pos = curr;
  159. h[max_child].pos = max_child;
  160.  
  161. h[curr].to_list->to_heap = curr;
  162. h[max_child].to_list->to_heap = max_child;
  163. }
  164.  
  165. curr = max_child;
  166. child_l = 2 * curr + 1;
  167. child_r = 2 * curr + 2;
  168. }
  169. }
  170.  
  171. void Heap::add(int ID, std::vector<int>& HeapIdx_to_ID, int l, int r, list_segments* to_list)
  172. {
  173.  
  174. heap_elements vertex;
  175. vertex.l = l;
  176. vertex.r = r;
  177. vertex.to_list = to_list;
  178. h.push_back(vertex);
  179. h[heap_size].pos = heap_size;
  180. if (h[heap_size].to_list != NULL)
  181. h[heap_size].to_list->to_heap = heap_size;
  182.  
  183. ID_to_HeapIdx.push_back(heap_size);
  184. HeapIdx_to_ID.push_back(ID);
  185.  
  186. heap_size++;
  187. siftup(ID, HeapIdx_to_ID);
  188. }
  189.  
  190. void Heap::delete_vertex(int ID, std::vector<int>& HeapIdx_to_ID)
  191. {
  192.  
  193. int pos = ID_to_HeapIdx[ID];
  194. h[pos] = h[heap_size - 1];
  195. h.pop_back();
  196. heap_size--;
  197.  
  198. int id_last_el = HeapIdx_to_ID[heap_size];
  199. HeapIdx_to_ID[pos] = id_last_el;
  200. //HeapIdx_to_ID[heap_size] = -100;
  201. ID_to_HeapIdx[id_last_el] = pos;
  202.  
  203. siftdown(ID, HeapIdx_to_ID);
  204. }
  205.  
  206. void Heap::out(void)
  207. {
  208.  
  209. for (int i = 0; i < heap_size; i++)
  210. {
  211. cout << "(" << h[i].l << "," << h[i].r << ") ";
  212. }
  213. cout << endl;
  214. }
  215.  
  216. class LIST_SEGMENTS
  217. {
  218. struct list_segments* Head;
  219. int count;
  220.  
  221. public:
  222. LIST_SEGMENTS();
  223. list_segments* get_head();
  224. void add(int, int, int, int, list_segments*, int heapID);
  225. void delete_segment(list_segments* segment);
  226. void add_head(int, int, int, int, int heapID);
  227. void Print();
  228. };
  229.  
  230. LIST_SEGMENTS::LIST_SEGMENTS()
  231. {
  232. Head = NULL;
  233. count = 0;
  234. }
  235.  
  236. void LIST_SEGMENTS::add_head(int l, int r, int status, int to_heap, int heapID)
  237. {
  238. list_segments* buff = new list_segments;
  239. buff->prev = 0;
  240. buff->l = l;
  241. buff->r = r;
  242. buff->status = status;
  243. buff->next = Head;
  244. buff->heapID = heapID;
  245.  
  246. if (Head != NULL)
  247. Head->prev = buff;
  248.  
  249. Head = buff;
  250. count++;
  251. }
  252.  
  253. void LIST_SEGMENTS::add(int l, int r, int status, int to_heap, list_segments* segment, int heapID)
  254. {
  255. list_segments* after_segment = new list_segments;
  256. after_segment->l = l;
  257. after_segment->r = r;
  258. after_segment->status = status;
  259. after_segment->next = segment->next;
  260. after_segment->prev = segment;
  261. after_segment->to_heap = to_heap;
  262. after_segment->heapID = heapID;
  263.  
  264. if (segment->next != NULL)
  265. segment->next->prev = after_segment;
  266.  
  267. segment->next = after_segment;
  268. count++;
  269. }
  270.  
  271. void LIST_SEGMENTS::delete_segment(list_segments* segment)
  272. {
  273. if (segment->next != NULL)
  274. segment->next->prev = segment->prev;
  275. if (segment->prev != NULL)
  276. segment->prev->next = segment->next;
  277. else
  278. Head = segment->next;
  279. delete segment;
  280. }
  281.  
  282. list_segments* LIST_SEGMENTS::get_head()
  283. {
  284. return Head;
  285. }
  286.  
  287. void LIST_SEGMENTS::Print()
  288. {
  289.  
  290.  
  291. list_segments* buff = Head;
  292. while (buff->next != NULL)
  293. {
  294. cout << "(" << buff->l << ";" << buff->status << ";" << buff->r << ")"<< ";";
  295. buff = buff->next;
  296. }
  297.  
  298. cout << "(" << buff->l << ";" << buff->status << ";" << buff->r << ")"<< ";\n";
  299. }
  300.  
  301.  
  302. void find_memory(Heap* heap, LIST_SEGMENTS* List, int K, int query_number,
  303. std::vector<int>& answers, list_segments** requests, std::vector<int>& HeapIdx_to_ID, int &ID)
  304. {
  305. int old_l;
  306. heap_elements* heap_head;
  307.  
  308. cout << "1" << endl;
  309. heap_head = heap->get_head();
  310. if (!heap->isempty() & (heap_head->r - heap_head->l + 1) >= K)
  311. {
  312. cout << "2" << endl;
  313. int top_heap_id = HeapIdx_to_ID[0];
  314. if ((heap_head->r - heap_head->l + 1) == K)
  315. {
  316. heap_head->to_list->status = 0;
  317. requests[query_number] = heap_head->to_list;
  318. answers.push_back(heap_head->l);
  319.  
  320. heap->delete_vertex(top_heap_id, HeapIdx_to_ID);
  321. }
  322. else
  323. {
  324. cout << "3" << endl;
  325. old_l = heap_head->l;
  326. heap_head->l = old_l + K;
  327. heap_head->to_list->l = old_l + K;
  328. int none_id = -5;
  329. if (heap_head->to_list->prev != NULL)
  330. {
  331. cout << "4" << endl;
  332. List->add(old_l, old_l + K - 1, 0, -1, heap_head->to_list->prev, none_id);
  333. requests[query_number] = heap_head->to_list->prev;
  334. cout << "4.1" << endl;
  335. }
  336. else
  337. {
  338. cout << "5" << endl;
  339. List->add_head(old_l, old_l + K - 1, 0, -1,none_id);
  340. requests[query_number] = List->get_head();
  341. }
  342. cout << "6.1" << endl;
  343. cout << "top_heap_id, HeapIdx_to_ID" << endl;
  344. cout << top_heap_id << endl;
  345. for (int j=0;j<HeapIdx_to_ID.size(); j++)
  346. cout << HeapIdx_to_ID[j] << " " ;
  347. cout << endl;
  348. heap->siftdown(top_heap_id, HeapIdx_to_ID);
  349. cout << "6.2" << endl;
  350. answers.push_back(old_l);
  351. cout << "6" << endl;
  352. }
  353. }
  354. else
  355. {
  356. cout << "7" << endl;
  357. requests[query_number] = NULL;
  358. answers.push_back(-1);
  359. }
  360. }
  361.  
  362. void free_up_memory(Heap* heap, LIST_SEGMENTS* List, int q, int query_number, //Сделать обновление кар АЙДИ при добавлении
  363. std::vector<int>& answers, list_segments** requests, std::vector<int>& HeapIdx_to_ID, int &ID)
  364. {
  365.  
  366. if (requests[q] != NULL)
  367. {
  368.  
  369. requests[q]->status = 1;
  370.  
  371. if (requests[q]->prev != NULL)
  372. {
  373. if (requests[q]->prev->status == 1 & requests[q]->prev->r == requests[q]->l - 1)
  374. {
  375.  
  376. requests[q]->l = requests[q]->prev->l;
  377. heap->delete_vertex(requests[q]->prev->heapID, HeapIdx_to_ID);
  378. List->delete_segment(requests[q]->prev);
  379.  
  380. }
  381. }
  382. if (requests[q]->next != NULL)
  383. {
  384.  
  385. if (requests[q]->next->status == 1 & requests[q]->next->l - 1 == requests[q]->r)
  386. {
  387. requests[q]->r = requests[q]->next->r;
  388. heap->delete_vertex(requests[q]->next->heapID, HeapIdx_to_ID);
  389. List->delete_segment(requests[q]->next);
  390.  
  391. }
  392. }
  393.  
  394. ID++;
  395. requests[q]->heapID = ID;
  396. heap->add(ID, HeapIdx_to_ID, requests[q]->l, requests[q]->r, requests[q]);
  397. requests[query_number] = requests[q];
  398. requests[q] = NULL;
  399.  
  400. }
  401. else
  402. {
  403. requests[query_number] = NULL;
  404. }
  405. }
  406.  
  407.  
  408.  
  409.  
  410. int main()
  411. {
  412.  
  413. /*
  414. -9
  415. 6 8 2 3 -1 3 3 -5 2 2 //1 3 -1 -1 1 -1
  416. 1 7 2 2 1 1 -4 -1 -3 //-1 -1 1 -1 yes
  417. 6 9 2 2 2 -1 -3 -2 2 2 2 // 1 3 5 1 3 5
  418. 6 2 3 -1 //1
  419. 6 7 6 6-2-1 2 4 -5
  420. 10 14 8 2 -2 5 -4 -4 -1 4 4 2 -8 -10 -9 10 //1 9 -1 1 5 9 1
  421. 1 1 1 // 1
  422. 1 2 1 2 //1 -1
  423. 1 9 2 -1 1 1 -1 2 1 -3 1 //-1 1 -1 -1 -1 1 yes
  424. 8 10 2 2 -1 1 3 -2 3 -4 3 -9
  425.  
  426. */
  427. int N, M, K, q, old_l;
  428.  
  429. int count_tests;
  430. ifstream ifs("tests.txt");
  431. ifs >> count_tests;
  432. ofstream out("results.txt");
  433.  
  434.  
  435. for (int i = 0; i < count_tests; ++i)
  436. {
  437. ifs >> N;
  438. ifs >> M;
  439. Heap heap;
  440. LIST_SEGMENTS List;
  441. heap_elements* heap_head;
  442. std::vector<int> answers;
  443. int ID = -1;
  444. std::vector<int> HeapIdx_to_ID;
  445.  
  446. ID++;
  447. List.add_head(1, N, 1, 0, ID);
  448. heap.add(ID, HeapIdx_to_ID, 1, N, List.get_head());
  449. //HeapIdx_to_ID.push_back(ID);
  450.  
  451. list_segments* requests[M];
  452. for (int query_number = 0; query_number < M; ++query_number)
  453. {
  454. ifs >> K;
  455. cout << "\n ЗАПРОС НОМЕР " << query_number+1 << " Enter K: " << K << endl;
  456. if (K > 0)
  457. {
  458. find_memory(&heap, &List, K, query_number, answers, requests, HeapIdx_to_ID, ID);
  459. }
  460. else
  461. {
  462. q = -K - 1;
  463. free_up_memory(&heap, &List, q, query_number, answers, requests, HeapIdx_to_ID, ID);
  464. }
  465.  
  466.  
  467. cout << "Куча после запроса: ";
  468. heap.out();
  469. cout << "List после запроса: ";
  470. List.Print();
  471. cout << "requests после запроса " << query_number << ": ";
  472.  
  473.  
  474. cout << "\n---------------\n";
  475.  
  476.  
  477.  
  478. }
  479.  
  480. for (int i = 0; i < answers.size(); ++i)
  481. {
  482. out << answers[i] << ' ';
  483. cout << answers[i] << " ";
  484. }
  485.  
  486. cout << endl;
  487. out << '\n';
  488.  
  489. }
  490. return 0;
  491. }
  492.  
Advertisement
Add Comment
Please, Sign In to add comment