[JAVA] ABC --026 --A & B & C

AtCoder ABC 026 A&B&C AtCoder - 026

--ABC027 I was really worried, but the difficulty level is very different.

A problem

-Try all combinations of $ (x, y) $

	private void solveA() {
		final int a = nextInt();
		int res = IntStream.range(1, a).reduce(0, (sum, i) -> Integer.max(i * (a - i), sum));

		out.println(res);
	}

B problem

--Paint from the outside from red to white ――Finally, I manually entered 3.1415 without using Math.PI and it was NG.

	private void solveB() {
		int numN = nextInt();
		int[] wk = IntStream.range(0, numN).map(i -> nextInt()).toArray();
		Arrays.sort(wk);

		long res = 0;
		boolean isRed = true;
		for (int i = 0; i < wk.length; i++) {
			res += Math.pow(wk[numN - 1 - i], 2) * (isRed ? 1 : -1);
			isRed = !isRed;
		}
		double mul = res * Math.PI;
		out.println(mul);
	}

C problem: class usage

--The boss is passed the input array ――From the description that there is only one boss with a small employee number, can you find the last person by looking from behind? `` ――If you look from the front, you will find your subordinate, but if you look from the back, you will find your boss. ――The salary of your boss is determined by your subordinates, so it seems better to look from behind.

――It's redundant, but I created a class to express my salary ――What is BOSS even though it expresses salary? .. .. --Calculate your salary and calculate BOSS salary based on the calculation result

	private void solveC() {
		int numN = nextInt();
		/*
		 * Index=0 -> B2
		 * Index=-1 -> B1
		 */
		int[] wk = IntStream.range(0, numN - 1).map(i -> nextInt()).toArray();

		/*
		 *Subordinate list
		 */
		Boss[] wkList = new Boss[numN - 1];
		/*
		 *For Takahashi only
		 */
		Boss takahashi = new Boss();
		/*
		 *If you run it from the end, you will always have a boss. right?
		 * (Takahashi excludes his boss)
		 */
		for (int i = wk.length - 1; i >= 0; i--) {
			/*
			 *Get your salary
			 */
			int salary = getSelfSalary(wkList, i);

			/*
			 *Match the boss number and index
			 *If Takahashi is a boss-Become 1
			 */
			int bossNum = wk[i] - 2;
			/*
			 *Get a list of boss's subordinates
			 *Register your salary in your boss's list (total?)
			 */
			if (bossNum == -1) {
				/*
				 *Since BOSS is Takahashi, salary+
				 *Even in Takahashi, max and min+Because the constraint of 1 is applied
				 */
				takahashi.setMax(salary);
				takahashi.setMin(salary);
				takahashi.addMembers(1);
			} else {
				Boss boss = wkList[bossNum];
				if (boss == null) {
					/*
					 *Registered as a boss who had no subordinates until now
					 */
					boss = new Boss();
					wkList[bossNum] = boss;
				}
				boss.setMax(salary);
				boss.setMin(salary);
				boss.addMembers(1);
			}

		}

		out.println(takahashi.getMax() + takahashi.getMin() + 1);
	}

	/**
	 *Get your subordinate list,
	 *My payroll
	 * @param wkList
	 * @param i
	 * @return
	 */
	private int getSelfSalary(Boss[] wkList, int i) {
		int salary;

		Boss self = wkList[i];
		if (self != null) {
			int min = self.getMin();
			int max = self.getMax();
			salary = max + min + 1;
		} else {
			salary = 1;
		}
		return salary;
	}

	/**
	 *With the number of my subordinates
	 *Keep the maximum and minimum of your salary
	 * @author works
	 *
	 */
	private static class Boss {
		int min = Integer.MAX_VALUE;
		int max = Integer.MIN_VALUE;
		int members;

		public int getMembers() {
			return members;
		}

		public void addMembers(int member) {
			this.members += member;
		}

		public int getMin() {
			return min;
		}

		public int getMax() {
			return max;
		}

		public void setMin(int min) {
			this.min = Integer.min(this.min, min);
		}

		public void setMax(int max) {
			this.max = Integer.max(this.max, max);
		}
	}

C problem: List

I calculated using class, but you can also do it with this List, right? ?? ?? I thought, I tried to express it in List.

It was much faster than class

	private void solveC2() {
		int numN = nextInt();
		/*
		 * Index=0 -> B2
		 * Index=-1 -> B1
		 */
		int[] wk = new int[numN + 1];
		List<List<Integer>> mems = new ArrayList<List<Integer>>();
		wk[0] = 0;
		wk[1] = 0;
		mems.add(new ArrayList<Integer>());
		mems.add(new ArrayList<Integer>());
		for (int i = 0; i < numN - 1; i++) {
			wk[i + 2] = nextInt();
			mems.add(new ArrayList<Integer>());
		}

		for (int i = wk.length - 1; i >= 0; i--) {
			List<Integer> tmp = mems.get(i);
			int salary = 0;
			if (tmp.size() != 0) {
				int min = Integer.MAX_VALUE;
				int max = Integer.MIN_VALUE;
				for (int j = 0; j < tmp.size(); j++) {
					min = Integer.min(min, tmp.get(j));
					max = Integer.max(max, tmp.get(j));
				}
				salary = min + max + 1;
			} else {
				salary = 1;
			}
			tmp = mems.get(wk[i]);
			tmp.add(salary);
		}
		long salary = 0;
		{
			List<Integer> tmp = mems.get(1);
			int min = Integer.MAX_VALUE;
			int max = Integer.MIN_VALUE;
			for (int j = 0; j < tmp.size(); j++) {
				min = Integer.min(min, tmp.get(j));
				max = Integer.max(max, tmp.get(j));
			}
			salary = min + max + 1;
		}
		out.println(salary);
	}

Recommended Posts

ABC --013-A & B & C
ABC --023 --A & B & C
ABC --010 --A & B & C
ABC --028 --A & B & C
ABC --015 --A & B & C
ABC --128 --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 --007 --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 --021 --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 --122 --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
NLP4J [006-034] 100 language processing knocks with NLP4J # 34 "A B"