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

Posted at

AtCoder ABC 044 A&B&C

AtCoder - 044

A問題

  • K泊までの金額とK+1泊の金額が違う
  • NがKより小さい場合はK+1白の金額は使わず
	private void solveA() {
		int numN = nextInt();
		int numK = nextInt();
		int numX = nextInt();
		int numY = nextInt();

		long res = 0;
		if (numN < numK) {
			res += numN * numX;
		} else {
			res += numK * numX + (numN - numK) * numY;
		}

		out.println(res);
	}

A問題:ループ

こちらの方が解法としては奇麗かな。。。

	private void solveA2() {
		int numN = nextInt();
		int numK = nextInt();
		int numX = nextInt();
		int numY = nextInt();

		long res = 0;
		for (int i = 1; i <= Math.min(numN, numK); i++) {
			res += numX;
		}
		for (int i = numK + 1; i <= numN; i++) {
			res += numY;
		}

		out.println(res);
	}

B問題

  • 全ての文字数が偶数回出現していればOK
	private void solveB() {
		String[] wk = next().split("");
		Map<String, Integer> res = Arrays.stream(wk).collect(() -> new HashMap<String, Integer>(),
				(t, i) -> {
					t.merge(i, 1, (oldV, newV) -> oldV + newV);
				},
				(t, u) -> {
					t.putAll(u);
				});

		boolean isJudge = true;
		for (Integer resV : res.values()) {
			if (resV % 2 != 0) {
				isJudge = false;
			}
		}

		out.println(isJudge ? "Yes" : "No");
	}

C問題:部分解法version(満点取れてません)

  • マスクを利用して全探索を行っています
    • resultはTLEだけど、実際にはマスクが桁あふれするためN=50はマスクを生成しても対応できません。。。
	/*
	 * 部分解法バージョン
	 */
	private void solveC2() {
		int numN = nextInt();
		int numA = nextInt();
		int[] wk = IntStream.range(0, numN).map(i -> nextInt()).toArray();
		out.println(chkC2(wk, numA, numN));
	}

	private long chkC2(final int[] wk, final int numA, final int numN) {

		long res = 0;
		/*
		 * このマスクだと2^50対応できないので、最初から部分点狙い。。。
		 */
		int max = (int) Math.pow(2, numN);

		/*
		 * マスク数分ループ
		 */
		for (int i = 1; i < max; i++) {
			int[] mask = new int[numN];
			int bit = i;
			int cnt = 0;
			//マスク生成
			while (bit != 0) {
				mask[cnt] = bit % 2;
				bit /= 2;
				cnt++;
			}
			long temp = 0;
			int maskCnt = 0;
			for (int j = 0; j < numN; j++) {
				//マスク対象の場合はカウントアップ+tempにも足す
				if (mask[j] == 1) {
					maskCnt++;
					temp += wk[j];
				}
			}
			//			if (((temp * 10) / maskCnt) % 10 == 0 && (temp * 10) / (maskCnt * 10) == numA) {
			if (temp == numA * maskCnt) {
				//				System.out.println("temp : " + temp + " / maskCnt : " + maskCnt);
				//				System.out.println(Arrays.toString(mask));
				res++;
			}
		}
		return 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?