Implementation of LIS algorithm in Java, doing drill formation problem

Source: Internet
Author: User
Tags repetition

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

The idea of the LIS algorithm is:

Set to exist sequence A.

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

② If there are two elements, then if a[1]>a[0], the length of the longest ascending subsequence is 2,a[1] is the last element of the longest ascending subsequence, and if a[1]<a[0], the length of the longest ascending subsequence is 1,a[0] and a[1] are The last element of its longest ascending sub-sequence.

③ If a three element is used, then if A[2]>A[0],A[2]>A[1], then a[2] can be the last element of the longest ascending subsequence of a[0] or a[1]. Which sequence to choose depends on a[0],a[1] which sequence is longer.

The ④ expands to n elements, which is the length of the longest ascending subsequence with A[n] as the last element.

Defines two arrays, one is a and the other is B.

A holds the original data, B[i] is the length of the longest ascending subsequence 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<a.length;i++) {int countmax=0;for (int j=0;j<i;j++) {if (A[i]>a[j]&&b[j]>countmax) {  Countmax=b[j];   The length of the subsequence with the longest sub-sequence is}}b[i]=countmax+1,      while the number of elements is smaller than a[i]. A[i] corresponds to the oldest oldest sequence length is}}}

Second, doing drill formation

Title Description:

In high school, every morning the school to organize students and teachers to run to exercise the body, whenever the doing drill to blow, we began to run downstairs, and then the height of the line in front of the team, taller than the queue at the end of the team. Suddenly, one day doing drill in charge of an idea, want to change formation, is when everyone from upstairs ran down, all the students are randomly accounted for in a row, and then doing drill in charge from the team to take out part of the students, so that the remaining students in the team's height from the back, is a first rise after the decline of the "peak" shape. It is said that this shape can bring good luck to everyone, and hope that everyone on the road of learning to climb the peak. (Note that only one side of the mountain can meet the conditions, such as 1, 1, 2, 2, 1 are eligible)

Input:

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

Output:

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

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, you can consider this:

First, use the LIS to find the length of the longest ascending subsequence at the end of each element, then reverse the array, and then use the LIS to find the length of the longest ascending sub-sequence ending with each element.

Get two arrays of B1,B2.

One of the b1,b2 corresponding to the sum minus the repetition is the longest ' peak '.

public class Peak {public static void main (string[] args) {int n;int re;do{   Scanner in = new Scanner (system.in);   n = In.nextint ();} while (n<0| | n>100000); int []a = new Int[n];                     Original array int []ar = new Int[n];                    Reverse array Scanner in = new Scanner (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);              The longest ascending subsequence B2=reverse.reverse (B2) of the inverse array is solved;         Reverse the longest ascending subsequence of an array of reversed order so that it corresponds to the longest ascending subsequence of the original array, adding RE = Result.result (B1, B2); System.out.print (re);}}



class Result{public static int result (int[] a,int[] b) {int max=0;int[] c = new Int[a.length];for (int i=0;i<a.length;i++ ) {c[i]=a[i]+b[i];} Arrays.sort (c); max=c[c.length-1]-1;//corresponds to the sum of the longest, minus the repetition of a person return A.length-max;}

Implementation of LIS algorithm in Java, doing drill formation problem

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.