Codeforces Beta Round #4 (Div. 2 only) D. Mysterious Present

Source: Internet
Author: User

The longest ascending subsequence, this water problem can be seen at a glance.



Main topic:

The protagonist wants to have an envelope in a w*h postcard coat. He has n envelopes, the length and width of each envelope, and the maximum number of layers to ask. Give the order from small to large.


Problem Solving Ideas:

The longest ascending subsequence is simply a memory path.



Here's the code:

#include <set> #include <map> #include <queue> #include <math.h> #include <vector># Include <string> #include <stdio.h> #include <string.h> #include <stdlib.h> #include < iostream> #include <cctype> #include <algorithm> #define EPS 1e-10#define pi acos ( -1.0) #define INF 107374182#define INF64 1152921504606846976#define LC l,m,tr<<1#define RC m + 1,r,tr<<1|1#define Zero (a) fabs (a) <eps#define iabs (x) ((x) > 0? (x):-(x)) #define CLEAR1 (A, X, SIZE) memset (A, X, sizeof (a[0]) * (min (size,sizeof (a))) #define ClearAll (A, X) memset (A, X , sizeof (a)) #define MEMCOPY1 (A, X, SIZE) memcpy (A, X, sizeof (X[0)) * (size)) #define Memcopyall (A, X) memcpy (A, X, sizeof ( x) #define MAX (x, Y) (((x) > (y))? (x): (y)) #define min (x, y) (((x) < (y))?    (x): (y)) using namespace std;struct node{int w,h,num; BOOL operator < (const Node A) const {if (W+H==A.W+A.H) {if (W==A.W) return h<A.H;        else return w<a.w;    } return w+h<a.w+a.h;    }} envelopes[5000];int cnt;int dp[5005],pre[5005];void output (int num) {if (pre[num]!=-1) output (Pre[num]);    printf ("%d", envelopes[num].num); return;}    int main () {int n,w,h;    cnt=0;    scanf ("%d%d%d", &n,&w,&h);        for (int i=0; i<n; i++) {scanf ("%d%d", &envelopes[cnt].w,&envelopes[cnt].h);        envelopes[cnt].num=i+1;    if (envelopes[cnt].w>w&&envelopes[cnt].h>h) cnt++;        } if (cnt==0) {puts ("0");    return 0;    } clearall (pre,-1);    Sort (envelopes,envelopes+cnt);    int maxnum=1,maxp=0;    Dp[0]=1;        for (int i=1; i<cnt; i++) {int max1=-1,mp=-1; for (int j=i-1; j>=0; j--) {if (envelopes[j].w<envelopes[i].w&&envelopes[j].h<envelopes[                I].h&&max1<dp[j]) {max1=dp[j];            Mp=j;      }} dp[i]=max1+1;  PRE[I]=MP;            if (dp[i]>maxnum) {maxnum=dp[i];        Maxp=i;    }} printf ("%d\n", maxnum);    Output (MAXP); return 0;}


Codeforces Beta Round #4 (Div. 2 only) D. Mysterious Present

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.