結果

問題 No.33 アメーバがたくさん
ユーザー fujisufujisu
提出日時 2015-03-06 05:32:09
言語 Java21
(openjdk 21)
結果
AC  
実行時間 56 ms / 5,000 ms
コード長 3,624 bytes
コンパイル時間 2,462 ms
コンパイル使用メモリ 84,956 KB
実行使用メモリ 53,468 KB
最終ジャッジ日時 2023-10-24 17:45:27
合計ジャッジ時間 3,707 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 56 ms
53,452 KB
testcase_01 AC 56 ms
51,440 KB
testcase_02 AC 54 ms
53,452 KB
testcase_03 AC 53 ms
53,452 KB
testcase_04 AC 53 ms
53,452 KB
testcase_05 AC 55 ms
53,460 KB
testcase_06 AC 56 ms
53,468 KB
testcase_07 AC 55 ms
52,376 KB
testcase_08 AC 55 ms
53,460 KB
testcase_09 AC 55 ms
53,464 KB
testcase_10 AC 54 ms
52,324 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.IOException;
import java.util.InputMismatchException;
import java.util.TreeMap;
import java.util.TreeSet;

public class Main {
	void add(long key, long value, TreeMap<Long, Long> map) {
		if (map.get(key) == null) {
			map.put(key, value);
		} else {
			map.put(key, map.get(key) + value);
		}
	}

	void run() {
		MyScanner sc = new MyScanner();

		int n = sc.nextInt();
		long d = sc.nextInt();
		long t = sc.nextInt();
		long[] a = sc.nextLongArray(n);

		TreeMap<Long, TreeSet<Long>> map = new TreeMap<Long, TreeSet<Long>>();
		for (int i = 0; i < n; i++) {
			long rem = ((a[i] % d) + d) % d;
			if (map.get(rem) == null) {
				TreeSet<Long> l = new TreeSet<Long>();
				l.add(a[i]);
				map.put(rem, l);
			} else {
				map.get(rem).add(a[i]);
			}
		}

		long sum = 0;
		for (Long key : map.keySet()) {
			TreeSet<Long> set = map.get(key);

			TreeMap<Long, Long> array = new TreeMap<Long, Long>();
			for (Long pos : set) {
				long l = pos - t * d;
				long r = pos + t * d;
				add(l, 1, array);
				add(r + d, -1, array);
			}

			//-----------------------------------------------
			//			for (Integer k : array.keySet()) {
			//				System.out.print(k + " ");
			//			}
			//			System.out.println();
			//			for (Integer k : array.keySet()) {
			//				int v = array.get(k);
			//				System.out.print(v + " ");
			//			}
			//			System.out.println();
			//-----------------------------------------------

			long ameba = 0;
			long pre = -1;
			for (Long pos : array.keySet()) {
				if (0 < ameba) {
					sum += (pos - pre) / d;
				}

				long value = array.get(pos);
				ameba += value;
				pre = pos;
			}
		}

		System.out.println(sum);
	}

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

	public void mapDebug(int[][] a) {
		System.out.println("--------map display---------");
		for (int i = 0; i < a.length; i++) {
			for (int j = 0; j < a[i].length; j++) {
				System.out.printf("%3d ", a[i][j]);
			}
			System.out.println();
		}
		System.out.println("----------------------------" + '\n');
	}

	class MyScanner {
		int read() {
			try {
				return System.in.read();
			} catch (IOException e) {
				throw new InputMismatchException();
			}
		}

		boolean isSpaceChar(int c) {
			return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
		}

		boolean isEndline(int c) {
			return c == '\n' || c == '\r' || c == -1;
		}

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

		int[] nextIntArray(int n) {
			int[] array = new int[n];
			for (int i = 0; i < n; i++)
				array[i] = nextInt();
			return array;
		}

		long nextLong() {
			return Long.parseLong(next());
		}

		long[] nextLongArray(int n) {
			long[] array = new long[n];
			for (int i = 0; i < n; i++)
				array[i] = nextLong();
			return array;
		}

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

		double[] nextDoubleArray(int n) {
			double[] array = new double[n];
			for (int i = 0; i < n; i++)
				array[i] = nextDouble();
			return array;
		}

		String next() {
			int c = read();
			while (isSpaceChar(c))
				c = read();
			StringBuilder res = new StringBuilder();
			do {
				res.appendCodePoint(c);
				c = read();
			} while (!isSpaceChar(c));
			return res.toString();
		}

		String[] nextStringArray(int n) {
			String[] array = new String[n];
			for (int i = 0; i < n; i++)
				array[i] = next();

			return array;
		}

		String nextLine() {
			int c = read();
			while (isEndline(c))
				c = read();
			StringBuilder res = new StringBuilder();
			do {
				res.appendCodePoint(c);
				c = read();
			} while (!isEndline(c));
			return res.toString();
		}
	}
}
0