Solve AtCoder Beginner Contest 160 in java

AtCoder Beginner Contest 160 Thank you for your hard work! Official page

The code I wrote this time is here The result was AC up to A-E. After a long time, 5 questions AC will raise your tension ^^

I will explain briefly below.

Problem A

Since a 6-character String is passed as an argument, String.charAt() It is OK if you take out the character of the specified character with and compare it.

Problem B

The problem is that you are given an amount and properly exchange it for 500-yen coins and 5-yen coins. The output of the answer is "joy", and if you have a 500-yen coin, you will get 1000 joy, so be careful not to double the amount given. Also, be aware that just because a 5 yen coin has the joy of 5 does not mean that you can simply add the surplus amount.

long happy = (x / 500) * 1000;
x = x % 500;
happy = happy + (x / 5) * 5;

I was able to AC like this.

Problem C

A house is built around the pond, giving you a distance from a certain point. It's a matter of finding the minimum distance to go around every house.

The house this time is built around a pond, so the actual route should look like a visual acuity tester. The shape is like a small circle. Therefore, if you can find the farthest distance between houses, you can calculate the minimum distance by subtracting that distance from the outer circumference of the pond.

Basically, remember the distance of the previous house and calculate it every time you enter it. The first and last houses are also next to each other, so don't forget to compare them.

Problem D

It is a problem to output how many undirected graphs with N vertices have a distance of k. I'm afraid to use undirected graphs or other difficult words, but this graph is simple.

At best, there are only two ways, either going directly or taking the road between X and Y.

Even if you think that TLE is likely to occur, you can rest assured to see the following.

3 ≤ N ≤ 2 × 10^3

Expressed in code, it looks like this: In a double for loop, I calculate which is closer, going directly or passing between XY, and add it to the array of answers.

for (int i = 1; i <= n; i++) {
	for (int j = i; j <= n; j++) {
		int direct = j - i;
		int shortCut = dist(i, x) + 1 + dist(j, y);
		int dist = Math.min(direct, shortCut);

A method called dist looks like the following.

public static int dist(int p, int q) {
	return Math.max(q - p, p - q);

Problem E

Eat X red apples and Y green apples. Given the deliciousness of red apples and the deliciousness of green apples, eat to maximize the deliciousness. There are also colorless apples, which can be colored.

I solved it with the following algorithm.

--Arrange red apples and green apples in order of deliciousness --Get X red apples in order of deliciousness and Y green apples in order of deliciousness and add them to the array APPLE --Add all colorless apples to the array APPLE --Arrange APPLE in order of deliciousness --Add X + Y from the array APPLE and add

So, I was able to AC safely. I think the reason for the victory was that I was able to add the colorless apples to the array APPLE.

Personally, the difficulty level is low in the E problem ...?

Problem F

After thinking about 5 minutes, I couldn't do it, so I was absent for the remaining 20 minutes lol


Water performance (1411) for the first time in a long time The rating is 936 → 1003, which is the first time to exceed 1000! !! : blush:

The E problem was a little easier, but I'm glad I managed to solve it ... I want to stick to A-E AC and stabilize it. If you can do this, you should be able to become a water name soon! : raised_hands:

Thank you for reading to the end!

Recommended Posts

Solve AtCoder Beginner Contest 151 in java
Solve AtCoder Beginner Contest 150 in java
Solve AtCoder Beginner Contest 153 in java
Solve AtCoder Beginner Contest 175 in java
Solve AtCoder Beginner Contest 160 in java
Solve AtCoder Beginner Contest 152 in java
Solve AtCoder Beginner Contest 156 in java
AtCoder Beginner Contest 168
AtCoder Beginner Contest 167 C Problem (Java)
AtCoder Beginner Contest 182 Participation Article
AtCoder Beginner Contest 132 D Problem
java beginner 4
java beginner 3
java beginner
AtCoder dwango Programming Contest B in Ruby, Perl and Java
AtCoder ARC 081 C hash to solve in Ruby, Perl and Java
Try to solve Project Euler in Java
Partization in Java
Changes in Java 11
Sorting AtCoder ABC 111 C hashes to solve in Ruby, Perl and Java
Rock-paper-scissors in Java
Java Beginner Exercises
Pi in Java
FizzBuzz in Java
Java Exercise "Beginner"
AtCoder Beginner Contest 169 A, B, C with ruby
Basics of threads and Callable in Java [Beginner]
[AtCoder Problem-ABC001] C-Do wind observation in Java [Code]
How to solve an Expression Problem in Java
[Beginner] Install java development tool in cloud9 development environment.
A story about a super beginner participating in the AtCoder contest for the first time (AtCoder Beginner Contest 140)
[java] sort in list
Read JSON in Java
Interpreter implementation in Java
Make Blackjack in Java
Rock-paper-scissors app in Java
Constraint programming in Java
Put java8 in centos7
NVL-ish guy in Java
Combine arrays in Java
"Hello World" in Java
It's late! Try implementing Android Notification in Java (Beginner)
Comments in Java source
Azure functions in java
Format XML in Java
Simple htmlspecialchars in Java
Boyer-Moore implementation in Java
Hello World in Java
Use OpenCV in Java
Type determination in Java
Various threads in java
Heapsort implementation (in java)
Zabbix API in Java
ASCII art in Java
Compare Lists in Java
Express failure in Java
Try to solve a restricted FizzBuzz problem in Java
Create JSON in Java
Date manipulation in Java 8
What's new in Java 8