Submission #1221794


Source Code Expand

import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
	public static void main(String[] args) {
		InputStream inputStream = System.in;
		OutputStream outputStream = System.out;
		FastScanner in = new FastScanner(inputStream);
		PrintWriter out = new PrintWriter(outputStream);
		TaskA solver = new TaskA();
		solver.solve(1, in, out);
		out.close();
	}

	static class TaskA {
		public void solve(int testNumber, FastScanner in, PrintWriter out) {
			int n = in.nextInt();
			int[] a = new int[n];
			for (int i = 0; i < n; i++) {
				a[i] = in.nextInt();
			}
			int ans = 1;
			int state = 0;
			for (int i = 1; i < n; i++) {
				if (state == 0) {
					if (a[i] < a[i - 1]) {
						state = 1;
					}
					if (a[i] > a[i - 1]) {
						state = 2;
					}
				} else if (state == 1) {
					if (a[i] > a[i - 1]) {
						state = 0;
						++ans;
					}
				} else {
					if (a[i] < a[i - 1]) {
						state = 0;
						++ans;
					}
				}
			}
			out.println(ans);
		}

	}

	static class FastScanner {
		private BufferedReader in;
		private StringTokenizer st;

		public FastScanner(InputStream stream) {
			in = new BufferedReader(new InputStreamReader(stream));
		}

		public String next() {
			while (st == null || !st.hasMoreTokens()) {
				try {
					String rl = in.readLine();
					if (rl == null) {
						return null;
					}
					st = new StringTokenizer(rl);
				} catch (IOException e) {
					throw new RuntimeException(e);
				}
			}
			return st.nextToken();
		}

		public int nextInt() {
			return Integer.parseInt(next());
		}

	}
}

Submission Info

Submission Time
Task A - Sorted Arrays
User fetetriste
Language Java8 (OpenJDK 1.8.0)
Score 300
Code Size 1935 Byte
Status AC
Exec Time 171 ms
Memory 36884 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 300 / 300
Status
AC × 3
AC × 17
Set Name Test Cases
Sample sample_01.txt, sample_02.txt, sample_03.txt
All sample_01.txt, sample_02.txt, sample_03.txt, sample_01.txt, sample_02.txt, sample_03.txt, subtask_1_01.txt, subtask_1_02.txt, subtask_1_03.txt, subtask_1_04.txt, subtask_1_05.txt, subtask_1_06.txt, subtask_1_07.txt, subtask_1_08.txt, subtask_1_09.txt, subtask_1_10.txt, subtask_1_11.txt
Case Name Status Exec Time Memory
sample_01.txt AC 72 ms 21460 KB
sample_02.txt AC 101 ms 23380 KB
sample_03.txt AC 72 ms 20820 KB
subtask_1_01.txt AC 158 ms 33440 KB
subtask_1_02.txt AC 164 ms 28264 KB
subtask_1_03.txt AC 130 ms 26068 KB
subtask_1_04.txt AC 168 ms 33856 KB
subtask_1_05.txt AC 171 ms 36884 KB
subtask_1_06.txt AC 171 ms 35220 KB
subtask_1_07.txt AC 168 ms 35732 KB
subtask_1_08.txt AC 171 ms 35860 KB
subtask_1_09.txt AC 169 ms 35452 KB
subtask_1_10.txt AC 68 ms 19412 KB
subtask_1_11.txt AC 71 ms 20948 KB