POJ 3190 Stall Reservations-dairy column (Greedy range, priority queue)

Source: Internet
Author: User
Tags time 0

POJ 3190 Stall Reservations-dairy column (Greedy range, priority queue)

Http://poj.org/problem? Id = 3190

A single cow needs to be exclusive to A column within the time range [A, B. Ask how many columns are required at least.

The greedy strategy is to give priority to the smallest cow of A, maintain the smallest heap of B, and put the new cow into the smallest column of B.


# Include

# Include

#include 
  
Using namespace std; struct Section {unsigned int index; unsigned int begin; unsigned int end; bool operator <(const Section & B) const {return begin <B. begin ;}}; struct Stall {unsigned int id; unsigned int end; bool operator <(const Stall & B) const {return end> B. end ;}stall () {}stall (unsigned int id, unsigned int end): id (id), end (end ){}}; # define MAX_COWS 50000 Section cow [MAX_COWS]; unsigned int result [MAX_COWS]; // the corresponding column of each ox priority_queue Que; // the smallest heap. It stores the end point (that is, the rightmost end) of all bullbar ranges. inline void put_cow (const int & I, const bool & new_stall) {Stall s; if (new_stall) {s. id = que. size () + 1;} else {s. id = que. top (). id; que. pop ();} s. end = cow [I]. end; result [cow [I]. index] = s. id; que. push (s );} /// // SubMain //////////// //// // int main (int argc, char * argv []) {# ifndef ONLINE_JUDGE freopen ("in.txt", "r", stdin); freopen ("out.txt", "w", stdout ); # endif int N; cin> N; for (int I = 0; I <N; ++ I) {cow [I]. index = I; cin> cow [I]. begin; cin> cow [I]. end;} sort (cow, cow + N); put_cow (0, true); for (int I = 1; I <N; ++ I) {put_cow (I, cow [I]. begin <= que. top (). end);} cout <que. size () <endl; for (int I = 0; I <N; ++ I) {cout <result [I] <endl ;} # ifndef ONLINE_JUDGE fclose (stdin); fclose (stdout); system ("out.txt"); # endif return 0 ;} /// // End Sub /////////// ///////////////////////


  
Code 2

Sort the cow. Sort by L and R in ascending order. Make sure that the smallest L is obtained first during traversal.

Then we can traverse and solve the problem.

# Include
   
    
# Include
    
     
# Include
     
      
Using namespace std; struct N {int l, r, id, stall; bool operator <(const N & a) const {return a. r
      
       
Que; int n; cin> n; for (int I = 1; I <= n; I ++) {cin> cow [I]. l> cow [I]. r; cow [I]. id = I;} sort (cow + 1, cow + n + 1, cmp); cow [0]. stall = 1; cow [0]. r = 0; que. push (cow [0]); int a [50010]; int S = 2; for (int I = 1; I <= n; I ++) {N c = que. top (); if (cow [I]. l> c. r) {que. pop (); cow [I]. stall = c. stall; a [cow [I]. id] = c. stall; que. push (cow [I]);} else {cow [I]. stall = S; a [cow [I]. id] = S ++; que. push (cow [I]) ;}} cout <
       
        
Gorgeous demarcation line .................................... .........
        

Two greedy strategies are known to be correct for this question.

  1. Sort the starting time of the ox by the starting time. Each time you select the earliest starting time in the ox set and the element that does not conflict with the ending time, delete or mark the element from the set. When the start time cannot be foundGreaterWhen the end time is an element of the condition, select the element from the element again until it is null.
  2. Sort the first ox by the start time, and first store the first ox in the set. If the following ox satisfies that the start time is greater than the earliest end time, this element is taken out, and update the end time of the time to the end time of the Current ox, and then save it to the collection again. Otherwise, the current ox will be directly stored in the collection. 2.2 greedy algorithm proof

    There are two ways to prove greedy algorithms.

    The first method is known as staying ahead [2]. This means that if we use a policy to keep a certain condition ahead in every step of the algorithm, in the end, we can use this leading condition to prove the optimal solution. In essence, this method combines the industry and Contradiction methods described above.

    Next, we will apply this method to the proof of POJ 3190 algorithm 2.

    1. First, we need to determine a leading condition. We set this leading condition to: in each step of the algorithm, we select the shortest bar of the minimum start time and place it to the next ox. That is, it indicates the current policy and the optimal policy. For the proof of greedy algorithms, the basic assumption is:The current greedy policy should at least ensure the same effect as the optimal policy..
    2. It is proved that this condition is true in every step of the algorithm. The use of the industry method first proves the base case. We first store the data in the ox from the start time 0, so it is clearly true. Next, we will prove that the first k cows were established, and k + 1 was also established. In either case, the start time of the k + 1 oxLessIf the minimum end time of the set is saved to the set, either the minimum End Time is updated to the end time of the first ox or the original End Time is kept. Otherwise, if the start time of the k + 1 oxGreaterThe minimum End Time in the set to update the minimum End Time element of the set. Therefore, you can select the shortest bar with the minimum end time.
    3. Next we will use the Contradiction method to prove that our strategy can get the solution with the minimum number of animals. Since we have proved that each step must have a minimum start time column, if the above strategy is not true, there must be a column in the optimal solution, and all the cows can be placed in other bars. These cows can also be placed in the barrier of greedy policy solutions (because we prove that the minimum start time selected in each step is at least the same as the optimal solution ). However, when a cow is placed, if its start timeGreaterThe minimum end time must have been placed in the previous barrier, so it is determined that these cows cannot be placed in the original barrier.
    4. Now, the proof is over. Algorithm 2 can obtain the optimal solution.

      The second method is called exchange arguments [2]. The elements of the optimal solution are exchanged one by one with the elements of the current greedy policy. There are many exchange cases [3], which can be

      Elements that exist in, can be in different order. If the optimal result is not affected, the greedy policy is established. In our questions, the exchange conditions can be different for the animals. We know that algorithm 2 is the optimal solution to prove that algorithm 1 is also the optimal solution.

      Exchange

      Different from the element in, if there is a different animal barrier in, there is only one situation:

      C1 ------------- | t1

      C2 ------- | t2

      If the start time of the Current ox

      Algorithm 1 will not process this element, but put this element in the second round, while algorithm 2 directly puts the current element. In this case, the results of the two algorithms are the same.

      If the start time of the Current ox is t1 ">

      Algorithm 1 puts the elements, but algorithm 2 puts the elements. However, such processing will not affect the optimal solution. After the algorithm puts the elements, it must only store the elements whose start time is later. Other elements whose start time is earlier will be placed.

      C1 ------------- | t1

      C2 ------- | t2 ------------ | t3

      For algorithm 1:

      C1 ------------- | t1 ----------- | t3

      C2 ------- | t2

      The other start time is

      The previous elements will be put in, and the start time of these elements must be t1 ">, so the put and put will not affect the final effect of the algorithm. Therefore, greedy policy sets up 2.3 Time Complexity Analysis

      The following is a brief analysis of the three solutions for this question:

      • Algorithm 1 solution 1Put the element into the set. When updating each time, find the set and delete the element from it. Element complexity ,. The traversal complexity. In the worst case, we delete an element each time:
      • Algorithm 1 solution 2Elements are stored in the array, and the elements that have been deleted are traversed each time. Sort :. Traversal is also the worst case. Each time we move forward an element (Binary Search ):
      • Algorithm 2 solution 1Maintain a minimum heap and update the minimum heap each time. Sort :. Minimum heap maintenance. In the worst case, the minimum heap cannot be updated every time:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.