Poj1065 wooden sticks (Greedy + Dynamic Planning-monotonic incrementing subsequence)

Source: Internet
Author: User

 

Description

C. Has some wood sticks, whose length and quality are known. A machine is required to handle these sticks, which takes a unit of time to start, if the weight and length of the I + 1 wood rod are greater than or equal

 

The I-th processed wooden stick will not take time, otherwise it will take a unit of time. C Xiaojia wants to finish processing the wooden stick in the shortest time because he is in a hurry to go on a date. Can you tell him what to do?

 

Input

The first line is an integer T, indicating a total of T groups of input data.

The first line of each group of test data is an integer N (1 <= n <= 5000), indicating that there are n sticks. In the next line, enter the L, w (0 <L, W <= 10000) of N wooden bars, and separate them with a space, indicating

 

Length and quality of wood sticks.

 

Output

The shortest time for processing these sticks.

 

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

 

 

The Code is as follows:

 

# Include <iostream> <br/> # include <algorithm> <br/> # include <string. h> <br/> using namespace STD; <br/> struct machine <br/>{< br/> int L, W; <br/>} data [5010]; <br/> bool use [5010]; // indicates whether the wooden rod has been used: 1 used, 0 used <br/> bool operator <(const machine, const machine B) // sort the sticks in ascending order. If they are equal, the weights increase. <br/>{< br/> If (. L = B. l) <br/> return. W <B. w; <br/> return. L <B. l; <br/>}< br/> int main () <br/> {<br/> // freopen ("1.txt"," R ", Stdin); <br/> int t; <br/> CIN> T; <br/> while (t --) <br/>{< br/> memset (use, 0, sizeof (use); <br/> int N, Count = 0; <br/> machine last; <br/> CIN> N; <br/> for (INT I = 0; I <n; ++ I) <br/>{< br/> CIN> data [I]. l> data [I]. w; <br/>}< br/> sort (data, data + n); // wood rod sorting <br/> for (INT I = 0; I <N; + + I) // returns the ascending subsequence of the I-th wooden stick. <br/>{< br/> last. W = data [I]. w; // record the start of the new sequence <br/> If (! Use [I]) <br/>{< br/> for (Int J = I + 1; j <n; j ++) <br/>{< br/> If (last. W <= data [J]. W &&! Use [J]) // L No comparison is required for sorting <br/>{< br/> use [J] = 1; // when used, mark it as 1 <br/> last. W = data [J]. w; // team end element <br/>}< br/> count ++; // after the sub-sequence ends, the time is incremented by 1. calculate the I + 1 wooden rod <br/>}< br/> cout <count <Endl; <br/>}< br/> return 0; <br/>} 

 

Solution: (Greedy Algorithm + Dynamic Planning-monotonic incrementing subsequence)

 

1. First, sort the wooden sticks. The overall order is l incremental. If l is equal, W increments.

 

2. Processing the I-th wooden stick: selects the monotonic incrementing subsequence and marks the monotonic incrementing subsequence as 1 (used ). After selection, the time is + 1. Processing of the I + 1 wooden rod

 

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.