Submission #5124652


Source Code Expand

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

/**
 * Support only ASCII code (8-bit char) for IO.
 */
public class Main {
	private static final int MOD = (int) 1E9 + 7;
	private static final int BUFFER_SIZE = (1 << 13);
	private static final int WHITE_SPACE = 32;
	private static final byte[] IN_BUFFER = new byte[BUFFER_SIZE];
	private static final byte[] OUT_BUFFER = new byte[BUFFER_SIZE];

	private int inNextByte;
	private int inNextIndex;
	private int inReadByteCount;
	private int outNextIndex;
	private InputStream in = System.in;
	private OutputStream out = System.out;
	private PrintStream err = System.err;

	private boolean isShowDebugInfo;
	private boolean isRunOnEditor;
	private boolean isLocalFileInput;
	private boolean isLocalFileOutput;

	public Main(String[] args) {
		if (args != null) {
			if (args.length > 0) {
				isShowDebugInfo = Boolean.parseBoolean(args[0]);
			}
			if (args.length > 1) {
				isRunOnEditor = Boolean.parseBoolean(args[1]);
			}
			if (args.length > 2) {
				isLocalFileInput = Boolean.parseBoolean(args[2]);
			}
			if (args.length > 3) {
				isLocalFileOutput = Boolean.parseBoolean(args[3]);
			}
		}
	}

	private void start() throws Exception {
		if (isLocalFileInput) {
			char fs = File.separatorChar;
			String root = new File("").getAbsolutePath();
			String inPath = String.format("%s%c%s", root, fs, "in.txt");
			if (isRunOnEditor) {
				inPath = String.format("%s%c%s%c%s", root, fs, "src", fs, "in.txt");
			}
			in = new FileInputStream(inPath);
			if (isShowDebugInfo) {
				err.printf("Input: %s\n", inPath);
			}
		}
		else if (isShowDebugInfo) {
			err.println("Input: Console\n");
		}

		if (isLocalFileOutput) {
			char fs = File.separatorChar;
			String root = new File("").getAbsolutePath();
			String outPath = String.format("%s%c%s", root, fs, "out.txt");
			if (isRunOnEditor) {
				outPath = String.format("%s%c%s%c%s", root, fs, "src", fs, "out.txt");
			}
			out = new FileOutputStream(outPath);
			if (isShowDebugInfo) {
				err.printf("Output: %s\n", outPath);
			}
		}
		else if (isShowDebugInfo) {
			err.println("Output: Console\n");
		}

		long now = 0;
		if (isShowDebugInfo) {
			now = System.currentTimeMillis();
		}

		readByte();
		solve();
		in.close();
		flushOutBuf();

		if (isShowDebugInfo) {
			long elapsed = System.currentTimeMillis() - now;
			err.printf("\nSolve completed in %.3f s\n", elapsed / 1000.0);
		}
	}

	public static void main(String[] args) {
		try {
			new Main(args).start();
		}
		catch (Exception e) {
			e.printStackTrace();
		}
	}

	private int readByte() throws IOException {
		if (inNextIndex >= inReadByteCount) {
			inReadByteCount = in.read(IN_BUFFER, 0, BUFFER_SIZE);
			if (inReadByteCount == -1) {
				return (inNextByte = -1);
			}
			inNextIndex = 0;
		}
		return (inNextByte = IN_BUFFER[inNextIndex++]);
	}

	private char nc() throws IOException {
		while (inNextByte <= WHITE_SPACE) {
			readByte();
		}
		char res = (char) inNextByte;
		readByte();
		return res;
	}

	private int ni() throws IOException {
		int res = 0;
		while (inNextByte <= WHITE_SPACE) {
			readByte();
		}
		boolean minus = (inNextByte == '-');
		if (minus) {
			readByte();
		}
		if (inNextByte < '0' || inNextByte > '9') {
			throw new RuntimeException();
		}
		do {
			res = (res << 1) + (res << 3) + inNextByte - '0';
		}
		while (readByte() >= '0' && inNextByte <= '9');

		return minus ? -res : res;
	}

	private long nl() throws IOException {
		long res = 0;
		while (inNextByte <= WHITE_SPACE) {
			readByte();
		}
		boolean minus = (inNextByte == '-');
		if (minus) {
			readByte();
		}
		if (inNextByte < '0' || inNextByte > '9') {
			throw new RuntimeException();
		}
		do {
			res = (res << 1) + (res << 3) + inNextByte - '0';
		}
		while (readByte() >= '0' && inNextByte <= '9');

		return minus ? -res : res;
	}

	private double nd() throws IOException {
		double pre = 0.0, suf = 0.0, div = 1.0;
		while (inNextByte <= WHITE_SPACE) {
			readByte();
		}
		boolean minus = (inNextByte == '-');
		if (minus) {
			readByte();
		}
		if (inNextByte < '0' || inNextByte > '9') {
			throw new RuntimeException();
		}
		do {
			pre = 10 * pre + (inNextByte - '0');
		}
		while (readByte() >= '0' && inNextByte <= '9');

		if (inNextByte == '.') {
			while (readByte() >= '0' && inNextByte <= '9') {
				suf += (inNextByte - '0') / (div *= 10);
			}
		}
		return minus ? -(pre + suf) : (pre + suf);
	}

	private String ns() throws IOException {
		while (inNextByte <= WHITE_SPACE) {
			readByte();
		}
		StringBuilder sb = new StringBuilder();
		while (inNextByte > WHITE_SPACE) {
			sb.append((char) inNextByte);
			readByte();
		}
		return sb.toString();
	}

	private char[] nc(int n) throws IOException {
		char a[] = new char[n];
		for (int i = 0; i < n; ++i) {
			a[i] = nc();
		}
		return a;
	}

	private char[][] nc(int r, int c) throws IOException {
		char a[][] = new char[r][c];
		for (int i = 0; i < r; ++i) {
			a[i] = nc(c);
		}
		return a;
	}

	private int[] ni(int n) throws IOException {
		int a[] = new int[n];
		for (int i = 0; i < n; ++i) {
			a[i] = ni();
		}
		return a;
	}

	private int[][] ni(int r, int c) throws IOException {
		int a[][] = new int[r][c];
		for (int i = 0; i < r; ++i) {
			a[i] = ni(c);
		}
		return a;
	}

	private long[] nl(int n) throws IOException {
		long a[] = new long[n];
		for (int i = 0; i < n; ++i) {
			a[i] = nl();
		}
		return a;
	}

	private long[][] nl(int r, int c) throws IOException {
		long a[][] = new long[r][c];
		for (int i = 0; i < r; ++i) {
			a[i] = nl(c);
		}
		return a;
	}

	private double[] nd(int n) throws IOException {
		double a[] = new double[n];
		for (int i = 0; i < n; ++i) {
			a[i] = nd();
		}
		return a;
	}

	private double[][] nd(int r, int c) throws IOException {
		double a[][] = new double[r][c];
		for (int i = 0; i < r; ++i) {
			a[i] = nd(c);
		}
		return a;
	}

	private String[] ns(int n) throws IOException {
		String a[] = new String[n];
		for (int i = 0; i < n; ++i) {
			a[i] = ns();
		}
		return a;
	}

	private String[][] ns(int r, int c) throws IOException {
		String a[][] = new String[r][c];
		for (int i = 0; i < r; ++i) {
			a[i] = ns(c);
		}
		return a;
	}

	private void flushOutBuf() {
		try {
			if (outNextIndex <= 0) {
				return;
			}
			out.write(OUT_BUFFER, 0, outNextIndex);
			out.flush();
			outNextIndex = 0;
		}
		catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void print(String s) {
		if (s == null) {
			s = "null";
		}
		for (int i = 0, N = s.length(); i < N; ++i) {
			OUT_BUFFER[outNextIndex++] = (byte) s.charAt(i);
			if (outNextIndex >= BUFFER_SIZE) {
				flushOutBuf();
			}
		}
	}

	private void println(String s) {
		print(s);
		print('\n');
	}

	private void print(Object obj) {
		if (obj == null) {
			print("null");
		}
		else {
			print(obj.toString());
		}
	}

	private void println(Object obj) {
		print(obj);
		print('\n');
	}

	private void print(String format, Object... args) {
		if (args != null) {
			format = String.format(format, args);
		}
		print(format);
	}

	private void println(String format, Object... args) {
		if (args != null) {
			format = String.format(format, args);
		}
		println(format);
	}

	private void solve() throws Exception {
		int N = ni() - 1;
		char[] a = nc(N + 1);
		char W = '.', B = '#';

		List<int[]> segments = new ArrayList<>();
		int wCount = 0, bCount = 0;

		for (int i = 0; i <= N; ++i) {
			if (a[i] == B) {
				++bCount;
				if (wCount > 0) {
					segments.add(new int[] {W, wCount});
					wCount = 0;
				}
				if (i == N) {
					segments.add(new int[] {B, bCount});
				}
			}
			else {
				++wCount;
				if (bCount > 0) {
					segments.add(new int[] {B, bCount});
					bCount = 0;
				}
				if (i == N) {
					segments.add(new int[] {W, wCount});
				}
			}
		}

		int res = 0;

		for (int i = 0, C = segments.size() - 1; i <= C; ++i) {
			int[] now = segments.get(i);
			if (now[0] == B && i < C) {
				int[] next = segments.get(i + 1);
				res += Math.min(now[1], next[1]);
			}
		}

		println(res);
	}
}

//javac Main.java & java Main true false true false
//javac Main.java ; java Main true false true false

Submission Info

Submission Time
Task C - Stones
User Compet
Language Java8 (OpenJDK 1.8.0)
Score 0
Code Size 8527 Byte
Status WA
Exec Time 117 ms
Memory 28116 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 300
Status
AC × 3
AC × 19
WA × 6
Set Name Test Cases
Sample s1.txt, s2.txt, s3.txt
All 01.txt, 02.txt, 03.txt, 04.txt, 05.txt, 06.txt, 07.txt, 08.txt, 09.txt, 10.txt, 11.txt, 12.txt, 13.txt, 14.txt, 15.txt, 16.txt, 17.txt, 18.txt, 19.txt, 20.txt, 21.txt, 22.txt, s1.txt, s2.txt, s3.txt
Case Name Status Exec Time Memory
01.txt WA 113 ms 27604 KiB
02.txt WA 109 ms 25684 KiB
03.txt WA 112 ms 25428 KiB
04.txt WA 104 ms 26964 KiB
05.txt WA 112 ms 24788 KiB
06.txt WA 112 ms 26196 KiB
07.txt AC 92 ms 21332 KiB
08.txt AC 92 ms 21844 KiB
09.txt AC 91 ms 20436 KiB
10.txt AC 92 ms 19028 KiB
11.txt AC 92 ms 21844 KiB
12.txt AC 93 ms 21332 KiB
13.txt AC 93 ms 19796 KiB
14.txt AC 83 ms 23252 KiB
15.txt AC 92 ms 18516 KiB
16.txt AC 91 ms 19924 KiB
17.txt AC 117 ms 28116 KiB
18.txt AC 108 ms 27732 KiB
19.txt AC 68 ms 18516 KiB
20.txt AC 67 ms 20564 KiB
21.txt AC 69 ms 20436 KiB
22.txt AC 68 ms 21076 KiB
s1.txt AC 69 ms 20052 KiB
s2.txt AC 67 ms 18260 KiB
s3.txt AC 68 ms 21204 KiB