1051 Wooden Sticks greedy

Source: Internet
Author: User

 

These basic things have been practiced through several questions over the past two days.

All questions are simple.

I forgot where I saw someone who said: Some people think that greedy cannot always find the optimal solution, so greedy is useless. The difference between a master and a cainiao is reflected here. The good greedy use is generally a great god.

So pay attention to greed in the future.

 

Wooden Sticks Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission (s): 10885 Accepted Submission (s): 4487



Problem Description There is a pile of n wooden sticks. the length and weight of each stick are known in advance. the sticks are to be processed by a woodworking machine in one by one fashion. it needs some time, called setup time, for the machine to prepare processing a stick. the setup times are associated with cleaning operations and changing tools and shapes in the machine. the setup times of the woodworking machine are given as follows:

(A) The setup time for the first wooden stick is 1 minute.
(B) Right after processing a stick of length l and weight w, the machine will need no setup time for a stick of length l' and weight W' if l <= l' and w <= W '. otherwise, it will need 1 minute for setup.

You are to find the minimum setup time to process a given pile of n wooden sticks. for example, if you have five sticks whose pairs of length and weight are (), and ), then the minimum setup time shocould be 2 minutes since there is a sequence of pairs ).

Input The input consists of T test cases. the number of test cases (T) is given in the first line of the input file. each test case consists of two lines: The first line has an integer n, 1 <= n <= 5000, that represents the number of wooden sticks in the test case, and the second line contains n 2 positive integers l1, w1, l2, w2 ,..., ln, wn, each of magnloud at most 10000, where li and wi are the length and weight of the I th wooden stick, respectively. the 2n integers are delimited by one or more spaces.

Output The output shoshould contain the minimum setup time in minutes, one per line.

Sample Input
3 5 4 9 5 2 2 1 3 5 1 4 3 2 2 1 1 2 2 3 1 3 2 2 3 1

Sample Output
213
When I searched for the question, the author said that this was a greedy question and I did not think much about it. I can learn things anyway.

 

This is because an error occurs during the intermediate process.

It is easy to use, that is, sort by length, length, and width.

The c ++ function called is very fast.

It is best to write the sorting function by myself, because I call all the functions, so it is difficult or even difficult for me to write fast sorting.

The length is ordered, so you don't have to worry about it. Next, we can only process the width.

Traverse over and over again. Traversal is the result + 1. During the traversal process, all the tags that can be processed this time have been processed, so you don't need to worry about it next time.

Finally, let's take a look at the process after several traversal.

Paste the code.

 

# Include
 
  
# Include
  
   
# Include using namespace std; struct Point {int l; int w ;}; bool comp (Point a, Point B) {if (a. l! = B. l) {return a. l
   
    
> Cases; while (cases --) {int n; cin> n; Point p [5001]; for (int I = 0; I
    
     
> P [I]. l; cin> p [I]. w;} sort (p, p + n, comp); int m = 0, t = 0; bool B [5001]; int ans = 0; memset (B, false, sizeof (B); while (m
     
      
= T) {m ++; B [j] = true; t = p [j]. w ;}}cout <the code is simple and easy to understand. I used a structure storage stick and a bool array to identify whether the code has been processed. M is used to indicate the number of elements processed. If the number of elements processed is the same as the number of input elements, the while operation ends.
      

 

T is the width of the current processing, and can only be processed later than the current time.

Okay!

Thank you for your persistence.

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.