Guest User

Untitled

a guest
May 28th, 2019
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 1.48 KB | None | 0 0
  1. import java.io.BufferedReader
  2. import java.io.File
  3.  
  4. /**
  5.  *
  6.  */
  7. private fun readLn() = readLine()!! // string line
  8. private fun readInt() = readLn().toInt() // single int
  9. private fun readStrings() = readLn().split(" ") // list of strings
  10. private fun readInts() = readStrings().map { it.toInt() } // list of ints
  11.  
  12. /**
  13.  * Can we place all doors while leaving rooms for each arriving employee available
  14.  */
  15. fun main() {
  16.     val filename = "/Users/gautamroy/code/java/kt-practice/i"
  17.     File(filename).bufferedReader().use { it.solve() }
  18. //    solve()
  19. }
  20.  
  21. private fun BufferedReader.solve() {
  22.     val (n, m) = readInts()
  23.     val a = readInts()
  24.     val q = readInt()
  25.     for (i in 0 until q) {
  26.         var ai = 0
  27.         val w = readInts()
  28.         val numEmp = w.size - 1
  29.         val asum = a.sum() // 1
  30.  
  31.         if (asum + numEmp > m) {
  32.             println("NO")
  33.             continue
  34.         }
  35.  
  36.         var lastPlaced = 0
  37.         var space = 0
  38.         for (j in 1 until w.size) {
  39.             space = w[j] - lastPlaced - 1
  40.             while (ai != a.size && space >= a[ai]) {
  41.                 space -= a[ai]
  42.                 ai++
  43.             }
  44.             lastPlaced = w[j]
  45.             if (ai == a.size) break
  46.         }
  47.         space = m - lastPlaced
  48.         while (ai < a.size && space >= a[ai]) {
  49.             space -= a[ai]
  50.             ai++
  51.  
  52.         }
  53.  
  54.         if (ai == a.size) {
  55.             println("YES")
  56.         } else {
  57.             println("NO")
  58.         }
  59.     }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment