Dilworth theorem + attribute ordering (stick processing)

Source: Internet
Author: User

P1233 Stick Processing Title Description

A pile of wood sticks have n roots, and the length and width of each stick are known. A stick can be machined one machine at a to the next. The machine needs to prepare time before it can handle a stick. The preparation time is defined like this:

The preparation time of the first stick is 1 minutes;

If the stick with the length of L and W is just finished, then if the next stick length is Li, the width is Wi, and satisfies the L>=LI,W>=WI, the stick does not need to prepare the time, otherwise it takes 1 minutes to prepare the time;

Calculates the shortest preparation time required to process n sticks. For example, you have 5 sticks, the lengths and widths were (4, 9), (5, 2), (2, 1), (3, 5), (1, 4), and the shortest preparation time was 2 (by (4, 9), (3, 5), (1, 4), (5, 2), (2, 1), and ().

Input output Format input format:

The first line is an integer n (n<=5000), and the 2nd line is a 2n integer, respectively, L1,w1,l2,w2,...,ln,wn. The values of L and W are not more than 10000, and the adjacent two numbers are separated by a space.

Output format:

A single line, an integer, that requires the shortest preparation time.

Input and Output Sample input example # #:
54 9 5 2 2 1 3 5 1 4
Sample # # of output:
2

Get this problem, the first idea: violence, with the operation to optimize the use of SAO, maybe it will soon fly!!!
But in fact, just a little bit of thinking, you will find this is a naked DP ah.
Further analysis, we will find that to meet the L0>L1,W0>W1 at the same time, it shows that it has two properties, the fool can see the problem of greed is to make the sequence satisfies the two properties, as much as possible
Find the longest ascending subsequence, right?
And because of the Dilworth theorem, we know that the number of the longest ascending subsequence in the same sequence is equal to the length of the longest non-ascending subsequence, so we replace the problem with the longest non-ascending sub-sequence.
I'm sure everyone will be O (NLOGN) algorithm to beg, right?
So I won't repeat it here.
But there is another problem, how to deal with the problem of double attributes, we have a common method, that is, one of the heavy attributes of the order to find the other heavy target sequence, this thing ah, I can not speak clearly
I feel more experience, and then use some of the Sao operation, I believe you will understand, right?
Connaught, here is the code (I wrote it in STL)
#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>#defineMAXN 5000+5using namespacestd;intN,F[MAXN];structstack{intL,w;} S[MAXN];BOOLcmpConstStack &a,ConstStack &b) {    returnA.l>B.L;}intMain () {scanf ("%d",&N);  for(intI=1; i<=n;i++) scanf ("%d%d",&s[i].l,&S[I].W); Sort (S+1, s+1+n,cmp)//To the attribute dimension//To find the longest non-ascending sub-sequence */intlen=0;  for(intI=1; i<=n;i++)    {        if(s[i].w>F[len]) {f[++len]=S[I].W; }        Else{            intK=lower_bound (f+1, f+1+LEN,S[I].W)-F; F[K]=S[I].W; }    }
printf ("%d", Len);
Length even if the answer.

return 0;}
I am still very konjac konjac, so if there are any loopholes, please also ask the readers of the master pointed out Qaq
Thank you! ......!

Dilworth theorem + attribute ordering (stick processing)

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.