Submission #856937


Source Code Expand

Copy
package main

import (
  "os"
  "bufio"
  "strconv"
  "fmt"
)

var scanner *bufio.Scanner

func getI64() int64 {
  scanner.Scan()
  x, _ := strconv.ParseInt(scanner.Text(), 10, 64)
  return x
}
func getI() int {
  return int(getI64())
}

func main() {
  scanner = bufio.NewScanner(os.Stdin)
  scanner.Split(bufio.ScanWords)
  writer := bufio.NewWriter(os.Stdout)
  defer writer.Flush()

  n := getI()
  hotels := make([]int, n)
  for i := range hotels {
    hotels[i] = getI()
  }
  hop := getI()
  maxLeap := 30

  leapsRight := make([][]int, n)
  for i := range leapsRight {
    leapsRight[i] = make([]int, maxLeap + 1)
    for j := range leapsRight[i] {
      leapsRight[i][j] = -1
    }
  }
  left, right := 0, 1
  for right < n {
    for hotels[left] + hop < hotels[right] {
      leapsRight[left][0] = right - 1
      left++
    }
    right++
  }
  for left < n - 1 {
    leapsRight[left][0] = n - 1
    left++
  }
  for x := 0; x <= maxLeap; x++ {
    for a := 0; a < n; a++ {
      b := leapsRight[a][x]
      if b != -1 {
        c := leapsRight[b][x]
        if c != -1 {
          leapsRight[a][x + 1] = c
        }
      }
    }
  }

  leapsLeft := make([][]int, n)
  for i := range leapsLeft {
    leapsLeft[i] = make([]int, maxLeap + 1)
    for j := range leapsLeft[i] {
      leapsLeft[i][j] = -1
    }
  }
  left, right := n - 2, n - 1
  for left >= 0 {
    for hotels[right] - hop > hotels[right] {
      leapsLeft[right][0] = left + 1
      right--
    }
    left--
  }
  for right > 0 {
    leapsLeft[right][0] = 0
    right--
  }
  for x := 0; x <= maxLeap; x++ {
    for a := n - 1; a >= 0; a-- {
      b := leapsLeft[a][x]
      if b != -1 {
        c := leapsLeft[b][x]
        if c != -1 {
          leapsLeft[a][x + 1] = c
        }
      }
    }
  }

  numQueries := getI()
  for qi := 0; qi < numQueries; qi++ {
    a, b := getI() - 1, getI() - 1
    result := 0
    if a < b {
      for x := maxLeap; x >= 0; x-- {
        p := leapsRight[a][x]
        if p != -1 && p <= b {
          result += 1 << uint(x)
          a = p
          if a == b {
            break
          }
        }
      }
    } else {
      for x := maxLeap; x >= 0; x-- {
        p := leapsLeft[a][x]
        if p != -1 && p >= b {
          result += 1 << uint(x)
          a = p
          if a == b {
            break
          }
        }
      }
    }
    writer.WriteString(fmt.Sprintf("%d\n", result))
  }
}

Submission Info

Submission Time
Task E - Tak and Hotels
User Eeyore
Language Go (1.6)
Score 0
Code Size 2543 Byte
Status CE

Compile Error

# command-line-arguments
./Main.go:73: no new variables on left side of :=