Java Implementation of LIS algorithm, operational formation problems, lis Formation

Source: Internet
Author: User

Java Implementation of LIS algorithm, operational formation problems, lis Formation

Suppose there is a sequence: 2, 1, 3, 5, and the longest ascending subsequence is 2, 3, 5, 3, 5, and the length is 3.

The idea of the LIS algorithm is:

Set sequence.

① If there is only one element, the maximum length of the ascending subsequence is 1;

② If there are two elements, if a [1]> a [0], the maximum length of the ascending subsequence is 2, a [1] is the last element of the longest ascending subsequence. If a [1] <a [0], the longest ascending subsequence is 1, both a [0] And a [1] are the last element of its longest ascending subsequence.

③ If three elements exist, if a [2]> a [0], a [2]> a [1], then a [2] can be used as the last element of the longest ascending subsequence of a [0] or a [1. Which sequence to choose depends on a [0], and a [1] must be in a longer sequence.

④ Extended to n elements is to see the length of the longest ascending subsequence with a [n] as the last element.

 

Define two Arrays: a and B.

A stores the original data, and B [I] stores the longest ascending sub-sequence length ending with a [I.

 

The Code is as follows:

Class Lmax {public static void Lmax (int [] a, int [] B) {B [0] = 1; for (int I = 1; I <. length; I ++) {int countmax = 0; for (int j = 0; j <I; j ++) {if (a [I]> a [j] & B [j]> countmax) {countmax = B [j]; // record the length of the Child sequence with a element value smaller than a [I] But corresponding to the longest child sequence} B [I] = countmax + 1; // The maximum length of the sequence corresponding to a [I] Is }}}

 

 

 

Ii. Operational Formation

Description:

When I was in high school, every morning, the school had to organize all the teachers and students to run and exercise their bodies. Every time the sound of an exercise, Everyone began to run downstairs, then, the team should be at the front of the team and the team should be at the end of the team. Suddenly, one day the director thought of an idea and wanted to change the formation, that is, when everyone ran down from the upstairs, all the students randomly ranked in a row, then the exercise manager extracts a part of the students from the team, so that the height of the remaining students in the team is a "Mountain" shape that increases first and then drops. It is said that such a shape can bring good luck to everyone and wish everyone a brave climb on the road to learning. (Note: only one side of the mountain meets the conditions, such as, and 1)

Input:

The input may contain multiple test examples.
For each test case, the first line is an integer n (1 <=n <= 1000000): represents the number of students to be entered.
The second line contains n integers: the height of the student (cm) (height is a positive integer not higher than 200 ).

Output:

For each test case, the minimum number of students to be extracted is output.

Sample input:

6

100 154 167 159 132 105

5

152 152 152 152 152

Sample output:

0

4

 

When using LIS to solve this problem, consider the following:

First, use LIS to calculate the length of the longest ascending subsequence ending with each element, and then sort the array in reverse order, use LIS to find the length of the longest ascending subsequence ending with each element.

Obtain two arrays b1 and b2.

B1 and b2 correspond to the sum and then subtract the repeated one, which is the longest 'mount '.

 

Public class peak {public static void main (String [] args) {int n; int re; do {random in = new random (System. in); n = in. nextInt () ;}while (n <0 | n> 100000); int [] a = new int [n]; // original array int [] ar = new int [n]; // reverse array struct in = new struct (System. in); for (int I = 0; I <n; I ++) {a [I] = in. nextInt ();} int [] b1 = new int [n]; @ SuppressWarnings ("unused") int [] b2 = new int [n]; Lmax. lmax (a, b1); ar = reverse. reverse (a); Lmax. lmax (ar, b2); // solves the longest ascending subsequence of the reverse order array b2 = reverse. reverse (b2); // returns the longest ascending subsequence of the reverse-order array in reverse order to add re = result to the longest ascending subsequence of the original array. result (b1, b2); System. out. print (re );}}



 

 

Class result {public static int result (int [] a, int [] B) {int max = 0; int [] c = new int [. length]; for (int I = 0; I <. length; I ++) {c [I] = a [I] + B [I];} Arrays. sort (c); max = c [c. length-1]-1; // returns. length-max ;}}

 

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.