Cod sursa(job #2461553)

Utilizator shantih1Alex S Hill shantih1 Data 25 septembrie 2019 20:23:32
Problema Subsir crescator maximal Scor 0
Compilator java Status done
Runda Arhiva educationala Marime 1.88 kb

import java.io.*;
import java.util.*;
import java.util.ArrayList;

public class Main {

    static int n;
    static int[] a = new int[100005];
    static ArrayList<Integer> v = new ArrayList<Integer>();
    
    public static void main (String[] args) {
        
        MyScanner sc = new MyScanner();
        out = new PrintWriter(new BufferedOutputStream(System.out));
        
        n = sc.nextInt();
        
        int i , b;
        for (i = 1; i <= n; i++) {
            
            b = sc.nextInt();
            addNmb (b);
        }
        
        out.println(v.size());
        
        out.close();
    }
    
    public static void addNmb (int x) {
        
        if(v.isEmpty())   {
            v.add(x);
            return;
        }
        
        int n = v.size(), p, step;
        for (step = 0; (1<<step) < n; step++);
        for (p = 0; step >= 0; step--)  {
            if ((p + (1<<step)) < n && v.get(p + (1<<step)) <= x)
                p += (1<<step);
        }
        
        if (p == n - 1 && x > v.get(n-1))  {
            v.add(x);
            return;
        }
        if (p == 0 && x < v.get(0)) {
            v.set(0, x);
            return;
        }
        
        v.set(p+1, x);
    }
    
    public static PrintWriter out;
    
    public static class MyScanner {
     
        BufferedReader br;
        StringTokenizer st;
        
        public MyScanner() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }
        
        String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }
        
        int nextInt () {
            return Integer.parseInt(next());
        }
    }
}