Submission #17510686


Source Code Expand

import java.io.*;
import java.math.*;
import java.util.*;
import java.util.stream.*;

public class Main {
	
	static final long INF = Long.MAX_VALUE / 5;
	
	long[] go(int[] as, int[] bs) {
		int n = as.length;
		long[] ret = new long[n + 1];
		HashMap<Integer, Integer> memo = new HashMap<>();
		int[] where = new int[n];
		boolean bad = false;
		for (int i = 0; i < n; i++) {
			if (bad) {
				ret[i + 1] = INF;
				continue;
			}
			Integer idx = memo.get(bs[i] - i);
			if (idx == null) {
				if (bs[i] == i + 1) {
					idx = -1;
				} else {
					bad = true;
					ret[i + 1] = INF;
					continue;
				}
			}
			where[i] = idx;
			memo.put(as[i] - i, i);
			if (i == 0) {
				ret[i + 1] = 1;
			} else {
				if (where[i] == where[i - 1]) {
					ret[i + 1] = ret[i] + 1;
				} else {
					ret[i + 1] = ret[i] + (i - where[i]);
				}
			}
		}
		return ret;
	}
	
	long go(int[] as, int[] bs, int left, int right) {
		int n = as.length;
		int[] asLeft = new int[n];
		int[] bsLeft = new int[n];
		for (int i = 0; i < n; i++) {
			asLeft[i] = as[i] - left;
			bsLeft[i] = bs[i] - left;
		}
		
		int[] asRight = new int[n];
		int[] bsRight = new int[n];
		for (int i = 0; i < n; i++) {
			asRight[i] = right - as[n - 1 - i];
			bsRight[i] = right - bs[n - 1 - i];
		}
		
		long[] ansL = go(asLeft, bsLeft);
		long[] ansR = go(asRight, bsRight);
		
		long ret = INF;
		for (int i = 0; i <= n; i++) {
			ret = Math.min(ret, ansL[i] + ansR[n - i]);
		}
		return ret;
	}

	void submit() {
		int n = nextInt();
		int l = nextInt();
		int[] as = new int[n + 2];
		int[] bs = new int[n + 2];
		as[0] = bs[0] = 0;
		as[n + 1] = bs[n + 1] = l + 1;
		for (int i = 1; i <= n; i++) {
			as[i] = nextInt();
		}
		for (int i = 1; i <= n; i++) {
			bs[i] = nextInt();
		}
		
		int last = 0;
		long outp = 0;
		for (int i = 1; i <= n + 1; i++) {
			if (as[i] == bs[i]) {
				if (i > last + 1) {
					long delta = go(Arrays.copyOfRange(as, last + 1, i), Arrays.copyOfRange(bs, last + 1, i), as[last], as[i]);
					if (delta >= INF) {
						out.println(-1);
						return;
					}
					outp += delta;
				}
				last = i;
			}
		}
		
		out.println(outp);
	}

	void test() {

	}

	void stress() {
		for (int tst = 0;; tst++) {
			if (false) {
				throw new AssertionError();
			}
			System.err.println(tst);
		}
	}

	Main() throws IOException {
		is = System.in;
		out = new PrintWriter(System.out);
		submit();
		// stress();
		// test();
		out.close();
	}

	static final Random rng = new Random();
	static final int C = 5;

	static int rand(int l, int r) {
		return l + rng.nextInt(r - l + 1);
	}

	public static void main(String[] args) throws IOException {
		new Main();
	}

	private InputStream is;
	PrintWriter out;

	private byte[] buf = new byte[1 << 14];
	private int bufSz = 0, bufPtr = 0;

	private int readByte() {
		if (bufSz == -1)
			throw new RuntimeException("Reading past EOF");
		if (bufPtr >= bufSz) {
			bufPtr = 0;
			try {
				bufSz = is.read(buf);
			} catch (IOException e) {
				throw new RuntimeException(e);
			}
			if (bufSz <= 0)
				return -1;
		}
		return buf[bufPtr++];
	}

	private boolean isTrash(int c) {
		return c < 33 || c > 126;
	}

	private int skip() {
		int b;
		while ((b = readByte()) != -1 && isTrash(b))
			;
		return b;
	}

	String nextToken() {
		int b = skip();
		StringBuilder sb = new StringBuilder();
		while (!isTrash(b)) {
			sb.appendCodePoint(b);
			b = readByte();
		}
		return sb.toString();
	}

	String nextString() {
		int b = readByte();
		StringBuilder sb = new StringBuilder();
		while (!isTrash(b) || b == ' ') {
			sb.appendCodePoint(b);
			b = readByte();
		}
		return sb.toString();
	}

	double nextDouble() {
		return Double.parseDouble(nextToken());
	}

	char nextChar() {
		return (char) skip();
	}

	int nextInt() {
		int ret = 0;
		int b = skip();
		if (b != '-' && (b < '0' || b > '9')) {
			throw new InputMismatchException();
		}
		boolean neg = false;
		if (b == '-') {
			neg = true;
			b = readByte();
		}
		while (true) {
			if (b >= '0' && b <= '9') {
				ret = ret * 10 + (b - '0');
			} else {
				if (b != -1 && !isTrash(b)) {
					throw new InputMismatchException();
				}
				return neg ? -ret : ret;
			}
			b = readByte();
		}
	}

	long nextLong() {
		long ret = 0;
		int b = skip();
		if (b != '-' && (b < '0' || b > '9')) {
			throw new InputMismatchException();
		}
		boolean neg = false;
		if (b == '-') {
			neg = true;
			b = readByte();
		}
		while (true) {
			if (b >= '0' && b <= '9') {
				ret = ret * 10 + (b - '0');
			} else {
				if (b != -1 && !isTrash(b)) {
					throw new InputMismatchException();
				}
				return neg ? -ret : ret;
			}
			b = readByte();
		}
	}
}

Submission Info

Submission Time
Task C - Penguin Skating
User mmaxio
Language Java (OpenJDK 1.8.0)
Score 700
Code Size 4889 Byte
Status AC
Exec Time 213 ms
Memory 42388 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 700 / 700
Status
AC × 3
AC × 43
Set Name Test Cases
Sample 00-sample-001.txt, 00-sample-002.txt, 00-sample-003.txt
All 00-sample-001.txt, 00-sample-002.txt, 00-sample-003.txt, 01-001.txt, 01-002.txt, 01-003.txt, 01-004.txt, 01-005.txt, 01-006.txt, 01-007.txt, 01-008.txt, 01-009.txt, 01-010.txt, 01-011.txt, 01-012.txt, 01-013.txt, 01-014.txt, 01-015.txt, 01-016.txt, 01-017.txt, 01-018.txt, 01-019.txt, 01-020.txt, 01-021.txt, 01-022.txt, 01-023.txt, 01-024.txt, 01-025.txt, 01-026.txt, 01-027.txt, 01-028.txt, 01-029.txt, 01-030.txt, 01-031.txt, 01-032.txt, 01-033.txt, 01-034.txt, 01-035.txt, 01-036.txt, 01-037.txt, 01-038.txt, 01-039.txt, 01-040.txt
Case Name Status Exec Time Memory
00-sample-001.txt AC 70 ms 25300 KiB
00-sample-002.txt AC 72 ms 25192 KiB
00-sample-003.txt AC 70 ms 25392 KiB
01-001.txt AC 66 ms 25096 KiB
01-002.txt AC 70 ms 25188 KiB
01-003.txt AC 111 ms 28024 KiB
01-004.txt AC 120 ms 31800 KiB
01-005.txt AC 134 ms 30276 KiB
01-006.txt AC 132 ms 29940 KiB
01-007.txt AC 138 ms 30948 KiB
01-008.txt AC 156 ms 34204 KiB
01-009.txt AC 167 ms 39932 KiB
01-010.txt AC 131 ms 31628 KiB
01-011.txt AC 176 ms 37076 KiB
01-012.txt AC 163 ms 36516 KiB
01-013.txt AC 121 ms 32484 KiB
01-014.txt AC 132 ms 32240 KiB
01-015.txt AC 174 ms 41804 KiB
01-016.txt AC 187 ms 40992 KiB
01-017.txt AC 194 ms 41284 KiB
01-018.txt AC 199 ms 41188 KiB
01-019.txt AC 191 ms 40236 KiB
01-020.txt AC 175 ms 38728 KiB
01-021.txt AC 187 ms 39384 KiB
01-022.txt AC 199 ms 38804 KiB
01-023.txt AC 175 ms 42388 KiB
01-024.txt AC 195 ms 40796 KiB
01-025.txt AC 195 ms 41440 KiB
01-026.txt AC 212 ms 39792 KiB
01-027.txt AC 180 ms 40544 KiB
01-028.txt AC 213 ms 40176 KiB
01-029.txt AC 210 ms 39280 KiB
01-030.txt AC 187 ms 39192 KiB
01-031.txt AC 191 ms 42100 KiB
01-032.txt AC 192 ms 42316 KiB
01-033.txt AC 200 ms 42384 KiB
01-034.txt AC 213 ms 41864 KiB
01-035.txt AC 165 ms 40628 KiB
01-036.txt AC 171 ms 39004 KiB
01-037.txt AC 191 ms 41048 KiB
01-038.txt AC 200 ms 40128 KiB
01-039.txt AC 199 ms 39404 KiB
01-040.txt AC 200 ms 39336 KiB