Codeforces 4D D. Mysterious Present (DP)

Source: Internet
Author: User

Topic Connection:

Codeforces 4D

Main topic:

Give n envelopes, the N envelopes are long and wide, give the size of the card, find the longest sequence that can be loaded into the card, the length and width of the sequence satisfies the last, the longest, and give a set of feasible solutions.

Topic Analysis:
  • This topic is the topic of DP, the state definition dp[i] is the maximum length of the sequence ending with I, and the path of the optimal solution is obtained by using an array record, which is stored in the form of a chain list.
  • First, the given envelopes are sorted, according to the width of the first keyword, high as the second keyword,
  • The state transition equation is as follows: d P[I]= Max j = Span class= "mn" id= "mathjax-span-2310" style= "font-size:70.7%; Font-family:mathjax_main; " >0 J? 1 {d P[J]+1},{a[J].W<a[I].W && a[J].h<a[I].h}
  • The answer is returned recursively in reverse order.
AC Code:
#include <iostream>#include <cstring>#include <cstdio>#include <algorithm>#define MAX 5007using namespace STD;intN,w,h;intDp[max];intBack[max];structnode{intW,h,x;BOOL operator< (ConstNode & A)Const{if(w = = A.W)returnH < a.h;returnW < A.W; }}a[max];voidPrint (intx) {if(A[x]. W <= W | | a[x].h <= h)return; Print (back[x]);printf("%d", a[x].x);}intMain () { while( ~scanf("%d%d%d", &n, &w, &h)) { for(inti =1; I <= N; i++) {scanf("%d%d", &AMP;A[I].W, &a[i].h);        a[i].x = i; } dp[0] = -1; Sort (A +1, a+n+1); for(inti =1; I <= N; i++) {inttemp =-1, id =0; for(intj =0; J < I; J + +)if(A[J].W < A[I].W && A[j].h < a[i].h && a[j].w > W && a[j].h > H)if(Temp < DP[J])                        {temp = Dp[j];                    id = j; }if(A[i].w > W && a[i].h > H) dp[i] =1, back[i] =0;ElseDp[i] =-1;if(temp+1> Dp[i]) {Dp[i] = temp+1;            BACK[I] = ID; }        }intMAXN =0; for(inti =1; I <= N; i++) MAXN = max (MAXN, Dp[i]);if(MAXN = =0)        {puts("0");Continue; }printf("%d\n", MAXN); for(inti =1; I <= N; i++)if(MAXN = = Dp[i]) {print (i);puts(""); Break; }    }}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Codeforces 4D D. Mysterious Present (DP)

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.