acm--sequence of wood--greedy + descending subsequence--hdoj 1051--wooden sticks

Source: Internet
Author: User
Tags sort time limit


Hdoj Title Address: Portal

Wooden SticksTime limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)Total submission (s): 17453 Accepted Submission (s): 7121

Problem Description There is a pile of n wooden sticks. The length and weight of each stick is known in advance. The sticks is 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 is associated with cleaning operations and changing tools and shapes. The setup times of the woodworking machine is 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 would need no setup time for a stick of length l ' and weight W ' if l<=l ' and W<=w '. Otherwise, it'll need 1 minute for setup.

You is 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 is (4,9), (5,2), (2,1), (3,5), and (1,4), then the Minimum setup time should be 2 minutes since there are a sequence of pairs (1,4), (3,5), (4,9), (2,1), (5,2).

Input the input consists of T test cases. The number of test cases (T) is given on the first line of the input file. Each test case consists of a lines:the first line has a integer n, 1<=n<=5000, that represents the number of Wo Oden sticks in the test case, and the second line contains n 2 positive integers L1, W1, L2, W2, ..., LN, WN, each of Magn Itude at most 10000, where Li and wi is the length and weight of the i th wooden stick, respectively. The 2n integers is delimited by one or more spaces.

Output the output should 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
2 1 3

Test instructions: A machine produces wood, it takes a minute to start, and if the next wood is shorter and lighter than the last wood, it will take a minute to debug the machine,

If there is no time to debug than the previous length and weight, ask for the shortest time.


Solving:

Greedy + descending subsequence (strictly non-incrementing subsequence). Sort by length from big to small, same length, sort by weight from big to small. Repeatedly looking for descending subsequence, that is, each time this sequence of wood is disposed of, that is, the start time +1, and the processed wood is marked. A variable is used to record the number of pieces of wood that have been processed, several times to find a subsequence, to dispose of wood, and to exit the loop when the number of variables equals the total number of logs.

For example, the first test data

4 9 5 2 2 1 3 5 14

After sorting, for the convenience of looking, we wrote vertically. After sorting:

L W

5 2

4 9

3 5

2 1

1 4

There are two descending sub-sequences:

5 2 2 1

4 9 3 5 1 4



#include <iostream> #include <stdio.h> #include <memory.h> #include <algorithm> using namespace
Std
   struct node{int length,weight;
The int ok;//is used to mark whether the wood has been disposed of}result[5010];
    /** Sort Sorting function */BOOL CMP (Node A,node b) {if (a.length>b.length) {return true;
       }else if (a.length==b.length) {if (a.weight>b.weight) return true;
    return false;
} return false;
   } int main () {int n,m,i,index;
   cin>>n;
      while (n--) {cin>>m;
           Enter the length and weight of the wood for (i=0;i<m;i++) {cin>>result[i].length>>result[i].weight;
      result[i].ok=0;
       }/Sorting sort (result,result+m,cmp);
        int Sum=0;int index=0;//The number of logs that have been processed by Node temp;
            while (1) {if (index==m)//When all wood is processed, end loop break; Every time you start, look for the longest final wood for (int i=0;i<m;i++) {if (!result[i].ok) {temp=
          Result[i];               break;//don't forget this phrase}} for (int i=0;i<m;i++) {//Before Mention is that the wood has not been disposed of, so it must be added. Wood[i].ok if (!RESULT[I].OK&AMP;&AMP;RESULT[I].LENGTH&LT;=TEMP.LENGTH&AMP;&AMP;RESULT[I].WEIGHT&LT;=TEMP.W
                    Eight) {result[i].ok=1;//The wood is disposed of index++;
                The temp=result[i];//promptly replaces the longest, heaviest piece of wood, and decrements the next element of the current element of the subsequence.
        }} sum++;//at the end of one boot process.
   } printf ("%d\n", sum);
 }
}



Reference Blog: Portal



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.