ABC --027 --A & B & C

AtCoder ABC 027 A&B&C AtCoder - 027

A problem

――Of the three numbers, two are the same but one is different. Find a different one. ――I want to play two to make one ――When you XOR, things that could not be canceled remain

	private void solveA() {
		int wk = IntStream.range(0, 3).map(i -> nextInt()).reduce(0, (sum, i) -> sum ^ i);
		out.println(wk);
	}

Problem A: Forcibly stream

――Something Bimyo ――In the first place --Is it okay to use collect to retrieve the values that match the conditions from the Map? ――Since it is guaranteed that only one condition is met with this pattern, is it okay to use findFirst ()?

Stream difficult

	private void solveA2() {
		int[] wk = IntStream.range(0, 3).map(i -> nextInt()).toArray();
		Map<Integer, Long> resA = Arrays.stream(wk).mapToObj(i -> new Integer(i))
				.collect(Collectors.groupingBy(i -> i, Collectors.counting()));

		List<Entry<Integer, Long>> res = resA.entrySet().stream().filter(i -> i.getValue() != 2)
				.collect(Collectors.toList());
		//		Optional<Entry<Integer, Long>> res = resA.entrySet().stream().filter(i -> i.getValue() != 2).findFirst();
		out.println(res.get(0).getKey());
	}

B problem

--I tried both cumulative sum and straightforward implementation --One is commented out

--Image of people flowing

i 1 bridge 2 bridge 3 bridge 4 bridge 5 Average number of people
0 5 - 10 3
1 3 - 2 - 10 3
2 3 - 2 - - 10 3
3 3 - 2 - - - 10 3
4 3 - 3 - 3 - 3 - 3 3
i 1 bridge 2 bridge 3 bridge 4 bridge 5 Average number of people
0 - 10 5 3
1 3 - 7 - 5 3
2 3 - 3 - 9 - 3
3 3 - 3 - 3 - 6 - 3
4 3 - 3 - 3 - 3 - 3 3

――Whether or not a bridge is necessary is judged only by "whether or not the number of people on one side is the same as island x average". --If there are many, a bridge is needed to reduce the number of people on the island. --If few, you need a bridge to send people to the island

――In the figure below, if you build a bridge between 1-3, 1-3 became the average.

i 1 bridge 2 bridge 3 bridge 4 bridge 5 Average number of people
0 - 9 3 3 3
1 3 - 6 - 3 3 3
2 3 - 3 - 3 3 3 3

――In this example, you need to build a bridge on 4-5 as well. ――When you look to the left from the 4-5 bridge, it is (3 × 4 ―― 9! = 0).

i 1 bridge 2 bridge 3 bridge 4 bridge 5 Average number of people
0 - 9 6 3
1 3 - 6 - 6 3
2 3 - 3 - 3 - 6 3
3 3 - 3 - 3 3 - 3 3
	private void solveB() {
		int numN = nextInt();
		int[] wk = IntStream.range(0, numN).map(i -> nextInt()).toArray();
		int sum = Arrays.stream(wk).sum();
		if (sum == 0) {
			out.println(0);
			return;
		}
		if (sum % numN != 0) {
			out.println(-1);
			return;
		}

		int avg = sum / numN;
		int cnt = 0;

		/*Up to this point, either implementation method is the same.*/

		//------------------------------------------------------------------------
		/*
		 *Cumulative sum version
		 */
		int[] forW = new int[numN];
		for (int i = 0; i < numN; i++) {
			if (i == 0) {
				forW[i] = wk[i];
			} else {
				forW[i] += forW[i - 1] + wk[i];
			}

		}
		/*
		 *Initially 0(i-1)From 1(i)Do you need a bridge?
		 *  ->So, start from 1. The maximum value of the bridge is N-1 so 0(or N)Either is not applicable
		 *necessary-> 0(i-1)The population at the point is different from the average value (it doesn't matter if it is large or small)
		 *Unnecessary-> 0(i-1)The population at the point is the same as the average value
		 *
		 * i-Whether a bridge is needed from 1 to i
		 *necessary-> sum(i-1)When,(i-1)*Mean is different (it doesn't matter if it's more or less)
		 *Unnecessary-> sum(i-1)When,(i-1)*Same average value
		 */
		for (int i = 1; i < numN; i++) {
			if (forW[i - 1] != i * avg) {
				cnt++;
			}
		}
		//------------------------------------------------------------------------
		/*
		 *Stupid implementation version
		 */
		//		for (int i = 1; i < numN; i++) {
		//			int sumLeft = 0;
		//			for (int j = 0; j < i; j++) {
		//				sumLeft += wk[j];
		//			}
		//			int sumRight = 0;
		//			for (int j = i; j < numN; j++) {
		//				sumRight += wk[j];
		//			}
		//			if (sumLeft != i * avg || sumRight != (numN - i) * avg) {
		//				cnt++;
		//			}
		//		}
		//------------------------------------------------------------------------
		out.println(cnt);
	}

C problem

――I couldn't understand without seeing the solution. I still only know somehow. ――I can illustrate it, but I can't explain it in words. .. .. -Solution: P.16 ~

	private void solveC() {
		long numN = nextLong();

		/*
		 *Examine the depth
		 */
		int depthCnt = 0;
		for (long n = numN; n > 0; n >>= 1) {
			depthCnt++;
		}

		long cnt = 1;

		/*
		 *If the depth is even[ 2*n ]Start from
		 *If the depth is odd[ 2*n+1 ]Start from
		 */
		int adjust = depthCnt % 2;
		/*
		 *Aoki wins if the first number cannot be counted
		 *After this,
		 *Takahashi wins if Aoki can't count
		 *Aoki wins if Takahashi can't count
		 *Repeat until you win alternately.
		 *Maybe I should repeat to MAX of Long, but I'm not confident so while
		 */
		boolean who = true;
		while (true) {
			cnt = 2 * cnt + adjust;
			if (cnt > numN) {
				out.println(who ? "Aoki" : "Takahashi");
				return;
			}
			who = !who;
			adjust = 1 - adjust;

		}

	}

Recommended Posts

ABC --013-A & B & C
ABC --023 --A & B & C
ABC --036-A & B & C
ABC --010 --A & B & C
ABC --028 --A & B & C
ABC --128 --A & B & C
ABC --012-A & B & C
ABC --018 --A & B & C
ABC --054 --A & B & C
ABC --017 --A & B & C
ABC --029- A & B & C
ABC --022 --A & B & C
ABC --019 --A & B & C
ABC --020 --A & B & C
ABC --030- A & B & C
ABC --127 --A & B & C
ABC --132- A & B & C
ABC --026 --A & B & C
ABC --014- A & B & C
ABC --016 --A & B & C
ABC --011-A & B & C
ABC --031 --A & B & C
ABC --025 --A & B & C
ABC --024 --A & B & C
ABC --027 --A & B & C
ABC --080- A & B & C
ABC --129- A & B & C & D
ABC --133- A & B & C & D
ABC --125- A & B & C & D
ABC --130- A & B & C & D
ABC --126 --A & B & C & D
ABC --134- A & B & C & D & E
ABC --131- A & B & C & D & E
diverta 2019 Programming Contest A & B & C & D
AtCoder Beginner Contest 169 A, B, C with ruby
atcoder ABC113 C problem
ABC093 C --Same Integers
atcoder ABC115 C problem
AtCoder Beginner Contest 170 A, B, C up to ruby
A person writing C ++ tried writing Java
Make a SOAP call in C #
Call a C function from Swift