Poj 3636 Russian nested dolls greedy binary dynamic planning

Source: Internet
Author: User

First, we need to distinguish between qsort in # include <cstdlib> and sort in C ++ STL algorithm. The latter has only three parameters and the comparison function is easier to write.

 

Similar to poj 1065, there are two differences. One is that the sorting method drops, and the other is that the requirements fall and are not equal.

However, regular greedy requests will time out and will not time out after optimization by binary mode. Note that the L-incrementing and W-decimal rows

// Similar to poj 1065, there are two differences. One is that the sorting method drops, and the other is that the requirement drops and is not equal. <br/> // The above solution times out, do not time out after optimization with binary mode. Pay attention to the ascending l column, descending W rows <br/> # include <iostream> <br/> # include <cstdio> <br/> # include <cstdlib> <br/> using namespace STD; <br/> typedef struct stick {<br/> int L; <br/> int W; <br/>} Stick; <br/> int CMP (const void * a, const void * B) <br/> {<br/> If (* (stick *) ). l! = (* (Stick *) B ). l) <br/> {<br/> return (* (stick *) ). l-(* (stick *) B ). l; <br/>}< br/> else <br/> {<br/> return (* (stick *) B ). w-(* (stick *) ). w; <br/>}< br/> int main () {<br/> int n, m, I; <br/> int T [20010], Len = 0, R, L, mid; <br/> stick P [20005]; <br/> scanf ("% d", & N); <br/> while (n --) {<br/> Len = 0; <br/> scanf ("% d", & M); <br/> for (I = 0; I <m; I ++) <br/>{< br/> scanf ("% d", & P [I]. l, & P [I]. w); <br/>}< br/> qsort (p, M, sizeof (stick), CMP); <br/> memset (T, 0, sizeof (t); <br/> for (I = 0; I <m; I ++) <br/>{< br/> L = 0; <br/> r = Len; <br/> while (L <r) // uses binary optimization <br/> {<br/> mid = (L + r) /2; <br/> If (T [Mid]> = P [I]. w) L = Mid + 1; <br/> else r = mid; <br/>}< br/> If (LEN = L) Len ++; <br/> T [l] = P [I]. w; <br/>}< br/> printf ("% d/N", Len); <br/>}< br/> return 0; <br/>}< br/> 

 

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.