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

AtCoder ABC 127 A&B&C&D AtCoder - 127

I'll be back to solve E & F in the near future.

2019/05/27 B code fix

A - Ferris Wheel

	private void solveA() {
		int a = nextInt();
		int b = nextInt();

		if (a <= 5) {
			out.println(0);
		} else if (a <= 12) {
			out.println(b / 2);
		} else {
			out.println(b);
		}
	}

B - Algae

	private void solveB() {
		int r = nextInt();
		int d = nextInt();
		int x2000 = nextInt();

		int pre = x2000;
		for (int i = 1; i <= 10; i++) {
			pre = r * pre - d;
			out.println(pre);
		}

	}

C - Prison

--Input example 1

4 2 1 3 2 4

--The first gate opens with the 1,2,3 keys --The second gate opens with the 2, 3 and 4 keys --A few keys can open both gates --Find the key used in all gates

Key:1 Key:2 Key:3 Key:4
Gate:1 O O O
Gate:2 O O O
The key to open everything

I replaced it with a number.

Key:1 Key:2 Key:3 Key:4
Gate:1 1 1 1
Gate:2 1 1 1
total 1 2 2 1

A key with total = number of gates can open all doors. The process of adding from a certain range to a certain range is faster with the potato method.

So I will post the usual reference site list

--Reference site (potato method and cumulative sum) -Imotz method -Summary of imos method / cumulative sum problem in competitive programming -Be able to write the cumulative sum without thinking!

		int n = nextInt();
		int m = nextInt();

		//For memo which number key is required at each gate
		int[] wk = new int[n];

		for (int i = 0; i < m; i++) {
			int l = nextInt() - 1;
			int r = nextInt() - 1;
			wk[l] += 1;
			if (r + 1 < n) {
				wk[r + 1] -= 1;
			}
		}

		//
		for (int i = 1; i < n; i++) {
			wk[i] += wk[i - 1];
		}
		int cnt = 0;
		for (int i = 0; i < n; i++) {
			if (wk[i] == m) {
				cnt++;
			}
		}

		out.println(cnt);

D - Integer Cards

--Since the order of rewriting the cards is not specified ――It seems good to rewrite the small ones of the imported cards with the large ones.

  1. Arrange A (cards taken in) (in ascending order here)
  2. Arrange B (number of sheets) and C (integer) in C order (descending order here)
  3. Compare with [B, C] in ascending order of A
  4. Compare up to B sheets to see if C is larger than A, and rewrite A if C is larger
  5. Repeat 3-4

――For the part 3, the following processing is performed in the code, but this is because "B and C are arranged in descending order, so the part once rewritten with C will not be rewritten after that (when rewritten C larger than C does not appear) " ――If you don't do this, you will run out of time due to extra processing.

 //There is no point in rewriting (comparing) the same place, so start is shifted.
        int start = 0;

--Whole code

	private void solveD() {
		int n = nextInt();
		int m = nextInt();
		long[] a = LongStream.range(0, n).map(i -> nextLong()).toArray();

		//Sort in ascending order
		Arrays.sort(a);

		int[][] bc = IntStream.range(0, m).collect(() -> new int[m][2],
				(t, i) -> {
					t[i][0] = nextInt();
					t[i][1] = nextInt();
				},
				(t, u) -> {
					Stream.concat(Arrays.stream(t), Arrays.stream(u));
				});

		//Sort in descending order of C
		Arrays.sort(bc, (x, y) -> -Integer.compare(x[1], y[1]));
		//There is no point in rewriting (comparing) the same place, so start is shifted.
		int start = 0;
		for (int j = 0; j < m; j++) {
			int max = Integer.min((start + bc[j][0]), n);
			for (int k = start; k < max; k++) {
				if (bc[j][1] > a[k]) {
					//a[i]Is smaller is rewritten
					a[k] = bc[j][1];
					//Preparing to shift the next start
					start = k + 1;
				} else {
					//It's already sorted, so a[i]No need to compare any more if is larger
					break;
				}
			}

		}
		//total
		long res = Arrays.stream(a).sum();

		out.println(res);
	}

Recommended Posts

ABC --023 --A & B & C
ABC --036-A & B & C
ABC --010 --A & B & C
ABC --028 --A & B & C
ABC --015 --A & B & C
ABC --128 --A & B & C
ABC --012-A & B & C
ABC --018 --A & B & C
ABC --054 --A & B & C
ABC --029- A & B & C
ABC --022 --A & B & C
ABC --020 --A & B & C
ABC --030- A & B & C
ABC --127 --A & B & C
ABC --007 --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 --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 --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 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"