Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.*;
- public class Main {
- public static void main(String[] args) {
- InputStream inputStream = System.in;
- OutputStream outputStream = System.out;
- InputReader in = new InputReader(inputStream);
- PrintWriter out = new PrintWriter(outputStream);
- Solver solver = new Solver();
- solver.solve(in, out);
- out.close();
- }
- static class Solver {
- public void solve(InputReader in, PrintWriter out) {
- int n = in.nextInt();
- int[] a = new int[n];
- int[] index = new int[n + 1];
- for (int i = 0; i < n; ++i) {
- a[i] = in.nextInt();
- index[a[i]] = i;
- }
- if (n == 1) {
- out.println(1);
- return;
- }
- int ind = index[1], moves = 0;
- while (true) {
- if (index[a[ind] + 1] < ind) {
- ++moves;
- }
- ind = index[a[ind] + 1];
- if (a[ind] == n) {
- out.println(moves + 1);
- return;
- }
- }
- }
- } // wubba lubba dub dub
- static class InputReader {
- public BufferedReader reader;
- public StringTokenizer tokenizer;
- public InputReader(InputStream stream) {
- reader = new BufferedReader(new InputStreamReader(stream), 32768);
- tokenizer = null;
- }
- public String next() {
- while (tokenizer == null || !tokenizer.hasMoreElements()) {
- try {
- tokenizer = new StringTokenizer(reader.readLine());
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
- }
- return tokenizer.nextToken();
- }
- public int nextInt() {
- return Integer.parseInt(next());
- }
- public long nextLong() {
- return Long.parseLong(next());
- }
- public double nextDouble() {
- return Double.parseDouble(next());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement