Using Java to implement the LIS algorithm, the problem of drill formation _java

Source: Internet
Author: User

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

The idea of the LIS algorithm is:

Set existence sequence A.

① If there is only one element, then 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] as the last element of the longest ascending subsequence, and if A[1]<a[0], the longest ascending subsequence is 1,a[0] and a[1] The last element of its longest ascending subsequence.

③ If there are three elements, then if A[2]>A[0],A[2]>A[1], then a[2] can act as the last element of the longest ascending subsequence of a[0] or a[1. Then choose which sequence to look at a[0],a[1] which sequence is longer.

④ expands to n elements to see the length of the longest ascending subsequence of A[n] as the last element.

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

A holds the original data, and B[i] stores the length of the longest ascending sequence ending with the 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];   Record the number of element values smaller than a[i] but correspond to the longest subsequence length of the substring}
       
      b[i]=countmax+1;     A[i] corresponds to the eldest child sequence length is
    }}
   

Second, drill formation

Topic Description:

In high school, every morning the school will organize the teachers and students running to exercise the body, whenever the drill to blow, everyone began to run downstairs, and then the height of the short line in front of the team, height will be ranked at the end of the team. Suddenly, one day drill in charge of the idea, want to change the formation, that is, when everyone is running down from upstairs, all the students are randomly occupied in a row, then the drill in charge from the team to draw out a part of the students, so that the remaining students in the ranks of the previous look, is a first rise after the fall of the "mountain" shape. It is said that such a shape can bring good luck to everyone, I wish you on the road of learning to climb the peak. (note, the peak is only one side also meet the conditions, such as 1, 1, 2, 2, 1 are eligible)

Input:

The input may contain more than one test sample.
For each test case, the first line entered is an integer n (1<=n<=1000000): Represents the number of students that will be entered.
The second line you enter includes n integers: A positive integer representing the height of the student (CM), which is no higher than 200.

Output:

For each test case, the minimum number of students needed to be drawn out.

Sample input:

6

100 154 167 159 132 105

5

152 152 152 152 152

Sample output:

0

4

In the use of LIS to solve this problem, you can consider this:

First, ask for the length of the longest ascending subsequence at the end of each element from the front and back, then reverse the array, then use the LIS to find the length of the longest ascending subsequence at the end of each element.

Gets two array b1,b2.

The b1,b2 corresponds to add and subtract the repetition of one, which 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];          The 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 sequence of an array in reverse order to correspond to the longest ascending sequence of the original array add re = Result.result (B1, B2);
  System.out.print (re); }}<br><br><br><br> 
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;    Add the longest and subtract the duplicate of a person return
    A.length-max;
  }

The above is small series for everyone to bring the use of Java to implement the LIS algorithm, drill formation of the entire content of the problem, I hope to help you, a lot of support cloud Habitat Community ~

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.