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