Submission #3020304


Source Code Expand

import java.io._
import java.util.StringTokenizer

import scala.collection.mutable
import scala.util.Sorting
import math.{abs, max, min}
import scala.collection.mutable.{ArrayBuffer, ListBuffer}
import scala.reflect.ClassTag

object Main {
  val MOD = 1000000007
  val out = new PrintWriter(System.out)

  def solve(): Unit = {
    val sc = new InputReader(System.in)
    val S = sc.next()
    val x, y = sc.nextInt()

    val X, Y = ArrayBuffer[Int]()
    var h = true
    var move = 0
    rep(S.length) { i =>
      S(i) match {
        case 'F' => move += 1
        case 'T' =>
          (if (h) X else Y) += move
          h ^= true
          move = 0
      }
    }
    if (move > 0) (if (h) X else Y) += move

    def reach(A: ArrayBuffer[Int], p: Int): Boolean = {
      val s = A.sum
      if (abs(p) > s) false // 移動距離が足りない
      else {
        var cur, next = Array.ofDim[Boolean](s * 2 + 1)
        cur(s) = true
        rep(A.length) { i =>
          rep(s * 2 + 1) { j =>
//            next(j) = cur(j)
            if (j + A(i) <= s * 2) next(j) ||= cur(j + A(i))
            if (j - A(i) >= 0) next(j) ||= cur(j - A(i))
          }
          val tmp = cur
          cur = next
          next = tmp
        }
        cur(s + p)
      }
    }

    val ok = reach(X, x) && reach(Y, y)
    val ans = if (ok) "Yes" else "No"
    out.println(ans)
  }

  def main(args: Array[String]): Unit = {
    solve()
    out.flush()
  }


  class InputReader(val stream: InputStream) {
    private val reader = new BufferedReader(new InputStreamReader(stream), 32768)
    private var tokenizer: StringTokenizer = _

    def next(): String = {
      while (tokenizer == null || !tokenizer.hasMoreTokens)
        tokenizer = new StringTokenizer(reader.readLine)
      tokenizer.nextToken
    }

    def nextInt(): Int = next().toInt
    def nextLong(): Long = next().toLong
    def nextChar(): Char = next().charAt(0)
  }
  def rep(n: Int, offset: Int = 0)(f: Int => Unit): Unit = {
    var i = offset
    val N = n + offset
    while(i < N) { f(i); i += 1 }
  }
  def rep_r(n: Int, offset: Int = 0)(f: Int => Unit): Unit = {
    var i = n - 1 + offset
    while(i >= offset) { f(i); i -= 1 }
  }

  def map[@specialized A: ClassTag](n: Int)(f: Int => A): Array[A] = {
    val res = Array.ofDim[A](n)
    rep(n)(i => res(i) = f(i))
    res
  }

  implicit class ArrayOpts[A](val as: Array[A]) extends AnyVal {
    // todo Orderingだとboxing発生するので自作Orderを用意したい
    def maxByOpt[B: Ordering](f: A => B): Option[A] = {
      if (as.nonEmpty) Some(as.maxBy(f)) else None
    }

    def grpBy[K](f: A => K): mutable.Map[K, ArrayBuffer[A]] = {
      val map = mutable.Map.empty[K, ArrayBuffer[A]]
      rep(as.length)(i => map.getOrElseUpdate(f(as(i)), ArrayBuffer()) += as(i))
      map
    }

    def sumBy[B](f: A => B)(implicit num: Numeric[B]): B = {
      var sum = num.zero
      rep(as.length)(i => sum = num.plus(sum, f(as(i))))
      sum
    }

    def minByEx[B](f: A => B, ixRange: Range = as.indices)(implicit cmp: Ordering[B]): (A, B) = {
      limit(f, ixRange)(cmp.lt)
    }

    def maxByEx[B](f: A => B, ixRange: Range = as.indices)(implicit cmp: Ordering[B]): (A, B) = {
      limit(f, ixRange)(cmp.gt)
    }

    private def limit[B](f: A => B, ixRange: Range)(cmp: (B, B) => Boolean): (A, B) = {
      var limA = as(ixRange.head)
      var limB = f(limA)

      for (i <- ixRange.tail) {
        val a = as(i)
        val b = f(a)
        if (cmp(b, limB)) {
          limA = a
          limB = b
        }
      }
      (limA, limB)
    }
  }

  implicit class IterableOpts[A](val as: Iterable[A]) extends AnyVal {
    def sumBy[B](f: A => B)(implicit num: Numeric[B]): B = {
      as.foldLeft(num.zero)((acc, a) => num.plus(acc, f(a)))
    }
  }
}

Submission Info

Submission Time
Task D - FT Robot
User yakamoto
Language Scala (2.11.7)
Score 0
Code Size 3948 Byte
Status WA
Exec Time 550 ms
Memory 27344 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 500
Status
AC × 6
AC × 53
WA × 3
Set Name Test Cases
Sample 0_00.txt, 0_01.txt, 0_02.txt, 0_03.txt, 0_04.txt, 0_05.txt
All 0_00.txt, 0_01.txt, 0_02.txt, 0_03.txt, 0_04.txt, 0_05.txt, 1_00.txt, 1_01.txt, 1_02.txt, 1_03.txt, 1_04.txt, 1_05.txt, 1_06.txt, 1_07.txt, 1_08.txt, 1_09.txt, 1_10.txt, 1_11.txt, 1_12.txt, 1_13.txt, 1_14.txt, 1_15.txt, 1_16.txt, 1_17.txt, 1_18.txt, 1_19.txt, 1_20.txt, 1_21.txt, 1_22.txt, 1_23.txt, 1_24.txt, 1_25.txt, 1_26.txt, 1_27.txt, 1_28.txt, 1_29.txt, 1_30.txt, 1_31.txt, 1_32.txt, 1_33.txt, 1_34.txt, 1_35.txt, 1_36.txt, 1_37.txt, 1_38.txt, 1_39.txt, 1_40.txt, 1_41.txt, 1_42.txt, 1_43.txt, 1_44.txt, 1_45.txt, 1_46.txt, 1_47.txt, 1_48.txt, 1_49.txt
Case Name Status Exec Time Memory
0_00.txt AC 312 ms 25284 KB
0_01.txt AC 313 ms 25164 KB
0_02.txt AC 318 ms 25412 KB
0_03.txt AC 315 ms 25256 KB
0_04.txt AC 312 ms 25140 KB
0_05.txt AC 318 ms 25288 KB
1_00.txt AC 346 ms 25424 KB
1_01.txt WA 340 ms 25404 KB
1_02.txt AC 340 ms 25488 KB
1_03.txt AC 341 ms 25380 KB
1_04.txt AC 339 ms 25296 KB
1_05.txt AC 338 ms 25384 KB
1_06.txt AC 529 ms 25424 KB
1_07.txt AC 323 ms 25020 KB
1_08.txt AC 500 ms 25624 KB
1_09.txt WA 499 ms 25388 KB
1_10.txt AC 532 ms 25524 KB
1_11.txt AC 327 ms 25248 KB
1_12.txt AC 505 ms 25388 KB
1_13.txt AC 503 ms 25376 KB
1_14.txt AC 521 ms 25356 KB
1_15.txt AC 523 ms 25612 KB
1_16.txt AC 550 ms 25316 KB
1_17.txt AC 549 ms 25388 KB
1_18.txt AC 512 ms 25372 KB
1_19.txt AC 327 ms 25280 KB
1_20.txt AC 511 ms 25408 KB
1_21.txt AC 324 ms 25272 KB
1_22.txt AC 511 ms 25392 KB
1_23.txt AC 430 ms 25588 KB
1_24.txt AC 514 ms 25296 KB
1_25.txt WA 516 ms 25168 KB
1_26.txt AC 341 ms 25252 KB
1_27.txt AC 328 ms 23220 KB
1_28.txt AC 522 ms 25444 KB
1_29.txt AC 325 ms 25404 KB
1_30.txt AC 478 ms 25312 KB
1_31.txt AC 321 ms 25280 KB
1_32.txt AC 422 ms 25380 KB
1_33.txt AC 383 ms 25472 KB
1_34.txt AC 383 ms 25384 KB
1_35.txt AC 317 ms 25296 KB
1_36.txt AC 368 ms 25324 KB
1_37.txt AC 318 ms 25284 KB
1_38.txt AC 355 ms 25240 KB
1_39.txt AC 319 ms 25260 KB
1_40.txt AC 352 ms 25396 KB
1_41.txt AC 352 ms 25480 KB
1_42.txt AC 482 ms 25492 KB
1_43.txt AC 485 ms 23680 KB
1_44.txt AC 460 ms 25496 KB
1_45.txt AC 473 ms 25496 KB
1_46.txt AC 527 ms 27344 KB
1_47.txt AC 325 ms 25176 KB
1_48.txt AC 324 ms 23480 KB
1_49.txt AC 320 ms 24996 KB