Zoj 2254-island country

Source: Internet
Author: User

Question: When two people go to a country with many islands, ask them to visit the largest islands in the same order.

Analysis: DP, Lis, and LCs. There are two solutions for this question: LCs and Lis.

LCS: sort the two persons separately. Find the numbered LCs and t = O (N ^ 2 );

LIS: Converting LCS into Lis using ing relationships T = O (nlogn );

The conversion is a bit disgusting. Find the position of B point with the same number as a after sorting.

Description: I wrote a lis function... But it's a lot worse than rhubarb... Ym ~~.

# Include <stdio. h> # include <stdlib. h> # include <string. h> typedef struct node {int ID; int in;} visit; visit a [1001]; visit B [1001]; int s [1001]; int V [1001]; int CMP (const void * a, const void * B) {visit * P = (visit *) A; visit * q = (visit *) B; if (p-> In = Q-> In) return p-> ID-Q-> ID; else return p-> In-Q-> In ;} int muq [1001]; int Lis (int n, int * q) {int L, R, M, I, tail = 0; For (muq [++ tail] = Q [1], I = 2; I <= N; ++ I) {If (muq [tail] <q [I]) {muq [++ tail] = Q [I]; continue;} For (M = (r = tail) + (L = 1)> 1 ); L <r; M = (L + r)> 1) if (muq [m] <q [I]) L = m + 1; else r = m; muq [m] = Q [I];} return tail;} int main () {int N; while (scanf ("% d", & N )! = EOF & N) {for (INT I = 1; I <= N; ++ I) scanf ("% d", & A [I]. in); For (INT I = 1; I <= N; ++ I) scanf ("% d", & B [I]. in); For (INT I = 1; I <= N; ++ I) A [I]. id = B [I]. id = I; qsort (& A [1], n, sizeof (visit), CMP); qsort (& B [1], n, sizeof (visit), CMP ); for (INT I = 1; I <= N; ++ I) s [A [I]. id] = I; for (INT I = 1; I <= N; ++ I) V [I] = s [B [I]. id]; printf ("% d \ n", Lis (n, v);} return 0 ;}

Zoj 2254-island country

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.