HDU Wooden Sticks (Greedy)

Source: Internet
Author: User

HDU Wooden Sticks (Greedy)
Wooden SticksTime Limit: 2000/1000 ms (Java/Other) Memory Limit: 65536/32768 K (Java/Other) Total Submission (s): 19 Accepted Submission (s): 7 Font: times New Roman | Verdana | GeorgiaFont Size: Batch → Problem DescriptionThere 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 ).
InputThe 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.
OutputThe 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
SourceAsia 2001, Taejon (South Korea) AC code:
#include 
 
  #include #include 
  
   #include 
   
    using namespace std;struct wooden{    int l,w;};wooden my[5010];bool comp(wooden a,wooden b){    if(a.l>b.l)return 1;    else if(a.l==b.l)        return a.w>b.w;    else return 0;}int main(){    int t;    cin>>t;    while(t--){        int n;        cin>>n;        int i=0,j;        while(i
    
     >my[i++].l>>my[i].w;        sort(my,my+n,comp);        //for(i=0;i
     
      =my[i].l&&my[j].w>=my[i].w){            out--;            my[j].l=my[i].l;            my[j].w=my[i].w;            my[i].l=0;            my[i].w=0;            break;            }        }        // for(i=0;i
      
     
    
   
  
 

Related Article

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.