HDU 4512 jige series story-perfect formation I (LCIS Longest Common ascending subsequence), hdulcis
HDU 4512 gigo series story-perfect formation I (LCIS Longest Common ascending subsequence)
Http://acm.hdu.edu.cn/showproblem.php? Pid = 1, 4512
Question:
Gego has been interested in the formation over the past few days.
One day, n people stood in front of him in sequence. Their heights were h [1], h [2]... h [n], gigo hopes to pick out some people from them to form a new formation. If the new formation meets the following three requirements, it is called a perfect formation:
1. The picked persons remain unchanged in the relative order of the original formation;
2. symmetric between the left and right. If m people form a new formation, the height of 1st people is the same as that of m people, and that of 2nd people is the same as that of m people, if m is an odd number, the person in the middle can be any;
3. The person from left to middle must increase his height. If H is used to represent the height of the new formation, then H [1] <H [2] <H [3]... <H [mid].
Now, gigo wants to know: How many people can be selected to form a perfect formation?
Analysis:
In fact, this question is about LCIS, but we need to enumerate each intermediate demarcation point, and we also need to deal with the situation where the intermediate point is shared by both the front and back sequences. That is, the original sequence is truncated into two sequences, and the subsequent sequence is completely reversed, and then the LCIS of the two new sequences is obtained.
When the demarcation point is not shared by two sequences:
First, we enumerate the decomposition point I of the current sequence, and then calculate a [1 .. I] and sequence a [n... The LCIS length of I + 1] * 2 is a possible maximum value. (Think about why)
When the demarcation point is used together by two sequences:
First, we enumerate the decomposition point I of the current sequence, and then calculate a [1 .. I] and sequence a [n... I] The LCIS length * 2-1 is a possible maximum value. (Think about why)
For how to calculate the LCIS of two sequences, refer:
Http://blog.csdn.net/u013480600/article/details/41094619
AC code:
# Include <cstdio> # include <algorithm> # include <cstring> using namespace std; const int maxn = 300 + 10; int n; int a [maxn]; int B [maxn]; // string B is the inverse int f [maxn] of string a; // obtain the longest common ascending subsequence int LCIS (int x, int y) between a [x] and B [y] {memset (f, 0, sizeof (f )); for (int I = 1; I <= x; I ++) {int max = 0; for (int j = 1; j <= y; j ++) {if (a [I]> B [j] & max <f [j]) max = f [j]; if (a [I] = B [j]) f [j] = max + 1 ;}} int ans = 0; for (int I = 1; I <= y; I ++) ans = max (ans, f [I]); return ans;} int main () {int T; scanf ("% d ", & T); while (T --) {int n; scanf ("% d", & n); for (int I = 1; I <= n; I ++) {scanf ("% d", & a [I]); B [n-I + 1] = a [I];} int ans = 0; for (int I = 1; I <= n; I ++) // enumerate each demarcation point {ans = max (ans, LCIS (I, n-I) * 2 ); ans = max (ans, LCIS (I, n-I + 1) * 2-1);} printf ("% d \ n", ans);} return 0 ;}