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 - 056 - A&B&C

Posted at

AtCoder ABC 056 B&C

AtCoder - 056

A問題

a='H'の時はAtCoDeerは正直に話している
a='D'の時はAtCoDeerは嘘をついている

b='H'の時はAtCoDeerはTopCoDeerを正直者だと言っている
b='D'の時はAtCoDeerはTopCoDeerは嘘つきだと言っている

で、TopCoDeerが正直か嘘つきかを判定する。

  • a='H' の時、

    • b='H' AtCoDeerは正直に、TopCoDeerを正直者だと言っている
    • b='D' AtCoDeerは正直に、TopCoDeerを嘘つきだと言っている
  • a='D' の時、

    • b='H' AtCoDeerは嘘をついていて、TopCoDeerを正直者だと言っている
      • TopCoDeerは嘘つきなのに、AtCoDeerはTopCoDeerが正直だと嘘をついている。
    • b='D' AtCoDeerは嘘をついていて、TopCoDeerを嘘つきだと言っている
      • TopCoDeerは正直者なのに、AtCoDeerはTopCoDeerが嘘つきだと嘘をついている。
	private void solveA() {
		String strA = next();
		String strB = next();

		if (strA.equals("H")) {
			System.out.println(strB.equals("H") ? "H" : "D");
		} else {
			System.out.println(strB.equals("H") ? "D" : "H");
		}

	}

B問題

四角形Aと四角形Bと考えて、パターンは二つ

  • 四角形Aが四角形Bより-のX座標軸にいる
    • 四角形Aの左端(座標A)より、四角形Bの右端(座標B+W)が-の位置にいる
  • 四角形Aが四角形Bより+のX座標軸にいる
    • 四角形Aの右端(座標A+W)より、四角形Bの左端(座標B)が+の位置にいる

上記2パターン以外は四角形Aと四角形BのX軸は一部かぶるので移動する必要がない。
そのため、常に移動量は0となる。

	private void solveB() {
		int numW = nextInt();
		int numA = nextInt();
		int numB = nextInt();

		long res = 0;

		if (numA + numW < numB) {
			res = Math.abs(numB - (numA + numW));
		} else if (numB + numW < numA) {
			res = Math.abs(numA - (numB + numW));
		}
		System.out.println(res);
	}

C問題

カンガルーは-/0/+に移動することができる。
座標Xに移動するために必要な最小の秒数を求める。
これ、考えてみると-に移動する必要がないと思う。
カンガルーの行動として、その場にとどまるか、進むか以外の選択をする必要がない。
単純に、i秒後にいることが可能な点i!を算出し、i!X座標以上かどうか

練習のためにStreamを使って実装してみた。
 ->コメントアウトの部分
結論として、このパターンにおいてはStreamを使うべきではないのでは?という結論になった。
頑張ったのですが、ろくな実装にならない。
おとなしくwhile使っておけという感じ。

	private void solveC() {
		int numX = nextInt();

		int res = 0;
		int cnt = 0;
		int wkTotal = 0;
		while (true) {
			cnt++;
			wkTotal += cnt;
			if (wkTotal >= numX) {
				res = cnt;
				break;
			}
		}
		//
		//		for (int i = 0;; i++) {
		//			sum += i;
		//			if (sum >= numX) {
		//				res = i;
		//				break;
		//			}
		//		}

		//		AtomicInteger wkRes = new AtomicInteger(-1);
		//		IntStream.range(0, 1000000000).reduce(0, (inSum, index) -> {
		//			int wk = inSum + index;
		//			if (wk >= numX && wkRes.intValue() == -1) {
		//				wkRes.set(index);
		//			}
		//			return wk;
		//		});

		//		AtomicInteger wkTotal2 = new AtomicInteger(0);
		//		AtomicInteger wkRes2 = new AtomicInteger(-1);
		//		IntStream.range(0, 200).forEach(index -> {
		//			wkTotal2.addAndGet(index);
		//			if (wkTotal2.intValue() >= numX && wkRes2.intValue() == -1) {
		//				wkRes2.set(index);
		//				return;
		//			}
		//		});

		out.println(res);
	}

参照

Stream APIの使い方
Streamの使い方
loop内でindexを取得する方法

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?