Hdu4512 (maximum public ascending subsequence)

Source: Internet
Author: User

I did this question once before. I didn't want to understand it at the time, wa. I saw it again today. After reviewing the question, I confirmed that this was the largest ascending subsequence. At that time, I was very excited, however, I don't know how to identify whether there is an intermediate highest point. After thinking for a long time, I still don't want to understand it, and I still want to check the problem-solving report .....

This algorithm is very clever. It uses brute force to enumerate all cases where there are intermediate points and no intermediate points, and then obtains the largest one. What is this? What about semi-DP semi-violence? What is the combination of violence and aesthetics?

For the algorithm of the maximum public ascending subsequence, I have already explained it in a blog, so I will not explain it anymore.

Link: hdu 1423 (maximum public ascending subsequence)


[Cpp] # include <stdio. h>
# Include <string. h>
# Define N 205
Int a [N], B [N], ans [N];
Int Max (int x, int y)
{
Return x> y? X: y;
}
Int LCIS (int x, int y)
{
Memset (ans, 0, sizeof (ans ));
Int sum;
Int I, j, k;
For (I = 1; I <= x; I ++)
{
K = 0;
For (j = 1; j <= y; j ++)
{
If (B [j] <a [I] & ans [k] <ans [j])
K = j;
If (B [j] = a [I])
Ans [j] = ans [k] + 1;
}
}
Sum = 0;
For (I = 1; I <= y; I ++)
Sum = Max (sum, ans [I]);
Return sum * 2;
}
Int main ()
{
Int T;
Scanf ("% d", & T );
While (T --)
{
Int n;
Int I;
Scanf ("% d", & n );
For (I = 1; I <= n; I ++)
{
Scanf ("% d", & a [I]);
B [n-I + 1] = a [I];
}
Int ans;
Ans = 0;
For (I = 1; I <= n; I ++)
{
Ans = Max (LCIS (I, n-I), ans );
Ans = Max (LCIS (I, n-I + 1)-1, ans );
}
Printf ("% d \ n", ans );
}
Return 0;
}

# Include <stdio. h>
# Include <string. h>
# Define N 205
Int a [N], B [N], ans [N];
Int Max (int x, int y)
{
Return x> y? X: y;
}
Int LCIS (int x, int y)
{
Memset (ans, 0, sizeof (ans ));
Int sum;
Int I, j, k;
For (I = 1; I <= x; I ++)
{
K = 0;
For (j = 1; j <= y; j ++)
{
If (B [j] <a [I] & ans [k] <ans [j])
K = j;
If (B [j] = a [I])
Ans [j] = ans [k] + 1;
}
}
Sum = 0;
For (I = 1; I <= y; I ++)
Sum = Max (sum, ans [I]);
Return sum * 2;
}
Int main ()
{
Int T;
Scanf ("% d", & T );
While (T --)
{
Int n;
Int I;
Scanf ("% d", & n );
For (I = 1; I <= n; I ++)
{
Scanf ("% d", & a [I]);
B [n-I + 1] = a [I];
}
Int ans;
Ans = 0;
For (I = 1; I <= n; I ++)
{
Ans = Max (LCIS (I, n-I), ans );
Ans = Max (LCIS (I, n-I + 1)-1, ans );
}
Printf ("% d \ n", ans );
}
Return 0;
}

 

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.