Spoj Mstick. Wooden sticks greedy structure sort

Source: Internet
Author: User

Mstick-wooden Sticks

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 (9, 4), (2, 5), (1, 2), (5, 3), and (4, 1), then the minimum setup time should was 2 minutes since there is a sequence of pairs (4, 1), (5, 3), (9, 4), (1, 2), (2, 5).
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 have an integer n, 1 <= n <=, which represents the number of wooden sticks in the test case, and the second line contains 2n positive integers L1, W1, L2, W2,..., Ln, WN, EAC H of magnitude 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.
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
Output
SAMPLE OUTPUT
2 1 3

Source: >            This is a greedy algorithm entry problem, the topic is given $n$ root stick, as well as the length of each stick $l$ and weight $w$. Now to process them, the machine adjusts for 1 minutes, and if a long $l$ heavy $w$ stick is finished, the next long "' $ heavy $w ' stick satisfies the $l\leq l ' $ and $w\leq W ' $, then the machine can continue processing without the need to enter the adjustment time. Otherwise, it will take another 1 minutes to adjust the time to continue processing. The minimum required adjustment time.         if the total minimum time is required, then try to divide the stick into several sequences so that the $l$ and $w$ of the previous root stick and the latter stick in each sequence are very small, but can satisfy$l \leq l ' $ and $w\leq W ' $. Instantly think of should be done by the sort, but the sort of primary and secondary, it is regrettable that we can not at the same time the two main elements of the order, we must first sort $l$, then $w$ greedy, or $w$ sort of $l$ greedy.         The basic idea is that I sort of $l$, so the whole sequence is to satisfy$l \leq l ' $ of spicy, and then traverse this sequence if it also satisfies$w \leq W ' $ So good, mark down the bottom of the stick and find the next stick to meet the conditions, so find the first sub-sequence spicy. To find the rest, we found that we do not know what is looked for, good, to the stick plus a vis state, 0 for not visited 1 for access to the DA, ok so each time to find a sub-sequence to find a stick, the res plus one, and then for this sequence I greedy to find the next stick and its vis recorded as 1. All sticks are marked as 1 when it's time to find out spicy!
1#include <stdio.h>2#include <algorithm>3 4 structSt {5     intL, W, Vis;6     BOOL operator< (ConstST&AMP;C)Const {7         returnl==c.l?w<c.w:l<C.L;8     }9}stick[5001];Ten  One intN; A voidRead () { -scanf"%d", &n); -      for(intI=0; i<n; i++) { thescanf"%d%d", &AMP;STICK[I].L, &STICK[I].W); -Stick[i].vis =0; -     } - } +  - voidFindinti) { +     intK =i; A      for(intj=i+1; j<n; J + +) at         if(!Stick[j].vis) -             if(stick[k].w<=STICK[J].W) { -Stick[j].vis =1; -k=J; -             } - } in  - voidWork () { to     intres =0; +Std::sort (Stick, stick+n); -      for(intI=0; i<n; i++) { the         if(!Stick[i].vis) { *Stick[i].vis =1; $++Res;Panax Notoginseng find (i); -         } the     } +printf"%d\n", res); A } the  + intMain () { -     intT, N; $scanf"%d", &T); $      while(t--) { - read (); - Work (); the     } -     return 0;Wuyi}

by Black Storm (using for Notes)

Spoj Mstick. Wooden sticks greedy structure sort

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.