Two-way monotonic auto-incrementing subsequence model [acdream 1216]

Source: Internet
Author: User

Title: acdream 1216 beautiful people


Question: each person has two values: ability value and potential value. Then, when both values of a person are strictly greater than those of the second person, the two people can stay together and give the values of many people, how many people can be found at most?


Analysis: it is easy to think of a monotonic non-incrementing model. If I use O (N * n), it will time out!

Then we need to use binary optimization to find out.

We can first sort by the first value x from small to large, and then sort by the second value from large to small, so that we can find the maximum number is correct. (Think about why)

For example:

5

1 10

2 12

3 5

3 1

4 3

6 7

First, the DP array only has the first, two groups of samples: 1 10 and 2 12

Then Replace the first group with the Third example: 3 5 and 2 12. Look at this example and find that it is not satisfied, but its total length will not change, however, if this parameter is saved, the subsequent number can be the longest. So we need to save the path

Then, replace the fourth group with the third group: 3 1 and 2 12.

Then, replace the second group with the example in the 5th group: 3 1 and 4 3 are the conditions met, and the maximum value is smaller,

Then the sixth group of samples is added after: 3 1 and 4 3 and 6 7, the maximum value.

The binary search value for all the final values is: 1 2 1 2 3

The storage path is also simple.

Then, output a descending initial array number from this value,


AC code:

# Include <cstdio> # include <cstring> # include <string> # include <iostream> # include <algorithm> using namespace STD; const int INF = 0x3f3f3f; const int n = 110000; struct node {int X, Y; int num, Count ;}; node A [n]; int CMP (node A, Node B) {if (. x! = B. X) return a. x <B. X; If (A. Y! = B. y) return. y> B. y;} int DP [N], Mark [N]; int bin_search (int l, int R, int X) {While (L <= r) {int mid = (L + r)/2; // if the values are equal, a smaller value is returned. If (DP [Mid] = x) return mid; else if (DP [Mid] <= x) L = Mid + 1; else r = mid-1 ;} return L;} int main () {int N; while (~ Scanf ("% d", & N) {memset (DP, 0, sizeof (DP); For (INT I = 1; I <= N; I ++) {scanf ("% d", & A [I]. x, & A [I]. y); A [I]. num = I;} Sort (a + 1, A + n + 1, CMP); memset (DP, 0x3f3f3f3f, sizeof (DP); int ans = 0; int Len = 1; for (INT I = 1; I <= N; I ++) {int TMP = bin_search (1, Len, a [I]. y); // lower_bound (DP + 1, DP + 1 + n, a [I]. y)-DP; If (TMP = Len) Len ++; DP [TMP] = A [I]. y; Mark [I] = TMP; ans = max (ANS, TMP);} printf ("% d \ n", ANS); For (INT I = N; i> = 1; I- -) {// Printf ("XX % d", Mark [I]); If (MARK [I] = ans) {printf ("% d ", A [I]. num); If (ANS! = 1) printf (""); ans --;} printf ("\ n");} return 0 ;}


Two-way monotonic auto-incrementing subsequence model [acdream 1216]

Related Article

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.