[JAVA] Use Priority Queue with Kotlin (1.0.0) of AtCoder (+ Investigate the environment of the judge server)

AtCoder Beginner Contest 141 D Problem

https://atcoder.jp/contests/abc141/tasks/abc141_d Yesterday's AtCoder D problem was a discount voucher problem, Simply put, the question is how to efficiently apply a discount voucher that can be used as many times as you like at half price.

In order to apply the discount efficiently, I need to get the maximum price item each time. For that purpose, we use a priority queue that allows you to add elements and get the maximum value with logN and 1, respectively. (I knew it for the first time after reading this commentary)

So instantiate the Priority Queue as follows:

Main.kt


    val tmpQueue: Queue<Int> = PriorityQueue<Int>() { i1: Int, i2: Int ->
        return@PriorityQueue i2 - i1
    }

Now it works perfectly locally! I thought, but AtCoder's judge server gives a compile error.

Main.kt:16:32: error: none of the following functions can be called with the arguments supplied: 

After investigating, it seems that the constructor that instantiates the PriorityQueue by passing only the Comparator came from Java1.8, so I thought that it was before Java1.8 PriorityQueue (int initialCapacity, Comparator <? Super E> comparator) When I used, the compilation error was resolved.

Main.kt


    val tmpQueue: Queue<Int> = PriorityQueue<Int>(size) { i1: Int, i2: Int ->
        return@PriorityQueue i2 - i1
    }

Speaking of which, I don't know the runtime version of the judge server, so I write a program that outputs system information and check it.

CodeTest.kt


fun main(args: Array<String>) {
    for ((key,value) in System.getProperties()) {
        println("key -> $key value -> $value")
    }
}

The result below

key -> java.runtime.name value -> OpenJDK Runtime Environment
key -> sun.boot.library.path value -> /usr/lib/jvm/java-7-openjdk-amd64/jre/lib/amd64
key -> java.vm.version value -> 24.121-b00
key -> java.vm.vendor value -> Oracle Corporation
key -> java.vendor.url value -> http://java.oracle.com/
key -> path.separator value -> :
key -> java.vm.name value -> OpenJDK 64-Bit Server VM
key -> file.encoding.pkg value -> sun.io
key -> user.country value -> US
key -> sun.java.launcher value -> SUN_STANDARD
key -> sun.os.patch.level value -> unknown
key -> java.vm.specification.name value -> Java Virtual Machine Specification
key -> user.dir value -> /imojudge
key -> java.runtime.version value -> 1.7.0_121-b00
key -> java.awt.graphicsenv value -> sun.awt.X11GraphicsEnvironment
key -> java.endorsed.dirs value -> /usr/lib/jvm/java-7-openjdk-amd64/jre/lib/endorsed
key -> os.arch value -> amd64
key -> java.io.tmpdir value -> /tmp
key -> line.separator value -> 

key -> kotlin.home value -> /opt/kotlinc
key -> java.vm.spec...

After all the runtime was Java 1.7. This means that the methods added in Java 1.8 cannot be used.

I have to be careful in the future. .. ..

Reference link

https://docs.oracle.com/javase/jp/8/docs/api/java/util/Comparator.html

Recommended Posts

Use Priority Queue with Kotlin (1.0.0) of AtCoder (+ Investigate the environment of the judge server)
I tried to build the environment of PlantUML Server with Docker
Prepare the environment of CentOS 8 with Sakura VPS
How to use git with the power of jgit in an environment without git commands
Use the --build-arg option of docker-compose to pass environment variables to the container built with Dockerfile
CI the architecture of Java / Kotlin applications with ArchUnit
Whether to make the server side at the time of system rebuild with Kotlin or Java
How to use trained model of tensorflow2.0 with Kotlin / Java
Find the number of days in a month with Kotlin
I tried to check the operation of gRPC server with grpcurl
Coexistence of Flyway in the embedded database (h2) of the development environment and the release database (SQL Server) with Spring Boot