Advertisement
kartikkukreja

Interval Partitioning

Aug 25th, 2013
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.54 KB | None | 0 0
  1. #include <cstdio>
  2. #include <algorithm>
  3. using namespace std;
  4.  
  5. /*
  6.  * Indexed min priority queue
  7.  * Supports insertion in O(log N), deletion of any key (regardless of whether
  8.  * the key is the minimum key or not) in O(log N) and changes to key values
  9.  * in O(log N), where N is the number of elements currently in the PQ
  10.  */
  11. class MinIndexedPQ {
  12.     int NMAX, N, *heap, *index, *keys;
  13.  
  14.     void swap(int i, int j) {
  15.         int t = heap[i]; heap[i] = heap[j]; heap[j] = t;
  16.         index[heap[i]] = i; index[heap[j]] = j;
  17.     }
  18.  
  19.     void bubbleUp(int k)    {
  20.         while(k > 1 && keys[heap[k/2]] > keys[heap[k]])   {
  21.             swap(k, k/2);
  22.             k = k/2;
  23.         }
  24.     }
  25.  
  26.     void bubbleDown(int k)  {
  27.         int j;
  28.         while(2*k <= N) {
  29.             j = 2*k;
  30.             if(j < N && keys[heap[j]] > keys[heap[j+1]])
  31.                 j++;
  32.             if(keys[heap[k]] <= keys[heap[j]])
  33.                 break;
  34.             swap(k, j);
  35.             k = j;
  36.         }
  37.     }
  38.  
  39. public:
  40.     // Create an empty MinIndexedPQ which can contain atmost NMAX elements
  41.     MinIndexedPQ(int NMAX)  {
  42.         this->NMAX = NMAX;
  43.         N = 0;
  44.         keys = new int[NMAX + 1];
  45.         heap = new int[NMAX + 1];
  46.         index = new int[NMAX + 1];
  47.         for(int i = 0; i <= NMAX; i++)
  48.             index[i] = -1;
  49.     }
  50.  
  51.     ~MinIndexedPQ() {
  52.         delete [] keys;
  53.         delete [] heap;
  54.         delete [] index;
  55.     }
  56.  
  57.     // check if the PQ is empty
  58.     bool isEmpty()  {
  59.         return N == 0;
  60.     }
  61.  
  62.     // check if i is an index on the PQ
  63.     bool contains(int i)    {
  64.         return index[i] != -1;
  65.     }
  66.  
  67.     // return the number of elements in the PQ
  68.     int size()  {
  69.         return N;
  70.     }
  71.  
  72.     // associate key with index i; 0 < i < NMAX
  73.     void insert(int i, int key) {
  74.         N++;
  75.         index[i] = N;
  76.         heap[N] = i;
  77.         keys[i] = key;
  78.         bubbleUp(N);
  79.     }
  80.  
  81.     // returns the index associated with the minimal key
  82.     int minIndex()  {
  83.         return heap[1];
  84.     }
  85.  
  86.     // returns the minimal key
  87.     int minKey()    {
  88.         return keys[heap[1]];
  89.     }
  90.  
  91.     // delete the minimal key and return its associated index
  92.     // Warning: Don't try to read from this index after calling this function
  93.     int deleteMin() {
  94.         int min = heap[1];
  95.         swap(1, N--);
  96.         bubbleDown(1);
  97.         index[min] = -1;
  98.         heap[N+1] = -1;
  99.         return min;
  100.     }
  101.  
  102.     // returns the key associated with index i
  103.     int keyOf(int i)    {
  104.         return keys[i];
  105.     }
  106.  
  107.     // change the key associated with index i to the specified value
  108.     void changeKey(int i, int key)  {
  109.         keys[i] = key;
  110.         bubbleUp(index[i]);
  111.         bubbleDown(index[i]);
  112.     }
  113.  
  114.     // decrease the key associated with index i to the specified value
  115.     void decreaseKey(int i, int key)    {
  116.         keys[i] = key;
  117.         bubbleUp(index[i]);
  118.     }
  119.  
  120.     // increase the key associated with index i to the specified value
  121.     void increaseKey(int i, int key)    {
  122.         keys[i] = key;
  123.         bubbleDown(index[i]);
  124.     }
  125.  
  126.     // delete the key associated with index i
  127.     void deleteKey(int i)   {
  128.         int ind = index[i];
  129.         swap(ind, N--);
  130.         bubbleUp(ind);
  131.         bubbleDown(ind);
  132.         index[i] = -1;
  133.     }
  134. };
  135.  
  136. class Interval {
  137. public:
  138.     int start, finish;
  139.  
  140.     bool operator < (const Interval& x) const   {
  141.         if (start != x.start)
  142.             return start < x.start;
  143.         return finish < x.finish;
  144.     }
  145. } *intervals;
  146.  
  147. int main()  {
  148.     int N, i, j, d, *schedule;
  149.  
  150.     scanf("%d", &N); // Number of intervals
  151.     intervals = new Interval[N];
  152.     for (i = 0; i < N; i++)
  153.         scanf("%d %d", &intervals[i].start, &intervals[i].finish);
  154.  
  155.     // sort intervals in non-decreasing order of start times
  156.     sort(intervals, intervals + N);
  157.  
  158.     schedule = new int[N];
  159.     MinIndexedPQ pq(N);
  160.  
  161.     d = 0;
  162.     schedule[0] = 0;
  163.     pq.insert(0, intervals[0].finish);
  164.  
  165.     for (i = 1; i < N; i++) {
  166.         j = pq.minIndex();
  167.         if (intervals[i].start >= pq.keyOf(j))  {
  168.             schedule[i] = j;
  169.             pq.increaseKey(j, intervals[i].finish);
  170.         } else  {
  171.             d++;
  172.             schedule[i] = d;
  173.             pq.insert(d, intervals[i].finish);
  174.         }
  175.     }
  176.  
  177.     printf("%d\n", d + 1); // Minimum number of resources required
  178.     for (i = 0; i < N; i++) // Assignment of resources to each interval
  179.         printf("%d %d %d\n", intervals[i].start, intervals[i].finish, schedule[i]);
  180.  
  181.     return 0;
  182. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement