|
|
back to boardTest 3 Sorry for my bad english I'm see all exists tests, and I'm get a correct result on these tests(according comments). Let <ms> is init array, <flag> is (answer is YES?). 1. I do check that (the number in <ms>) is not greater than <n> 2. I'm sorted <ms>. 3. I'm create a <isFills> = new Array[Boolean](n+1) And then I use the following code: if (m>1 && ms(1) == 0) return false // (ms(0)==ms(1)==0) => "NO" if (m>1 && ms(m-2)==n) return false // (ms(m-2)==ms(m-1)==n) => "NO"
isFills(max(ms(0)-1, 0)) = true for (i <- 1 until m) { if (!isFills(ms(i)-1) || !isFills(ms(i))) { if (!isFills(ms(i)-1)) isFills(ms(i)-1) = true else isFills(ms(i)) = true } else return false } return flag Console.println(if (getFlag==true) "YES" else "NO") Full code: object Main { import java.util.Scanner import math._
val scan = new Scanner(System.in) val n = scan.nextInt() val m = scan.nextInt() val ms = new Array[Int](max(m,1)) for (i <- 0 until m) ms(i) = scan.nextInt() scan.close()
def getFlag: Boolean = { var flag = true
for (i <- 0 until m) flag &&= ms(i)<=n if (!flag) return flag
java.util.Arrays.sort(ms) val isFills = new Array[Boolean](n+1)
if (m>1 && ms(1) == 0) return false if (m>1 && ms(m-2)==n) return false
isFills(max(ms(0)-1, 0)) = true for (i <- 1 until m) { if (!isFills(ms(i)-1) || !isFills(ms(i))) { if (!isFills(ms(i)-1)) isFills(ms(i)-1) = true else isFills(ms(i)) = true } else return false } flag } Console.println(if (getFlag==true) "YES" else "NO")
def main(args: Array[String]){} } Edited by author 26.12.2015 23:56 Edited by author 26.12.2015 23:57 |
|
|