結果

問題 No.596 郵便配達
ユーザー 37zigen37zigen
提出日時 2017-08-30 22:02:51
言語 Java21
(openjdk 21)
結果
RE  
実行時間 -
コード長 9,568 bytes
コンパイル時間 3,086 ms
コンパイル使用メモリ 83,280 KB
実行使用メモリ 402,724 KB
最終ジャッジ日時 2024-11-17 14:12:22
合計ジャッジ時間 13,964 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 498 ms
94,920 KB
testcase_01 AC 421 ms
94,428 KB
testcase_02 RE -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 56 ms
49,752 KB
testcase_08 AC 828 ms
114,284 KB
testcase_09 RE -
testcase_10 AC 53 ms
49,992 KB
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 AC 1,331 ms
402,552 KB
testcase_21 AC 1,354 ms
402,724 KB
testcase_22 AC 1,354 ms
402,572 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.NoSuchElementException;
import java.util.Random;

public class Main {
	public static void main(String[] args) throws FileNotFoundException {
		new Main().run();
	}

	class BruteForce {
		int[] l, r;

		public BruteForce(int[] l_, int[] r_) {
			l = l_;
			r = r_;
		}

		int calc() {
			ArrayDeque<State> que = new ArrayDeque<>();
			for (int i = 0; i < MAX; ++i) {
				que.add(new State(i, l, r));
			}
			while (!que.isEmpty()) {
				State s = que.poll();
				if (s.cnt > MAX * 2) {
					continue;
				}
				if (s.ok()) {
					return s.cnt;
				}
				for (int dx = -1; dx <= 1; dx += 2) {
					if (0 <= s.cur + dx && s.cur + dx < MAX) {
						que.addLast(s.move(s.cur + dx));
					}
				}
			}
			throw new AssertionError();
		}

		class State {
			int cur;
			boolean[] vis;
			int[] l;
			int[] r;
			int cnt = 0;

			public State(int cur_, int[] l_, int[] r_) {
				cur = cur_;
				l = Arrays.copyOf(l_, l_.length);
				r = Arrays.copyOf(r_, r_.length);
				vis = new boolean[MAX];
				vis[cur] = true;
				if (l[cur] == cur) {
					l[cur] = -1;
				}
				if (r[cur] == cur) {
					r[cur] = -1;
				}
			}

			State move(int dst) {
				State ret = new State(cur, l, r);
				ret.vis = Arrays.copyOf(vis, vis.length);
				ret.cur = dst;
				ret.vis[ret.cur] = true;
				ret.cnt = cnt;
				ret.l = Arrays.copyOf(l, l.length);
				ret.r = Arrays.copyOf(r, r.length);
				for (int i = 0; i < MAX; ++i) {
					if (ret.vis[i] && ret.l[i] == dst) {
						ret.l[i] = -1;
					}
					if (ret.vis[i] && ret.r[i] == dst) {
						ret.r[i] = -1;
					}
				}
				ret.cnt++;
				return ret;

			}

			boolean ok() {
				for (int i = 0; i < MAX; ++i) {
					if (!(r[i] == -1 && l[i] == -1))
						return false;
				}
				return true;
			}
		}
	}

	int MAX = 0;

	int solve(int[] l, int[] r) {
		int rEdge1 = Integer.MAX_VALUE / 10;
		int rEdge2 = -Integer.MAX_VALUE / 10;
		int lEdge1 = Integer.MAX_VALUE / 10;
		int lEdge2 = -Integer.MAX_VALUE / 10;
		for (int i = 0; i < MAX; ++i) {
			if (r[i] != -1) {
				rEdge1 = i;
				break;
			}
		}
		for (int i = MAX - 1; i >= 0; --i) {
			if (r[i] != -1) {
				rEdge2 = r[i];
				break;
			}
		}
		for (int i = MAX - 1; i >= 0; --i) {
			if (l[i] != -1) {
				lEdge2 = i;
				break;
			}
		}
		for (int i = 0; i < MAX; ++i) {
			if (l[i] != -1) {
				lEdge1 = l[i];
				break;
			}
		}
		for (int i = Math.min(lEdge1, rEdge1) + 1; i < Math.max(lEdge2, rEdge2); ++i) {
			if (l[i] == i) {
				l[i] = -1;
			}
			if (r[i] == i) {
				r[i] = -1;
			}
		}

		int[] from = new int[MAX];
		Arrays.fill(from, -1);
		for (int i = 0; i < l.length; ++i) {
			if (l[i] != -1) {
				from[l[i]] = i;
			}
		}
		int[] middle = new int[MAX];
		for (int i = MAX - 1; i >= 0; --i) {
			if (l[i] != i && l[i] != -1) {
				++middle[i - 1];
				if (l[i] > 0) {
					--middle[l[i] - 1];
				}
			}
			if (i + 1 < MAX) {
				middle[i] = middle[i + 1] + middle[i];
			}
		}
		int[][] s = new int[2][MAX];
		for (int i = 0; i < s.length; ++i) {
			for (int j = 0; j < s[i].length; ++j) {
				s[i][j] = Integer.MAX_VALUE / 10;
			}
		}
		{

			for (int i = Math.min(rEdge1, lEdge1); i <= Math.max(rEdge2, lEdge2); ++i) {
				if (rEdge1 <= i && i < lEdge1) {
					s[0][i] = i - rEdge1;
				}
				if (i < lEdge1 || (l[i] != -1 && middle[i] == 0)) {
					s[0][i] = Math.min(s[0][i], 2 * Math.abs(i - Math.min(lEdge1, rEdge1)));
				}
				if (l[i] == i || (middle[i] == 0 && l[i] == -1 && from[i] == -1)) {
					if (i == 0) {
						s[1][i] = 0;
						s[0][i] = 0;
					} else {
						s[1][i] = Math.min(s[1][i], Math.min(s[1][i - 1]+ 3, s[0][i - 1] + 1));
						s[0][i] = Math.min(s[0][i], Math.min(s[1][i - 1] + 3, s[0][i - 1] + 1));
					}
				} else if (from[i] != -1 && i != 0) {
					s[1][i] = Math.min(s[1][i], Math.min(s[0][i - 1] + 1, s[1][i - 1] + 3));
				} else if (l[i] != -1) {
					if (i == 0) {
						throw new AssertionError();
					}
					s[1][i] = Math.min(s[1][i], s[1][i - 1] + 3);
					s[0][i] = Math.min(s[0][i], s[1][i - 1] + 3);
				} else if ((middle[i] > 0 || from[i] != -1 || l[i] != -1) && i != 0) {
					s[1][i] = Math.min(s[1][i], s[1][i - 1] + 3);
				} else {

				}
			}
		}
		int ans = 2 * (Math.max(lEdge2, rEdge2) - Math.min(lEdge1, rEdge1));
		for (int i = 0; i < MAX; ++i) {
			ans = Math.min(ans,
					s[0][i] + 2 * Math.abs(i - Math.max(lEdge2, rEdge2)) - (i == Math.max(lEdge2, rEdge2) ? 0 : 1));
		}
		return ans;
	}

	void random() {
		Random rand = new Random();
		int MAX_ = rand.nextInt(10) + 1;
		int m = rand.nextInt(MAX_ * 20) + 1;
		int[] x = new int[m];
		int[] d = new int[m];
		int[][] y = new int[m][2];
		for (int i = 0; i < m; ++i) {
			x[i] = rand.nextInt(MAX_);
			d[i] = 2;
			for (int j = 0; j < 2; ++j) {
				y[i][j] = rand.nextInt(MAX_);
			}
		}
		tr("Comparison", solver(MAX_, m, x, d, y), new BruteForce(l(MAX_, m, x, d, y), r(MAX_, m, x, d, y)).calc());
		if (solver(MAX_, m, x, d, y) != new BruteForce(l(MAX_, m, x, d, y), r(MAX_, m, x, d, y)).calc()) {
			tr(MAX_);
			tr(l(MAX_, m, x, d, y));
			tr(r(MAX_, m, x, d, y));
			System.exit(0);
		}

	}

	void run() {
		Scanner sc = new Scanner();
		int MAX_ = sc.nextInt();
		int m = sc.nextInt();
		int[] x = new int[m];
		int[] d = new int[m];
		int[][] y = new int[m][2];
		for (int i = 0; i < m; ++i) {
			x[i] = sc.nextInt();
			d[i] = sc.nextInt();
			for (int j = 0; j < d[i]; ++j) {
				y[i][j] = sc.nextInt();
			}
		}
		System.out.println(solver(MAX_, m, x, d, y));
	}

	int solver(int MAX_, int m, int[] x, int[] d, int[][] y) {
		MAX = MAX_;
		int[] r = r(MAX_, m, x, d, y);
		int[] l = l(MAX_, m, x, d, y);
		int[] invl = new int[MAX];
		int[] invr = new int[MAX];
		Arrays.fill(invl, -1);
		Arrays.fill(invr, -1);
		for (int i = 0; i < MAX; ++i) {
			if (l[i] != -1) {
				int src = (MAX - 1) - i;
				int dst = (MAX - 1) - l[i];
				invr[src] = dst;
			}
			if (r[i] != -1) {
				int src = (MAX - 1) - i;
				int dst = (MAX - 1) - r[i];
				invl[src] = dst;
			}
		}
		return Math.min(solve(l, r), solve(invl, invr));
	}

	int[] r(int MAX_, int m, int[] x, int[] d, int[][] y) {
		int[] r = new int[MAX];
		int[] l = new int[MAX];

		Arrays.fill(r, -1);
		Arrays.fill(l, -1);
		for (int i = 0; i < m; ++i) {
			int ma = x[i] - 1;
			int mi = x[i] + 1;
			for (int j = 0; j < d[i]; ++j) {
				ma = Math.max(ma, y[i][j]);
				mi = Math.min(mi, y[i][j]);
			}
			if (ma >= x[i]) {
				r[x[i]] = ma;
			}
			if (mi <= x[i]) {
				l[x[i]] = mi;
			}
		}

		{
			int ma = -1;
			for (int i = 0; i < MAX; ++i) {
				if (r[i] == -1)
					continue;
				if (ma < r[i]) {
					ma = r[i];
				} else if (ma >= r[i]) {
					r[i] = -1;
				}
			}
		}
		{
			int mi = MAX * 10;
			for (int i = MAX - 1; i >= 0; --i) {
				if (l[i] == -1)
					continue;
				if (mi > l[i]) {
					mi = l[i];
				} else if (mi <= l[i]) {
					l[i] = -1;
				}
			}
		}
		return r;
	}

	int[] l(int MAX_, int m, int[] x, int[] d, int[][] y) {
		int[] r = new int[MAX];
		int[] l = new int[MAX];

		Arrays.fill(r, -1);
		Arrays.fill(l, -1);
		for (int i = 0; i < m; ++i) {
			int ma = x[i] - 1;
			int mi = x[i] + 1;
			for (int j = 0; j < d[i]; ++j) {
				ma = Math.max(ma, y[i][j]);
				mi = Math.min(mi, y[i][j]);
			}
			if (ma >= x[i]) {
				r[x[i]] = ma;
			}
			if (mi <= x[i]) {
				l[x[i]] = mi;
			}
		}

		{
			int ma = -1;
			for (int i = 0; i < MAX; ++i) {
				if (r[i] == -1)
					continue;
				if (ma < r[i]) {
					ma = r[i];
				} else if (ma >= r[i]) {
					r[i] = -1;
				}
			}
		}
		{
			int mi = MAX * 10;
			for (int i = MAX - 1; i >= 0; --i) {
				if (l[i] == -1)
					continue;
				if (mi > l[i]) {
					mi = l[i];
				} else if (mi <= l[i]) {
					l[i] = -1;
				}
			}
		}
		return l;
	}

	class Scanner {
		private final InputStream in = System.in;
		private final byte[] buffer = new byte[1024];
		private int ptr = 0;
		private int buflen = 0;

		private boolean hasNextByte() {
			if (ptr < buflen) {
				return true;
			} else {
				ptr = 0;
				try {
					buflen = in.read(buffer);
				} catch (IOException e) {
					e.printStackTrace();
				}
				if (buflen <= 0) {
					return false;
				}
			}
			return true;
		}

		private int readByte() {
			if (hasNextByte())
				return buffer[ptr++];
			else
				return -1;
		}

		private boolean isPrintableChar(int c) {
			return 33 <= c && c <= 126;
		}

		private void skipUnprintable() {
			while (hasNextByte() && !isPrintableChar(buffer[ptr]))
				ptr++;
		}

		public boolean hasNext() {
			skipUnprintable();
			return hasNextByte();
		}

		public String next() {
			if (!hasNext())
				throw new NoSuchElementException();
			StringBuilder sb = new StringBuilder();
			int b = readByte();
			while (isPrintableChar(b)) {
				sb.appendCodePoint(b);
				b = readByte();
			}
			return sb.toString();
		}

		public long nextLong() {
			if (!hasNext())
				throw new NoSuchElementException();
			long n = 0;
			boolean minus = false;
			int b = readByte();
			if (b == '-') {
				minus = true;
				b = readByte();
			}
			if (b < '0' || '9' < b) {
				throw new NumberFormatException();
			}
			while (true) {
				if ('0' <= b && b <= '9') {
					n *= 10;
					n += b - '0';
				} else if (b == -1 || !isPrintableChar(b)) {
					return minus ? -n : n;
				} else {
					throw new NumberFormatException();
				}
				b = readByte();
			}
		}

		public int nextInt() {
			return (int) nextLong();
		}
	}

	static void tr(Object... objects) {
		System.out.println(Arrays.deepToString(objects));
	}
}
0