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

Posted at

AtCoder ABC 093 A&B&C

AtCoder - 093

A問題

  • 文字列の中にa,b,cがそれぞれ1文字ずつ含まれていれば並び替えでabcを作ることは可能
	private void solveA() {
		Scanner scanner = null;
		String s = "";

		try {
			scanner = new Scanner(System.in);
			s = scanner.next();

			char[] wk = s.toCharArray();

			int cntA = 0;
			int cntB = 0;
			int cntC = 0;

			for (int i = 0; i < wk.length; i++) {
				char c = wk[i];
				if (c == 'a' && cntA == 0) {
					cntA++;
				} else if (c == 'b' && cntB == 0) {
					cntB++;
				} else if (c == 'c' && cntC == 0) {
					cntC++;
				}
			}

			System.out.println((cntA + cntB + cntC) == 3 ? "Yes" : "No");

		} finally {
			if (scanner != null) {
				scanner.close();
			}
		}
	}

B問題

  • 何も考えずにA~Bまでループ

    • 前からK番目
    • 後ろからK番目
  • 速度もとめるなら二つのループか?

    • A~min(B,A+K)までループ
    • min(A,B-K)~Bまでループ
	private void solveB() {
		Scanner scanner = null;
		int numA = 0;
		int numB = 0;
		int numK = 0;

		try {
			scanner = new Scanner(System.in);
			numA = scanner.nextInt();
			numB = scanner.nextInt();
			numK = scanner.nextInt();

			List<Integer> wk = new ArrayList<Integer>();
			for (int i = numA; i <= numB; i++) {
				if (i < numA + numK || numB - numK < i) {
					wk.add(i);
				}
			}

			for (int i = 0; i < wk.size(); i++) {
				System.out.println(wk.get(i));
			}

		} finally {
			if (scanner != null) {
				scanner.close();
			}
		}
	}

C問題

  • コードのコメントに色々記載
{1,2,3} という並びの場合
可能であれば、3に合わせるのが最小手数に見えるが、、、
 -> 3にあわせようとする
    -> 1,2にそれぞれ+1する
    -> {2,3,3} となり無理

 -> 3+1=4にあわせようとする
    -> 1に+2する {3,2,3}
    -> 2に+2する {3,4,3}
    -> 1,3に+1する {4,4,4}

操作は常に+2するので、埋め合わせる数は偶数でないといけない
なので、最大値に合わせた場合に埋め合わせる数の総計が偶数となるか奇数となるかで分岐
偶数なら/2をすればよく、奇数なら+3(3つの数字全てに+1)することで偶数に変換する
	private void solveC() {

		try (Scanner scanner = new Scanner(System.in)) {

			int[] wk = new int[3];
			wk[0] = scanner.nextInt();
			wk[1] = scanner.nextInt();
			wk[2] = scanner.nextInt();

			Arrays.sort(wk);

			/*
			 * 基準となる値の計算
			 * 全て最大値に合わせる前提
			 */
			int m3 = wk[2] * 3;
			/*
			 * 最大値に合わせてよいのか?
			 * 現在の総計と最大値に全てあわせた場合の偶奇が同じなら、
			 * 埋めなくてはいけない数値は偶数
			 * 違う場合は、埋めなくてはいけない数値が奇数
			 *  - 奇数に偶数を足しても奇数/偶数に偶数を足しても偶数
			 */
			boolean isEqual = (wk[0] + wk[1] + wk[2]) % 2 == m3 % 2;

			int targetNum = 0;
			int res = 0;
			/*
			 * 埋める数値が奇数の場合、そのままでは埋められないので
			 * 合わせる数値を+1する
			 */
			if (isEqual) {
				targetNum = wk[2];
			} else {
				targetNum = wk[2] + 1;
			}

			/*
			 * 埋めなくてはいけない差分の数値を合計して2で割る
			 */
			for (int i = 0; i < wk.length; i++) {
				res += (targetNum - wk[i]);
			}
			res /= 2;

			//			/*
			//			 * 1つ1つ何回操作するのか試しているパターン
			//			 */
			//			boolean isPlusOne = false;
			//			for (int i = 0; i < wk.length; i++) {
			//				//常に+2の操作パターン
			//				res += (targetNum - wk[i]) / 2;
			//				if (!isPlusOne) {
			//					//あまりがあったということは、二つに+1ずつする操作があるということ
			//					isPlusOne = (targetNum - wk[i]) % 2 != 0;
			//				}
			//			}
			//
			//			/*
			//			 * 二つに+1する操作
			//			 */
			//			if (isPlusOne) {
			//				res++;
			//			}

			System.out.println(res);

		}
	}
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?