0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ABC - 071- A&B&C

Posted at

AtCoder ABC 071 A&B&C

AtCoder - 071

A問題

  • 2点間の距離の絶対値を比較する
	private void solveA() {
		int numX = nextInt();
		int numA = nextInt();
		int numB = nextInt();

		if (Math.abs(numX - numA) > Math.abs(numX - numB)) {
			out.println("B");
		} else {
			out.println("A");
		}

	}

B問題

  • 比較用に、'a' - 'z' のcharが詰まったsetを用意
  • 文字列をchar[]として読み込み
  • 初期化した中から読み込んだcharをremoveしていく
  • removeが終わったsetをchar[]に戻す
  • ソート(文字コード順になる)後出力
	private void solveB() {

		Set<Character> wk = IntStream.range('a', 'z' + 1).collect(() -> new TreeSet<Character>(),
				(t, i) -> t.add(new Character((char) i)),
				(t, u) -> t.addAll(t));

		char[] wkA = next().toCharArray();

		for (int j = 0; j < wkA.length; j++) {
			wk.remove(wkA[j]);
		}

		Character[] res = wk.stream().toArray(Character[]::new);

		if (res.length == 0) {
			out.println("None");
		} else {
			out.println(res[0]);
		}
	}

C問題

  1. 出現回数が2以上の棒の中のみ利用
    2. 出現回数が2未満の棒は方形を作れないので除外
  2. 利用可能な棒を長さでソートして、2つ×2個用意する
  • 値をMapに読み込みつつ出現回数をカウント
  • 出現回数2未満の棒を除外してListに変換して長さの昇順にソート
  • 長さ順に2個ずつ取得(2個ずつでないと辺を作れない)
  • 計4個取得出来たら終了
	private void solveC() {
		int numN = nextInt();

		Map<Integer, Integer> wk = IntStream.range(0, numN).map(i -> nextInt())
				.collect(() -> new HashMap<Integer, Integer>(), (t, i) -> {
					t.merge(i, 1, (oldV, newV) -> oldV + newV);
				}, (t, u) -> t.putAll(u));

		List<int[]> operatedWk = wk.entrySet().stream().filter(e -> e.getValue() > 1).collect(
				() -> new ArrayList<int[]>(),
				(t, i) -> {
					t.add(new int[] { i.getKey(), i.getValue() });
				},
				(t, u) -> {
					t.addAll(u);
				});

		Collections.sort(operatedWk, (x, y) -> Long.compare(x[0], y[0]));

		long radX = 0;
		long radY = 0;

		for (int j = operatedWk.size() - 1; j >= 0; j--) {
			for (int k = 0; k < 2; k++) {
				long radTmp = 0;
				if (operatedWk.get(j)[1] >= 2) {
					operatedWk.get(j)[1] = operatedWk.get(j)[1] - 2;
					radTmp = operatedWk.get(j)[0];
				}
				if (radX == 0) {
					radX = radTmp;
					radTmp = 0;
				}
				if (radY == 0) {
					radY = radTmp;
					radTmp = 0;
				}
			}

			if (radX > 0 && radY > 0) {
				break;
			}
		}

		out.println(radX * radY);
	}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?