Acdream 1216Beautiful people (double longest ascending subsequence)

Source: Internet
Author: User

Test instructions is: There are a lot of people, have wisdom and charm value, everyone must be taller than others or lower than others to do, or will fight, now find out the largest number of not to fight to participate in the party
This topic just began to listen than people say is the first reorder, the second order, and then find the longest ascending subsequence, In fact, it is not as simple as it is, 1 10,2 12,3 1 This set of data if according to the above statement is wrong, because the need to output subscript, it is obviously wrong, then how to operate now, need a path array to hold something, just at the beginning, I stored the data subscript, Later found that the above situation is not considered, that is, path can be used to store the element in the "queue" is now located in the location is the first place, where you can use two points to find, so that the length of the minimum is unchanged, and then the key point, why I want to reverse the second, the positive sequence can not? If the positive sequence of the words 1 2,2 2, then the value of the path array is 1,2,1,2, whether from the reverse or the positive sequence is not the correct answer, then why to reverse order, reverse to find it, because the words below the largest will and the smallest in a piece, such words, It solves all the problems

#include <iostream> #define INF-9999999999 #include <string.h> #include <algorithm> #include <
Stdio.h> using namespace std;
const int maxn=100005;
    struct SA {int x, y;
int p;
};
    int cmp2 (const SA a,const sa b) {if (a.x==b.x) return a.y>b.y;
else return a.x<b.x;
} sa A[MAXN];
int DP[MAXN],PATH[MAXN];
    int main () {int n;
        while (cin>>n) {memset (a,0,sizeof (a));
        Memset (Dp,0,sizeof (DP));
        memset (path,0,sizeof (path));
            for (int i=1; i<=n; i++) {cin>>a[i].x>>a[i].y;
        A[i].p=i;
        } sort (A+1,A+1+N,CMP2);
        int Len=1,pos=1;
        DP[1]=A[1].Y;
    PATH[1]=A[1].P;
        Path[1]=1;
                for (int i=2; i<=n; i++) {if (A[i].y>dp[len]) {dp[++len]=a[i].y;
            Path[i]=len; } else {Pos=lower_bound (dp+1,dp+len+1,a[I].Y)-DP;
                    {dp[pos]=a[i].y;
                Path[i]=pos;
        }}} cout<<len<<endl;
                for (int i=n; i>=1; i--) {if (Path[i]==len) {cout<<a[i].p;
                if (len!=1) cout<< "";
            len--;
    }} cout<<endl;
} 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.