ACdream 1216 (ASC training 1) Beautiful People (DP), acdream1216

Source: Internet
Author: User

ACdream 1216 (ASC training 1) Beautiful People (DP), acdream1216

Question address: http://acdream.info/problem? Pid = 1, 1216

At the beginning of this question, we used a line segment tree. Later we found that we still needed DP to process the query, which was quite troublesome .. It will be gone .. Later I thought that this question is actually a two-dimensional longest ascending subsequence ..

Sort first, sort by the first keyword on the left, and then sort by the second keyword on the right. In this case, those with the same first keyword are definitely not in the same ascending subsequence. Then, only perform the DP with the complexity of the second keyword O (n * logn), find the longest ascending sequence, process the precursor, and output it.

The Code is as follows:

#include <iostream>#include <cstdio>#include <string>#include <cstring>#include <stdlib.h>#include <math.h>#include <ctype.h>#include <queue>#include <map>#include <set>#include <algorithm>using namespace std;#define LL long longstruct node{    int x, y, num;}fei[110000];int cmp(node x, node y){    if(x.x==y.x)        return x.y>y.y;    return x.x<y.x;}int a[110000], d[110000], pre[110000], len, b[110000];int bin_seach(int x){    int low=0, high=len, mid, ans;    while(low<=high)    {        mid=low+high>>1;        if(a[mid]>=x)        {            high=mid-1;            ans=mid;        }        else        {            low=mid+1;        }    }    return ans;}int main(){    int n, i, j, pos, cnt;    while(scanf("%d",&n)!=EOF)    {        for(i=0;i<n;i++)        {            scanf("%d%d",&fei[i].x,&fei[i].y);            fei[i].num=i+1;        }        sort(fei,fei+n,cmp);        len=1;        a[1]=fei[0].y;        d[0]=-1;        d[1]=0;        memset(pre,-1,sizeof(pre));        for(i=1;i<n;i++)        {            if(fei[i].y>a[len])            {                a[++len]=fei[i].y;                pre[i]=d[len-1];                d[len]=i;            }            else            {                pos=bin_seach(fei[i].y);                a[pos]=fei[i].y;                pre[i]=d[pos-1];                d[pos]=i;            }        }        printf("%d\n",len);        cnt=0;        /*for(i=0;i<n;i++)        {            printf("%d ",fei[i].num);        }        puts("");*/        for(i=d[len];i!=-1;i=pre[i])        {            b[cnt++]=fei[i].num;            //printf("%d\n",i);        }        for(i=0;i<cnt-1;i++)            printf("%d ",b[i]);        printf("%d\n",b[cnt-1]);    }    return 0;}





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.