Advertisement
Guest User

Untitled

a guest
May 4th, 2015
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.71 KB | None | 0 0
  1. #include<iostream>
  2. #include<vector>
  3. #include<set>
  4.  
  5. using std::cin;
  6. using std::cout;
  7. using std::vector;
  8.  
  9. struct Staff {
  10.     int time;
  11.     vector<int> friends;
  12.     void Print() const {
  13.         cout << " Time: " << time << "  ";
  14.         cout << "Friends: ";
  15.         for (auto i : friends) cout << i << " ";
  16.         cout << "\n";
  17.     }
  18.     bool operator < (Staff a) const {
  19.         return this->time < a.time;
  20.     }
  21.  
  22.     bool operator >(Staff a) const {
  23.         return this->time > a.time;
  24.     }
  25. };
  26.  
  27. struct HeapStaff {
  28.     int name;
  29.     int time;
  30.  
  31.     HeapStaff(int n, int t): name(n), time(t) {}
  32.  
  33.     HeapStaff() : name(0), time(0) {}
  34.  
  35.     void Print() {
  36.         cout << "name: " << name << " time: " << time;
  37.     }
  38. };
  39.  
  40. class Heap {
  41. private:
  42.     vector<HeapStaff> values;
  43.     int GetLeftChild(int index) const {
  44.         return (2 * index + 1) <= values.size() - 1 ? (2 * index + 1) : -1;
  45.     }
  46.     int GetRightChild(int index) const {
  47.         return (2 * index + 2) <= values.size() - 1 ? (2 * index + 2) : -1;
  48.     }
  49.     int GetParent(int index) const {
  50.         return index != 0 ? (index + 1) / 2 - 1 : -1;
  51.     }
  52.     void swap(int index1, int index2) {
  53.         HeapStaff a = values[index1];
  54.         values[index1] = values[index2];
  55.         values[index2] = a;
  56.     }
  57.     void Heapify(int index) {
  58.         if (index >= values.size()) return;
  59.         int min_index = index;
  60.         if (GetLeftChild(index) != -1)
  61.             if (values[min_index].time > values[GetLeftChild(index)].time) {
  62.                 min_index = GetLeftChild(index);
  63.             }
  64.         if (GetRightChild(index) != -1) {
  65.             if (values[min_index].time > values[GetRightChild(index)].time) {
  66.                 min_index = GetRightChild(index);
  67.             }
  68.         }
  69.         if (min_index == index) return;
  70.         else {
  71.             swap(index, min_index);
  72.             Heapify(min_index);
  73.         }
  74.     }
  75.     void HeapUp(int index) {
  76.         if (index == 0) return;
  77.         int parent = GetParent(index);
  78.         while (index > 0 && values[index].time < values[parent].time) {
  79.             swap(index, parent);
  80.             index = parent;
  81.             parent = GetParent(index);
  82.         }
  83.         return;
  84.     }
  85.  
  86.  
  87. public:
  88.     void BuildHeap(const vector<HeapStaff>& val) {
  89.         this->values.resize(val.size());
  90.         std::copy(val.begin(), val.end(), this->values.begin());
  91.         for (int i = (val.size() / 2); i >= 0; --i) {
  92.             Heapify(i);
  93.         }
  94.     }
  95.     HeapStaff GetMin() const {
  96.         return values[0];
  97.     }
  98.     void ExtractMin() {
  99.         swap(0, values.size() - 1);
  100.         values.erase(--values.end());
  101.         Heapify(0);
  102.     }
  103.  
  104.     void Add(HeapStaff elem) {
  105.         values.push_back(elem);
  106.         HeapUp(values.size() - 1);
  107.  
  108.     }
  109.  
  110.     bool Empty() {
  111.         return values.size() == 0;
  112.     }
  113.  
  114.     void PrintHeap() {
  115.         for (int i = 0; i < values.size(); ++i) {
  116.             cout << "#elem = " << i << "\n";
  117.             values[i].Print();
  118.         }
  119.     }
  120. };
  121.  
  122. vector<Staff> Input() {
  123.     int num_staff;
  124.     cin >> num_staff;
  125.     vector<Staff> personal(num_staff);
  126.     for (int i = 0; i < num_staff; ++i) {
  127.         cin >> personal[i].time;
  128.         int num_friends;
  129.         cin >> num_friends;
  130.         personal[i].friends.resize(num_friends);
  131.         for (int j = 0; j < num_friends; ++j) {
  132.             cin >> personal[i].friends[j];
  133.             personal[i].friends[j]--;
  134.         }
  135.     }
  136.     return personal;
  137. }
  138.  
  139. int MinTime(const vector<Staff>& personal) {
  140.     Heap current_staff;
  141.     vector<bool> staff_in_heap(personal.size(), false);
  142.     for (int i = 0; i < personal.size() && personal[i].time == 0; ++i) {
  143.         HeapStaff staff(i, personal[i].time);
  144.         current_staff.Add(staff);
  145.         staff_in_heap[i] = true;
  146.     }
  147.     int min_time = 0;
  148.     HeapStaff staff(-1, -1);
  149.     int num_staff_know = 0;
  150.     while (!current_staff.Empty() && num_staff_know < (personal.size() + 1) / 2) {
  151.         staff = current_staff.GetMin();
  152.         current_staff.ExtractMin();
  153.         ++num_staff_know;
  154.         if (min_time < staff.time) min_time = staff.time;
  155.        
  156.         for (int i = 0; i < personal[staff.name].friends.size(); ++i) {
  157.             if (!staff_in_heap[personal[staff.name].friends[i]]) {
  158.                 HeapStaff s(personal[staff.name].friends[i], personal[personal[staff.name].friends[i]].time);
  159.                 current_staff.Add(s);
  160.                 staff_in_heap[personal[staff.name].friends[i]] = true;
  161.             }
  162.         }
  163.     }
  164.     if (num_staff_know >= (personal.size() + 1) / 2) return min_time;
  165.     else return -1;
  166. }
  167.  
  168. int main() {
  169.     vector<Staff> personal = Input();
  170.     cout << MinTime(personal);
  171.     return 0;
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement