Cod sursa(job #3241951)

Utilizator obsidianMidnight Majesty obsidian Data 6 septembrie 2024 17:09:38
Problema Subsir crescator maximal Scor 50
Compilator java Status done
Runda Arhiva educationala Marime 3.08 kb
import java.io.*;
import java.util.*;

public class Main {
    static final String INPUT_FILE = "scmax.in";
    static final String OUTPUT_FILE = "scmax.out";

    public static class TokenizedReader {
        private final BufferedReader reader;
        private StringTokenizer tokenizer;

        TokenizedReader(String filePath) throws FileNotFoundException {
            reader = new BufferedReader(new FileReader(filePath));
        }

        private String nextToken() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        private int nextInt() {
            return Integer.parseInt(nextToken());
        }

        public void close() throws IOException {
            reader.close();
        }
    }

    public static void main(String[] args) throws IOException {
        TokenizedReader reader = new TokenizedReader(INPUT_FILE);
        PrintWriter writer = new PrintWriter(OUTPUT_FILE);
        solve(reader, writer);
        reader.close();
        writer.flush();
        writer.close();
    }

    static class FenwickTreeMax {
        private final int[] nodes;
        private final int n;

        public FenwickTreeMax(int n) {
            this.n = n;
            this.nodes = new int[n + 1];
            Arrays.fill(this.nodes, 0);
        }

        public int queryMax(int index) {
            int max = 0;
            while (index > 0) {
                max = Math.max(max, nodes[index]);
                index -= (index & (-index));
            }
            return max;
        }

        public void update(int index, int value) {
            while (index <= n) {
                nodes[index] = Math.max(nodes[index], value);
                index += (index & (-index));
            }
        }
    }

    public static void solve(TokenizedReader reader,
                             PrintWriter writer) {
        int n = reader.nextInt();
        int[] a = new int[n];
        int[] sorted = new int[n];
        for (int i = 0; i < n; ++i) {
            a[i] = sorted[i] = reader.nextInt();
        }
        Arrays.sort(sorted);
        // remove duplicates from sorted
        int cntUnique = 1;    // skip first
        for (int i = 1; i < n; ++i) {
            if (sorted[i] != sorted[cntUnique - 1]) {
                sorted[cntUnique] = sorted[i];
                ++cntUnique;
            }
        }

        for (int i = 0; i < n; ++i) {
            a[i] = Arrays.binarySearch(sorted, 0, cntUnique, a[i]) + 1;
        }

        FenwickTreeMax fenwickTree = new FenwickTreeMax(n);
        for (int i = 0; i < n; ++i) {
            int currBest = 1 + fenwickTree.queryMax(a[i] - 1);
            fenwickTree.update(a[i], currBest);
        }

        int best = fenwickTree.queryMax(n);
        writer.println(best);
    }
}