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

Posted at

AtCoder ABC 082 B&C

AtCoder - 082

A問題

  • 平均値を求めて切り上げる
	private void solveA() {
		Scanner scanner = null;
		String a = "0";
		String b = "0";

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

			BigDecimal aB = new BigDecimal(a);
			BigDecimal bB = new BigDecimal(b);

			BigDecimal res = aB.add(bB);

			System.out.println(res.divide(new BigDecimal("2"), 0, RoundingMode.UP));

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

B問題

  • SをA to Zとなるように操作
  • yxxyとする
    • そのために配列に変えてソートをする。その結果、yxという配列がxyに並び変わる
      • 文字コード順にソートされるため
  • TをA to Zとなるように操作
  • axyaxyとする(この例だと変化なし)
  • Sはその文字順のまま、Tの文字について前後を入れ替える(Tはreverseする)
    • Tをaxy -> yxaに変える
  • 後は、操作した後のSとTの順序を調べる
    • Listに入れてソートすると楽かなとおもった。
	private void solveB() {
		Scanner scanner = null;
		String s = "";
		String t = "";

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

			List<String> wkList = new ArrayList<String>();
			char[] sA = s.toCharArray();
			char[] tA = t.toCharArray();
			Arrays.sort(sA);
			Arrays.sort(tA);

			String wkSa = new String(sA);
			String wkTa = new StringBuilder(new String(tA)).reverse().toString();
			wkList.add(wkSa);
			wkList.add(wkTa);

			Collections.sort(wkList);

			boolean res = false;
			if (s.equals(t)) {
				res = false;
			} else {
				res = wkList.get(0).equals(wkSa);
			}
			if (res) {
				System.out.println("Yes");
			} else {
				System.out.println("No");
			}

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

C問題

  • 数字毎の個数をカウントする
  • 数字と個数を比較し
    • 同じなら何もしなくてよい
    • 数字の方が個数よりも多いなら、その数字はすべて取り除く必要がある
    • 個数の方が数字よりも多いなら、数字よりも多い個数を取り除く必要がある

for文versionもコメントアウトして入れてある。

	private void solveC() {
		Scanner scanner = null;
		int n = 0;

		try {
			scanner = new Scanner(System.in);
			n = scanner.nextInt();

			int[] wk = new int[n];
			for (int i = 0; i < n; i++) {
				wk[i] = scanner.nextInt();
			}

			Map<Integer, Integer> wkMap = new HashMap<Integer, Integer>();

			Arrays.stream(wk).forEach(elm -> {
				wkMap.merge(elm, 1, (oldV, newV) -> oldV + newV);
			});
			/**
			 * For version
			 */
			//			for (int i = 0; i < wk.length; i++) {
			//				if (!wkMap.containsKey(wk[i])) {
			//					wkMap.put(wk[i], 1);
			//				} else {
			//					wkMap.put(wk[i], wkMap.get(wk[i]) + 1);
			//				}
			//			}

			int res = wkMap.keySet().stream().reduce(0, (sum, entry) -> {
				int num = entry;
				int size = wkMap.get(num);
				if (num == size) {
				} else if (num > size) {
					sum += size;
				} else if (num < size) {
					sum += Math.abs(size - num);
				}
				return sum;
			});

			/**
			 * For version
			 */
			//			for (Iterator<Integer> ite = wkMap.keySet().iterator(); ite.hasNext();) {
			//				int num = ite.next();
			//				int size = wkMap.get(num);
			//				if (num == size) {
			//				} else if (num > size) {
			//					res += size;
			//				} else if (num < size) {
			//					res += Math.abs(size - num);
			//				}
			//			}

			System.out.println(res);

		} finally {
			if (scanner != null) {
				scanner.close();
			}
		}
	}
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?