問題 | https://paiza.jp/poh/paizen |
---|---|
タイム一覧/Ruby/C/C++/C#/Java | http://qiita.com/cielavenir/items/17f66daa2be639fd74f3 |
Perl/PHP/Python | http://qiita.com/cielavenir/items/5b57808a28a8b4c5c7b2 |
JavaScript | http://qiita.com/cielavenir/items/9dab2bfbcfa0047b345f |
CoffeeScript/Go/Scala/VB/F# | http://qiita.com/cielavenir/items/32591dd03376ba534d1e |
CoffeeScript
paizapoh2.coffee
#!/usr/bin/env coffee
T=[]
stdin = process.openStdin()
stdin.setEncoding('utf8')
input_fragment=''
stdin.on 'data', (input) ->
ref=(input_fragment+input).split("\n")
input_fragment=ref.pop()
for i in [0...ref.length]
if ref[i]==''
continue
T.push(ref[i])
stdin.on 'end', (z) ->
if input_fragment
ref=(input_fragment+"\n").split("\n")
input_fragment=ref.pop()
for i in [0...ref.length]
if ref[i]==''
continue
T.push(ref[i])
arg=T[0].split(' ').map(Number)
h=arg[0]
w=arg[1]
a=Array(h)
for i in [0...h]
line=T[1+i]
a[i]=Array(w)
for j in [0...w]
a[i][j]=Array(h)
a[i][j][0]=line[j]^1
accum=Array(h)
for i in [0...h]
accum[i]=Array(w+1)
for j in [0..w]
accum[i][j]=0;
for i in [0...h]
accum[0][a[i][0][0]]++
for j in [1...w]
if a[i][j][0]
a[i][j][0]+=a[i][j-1][0]
accum[0][a[i][j][0]]++
for i in [1...h]
for j in [0...w]
for k in [1..i]
a[i][j][k]=a[i-1][j][k-1]
if a[i][j][k]>a[i][j][k-1]
a[i][j][k]=a[i][j][k-1]
if !a[i][j][k]
break
accum[k][a[i][j][k]]++
for i in [0...h]
for j in [0...w]
accum[i][w-1-j]+=accum[i][w-1-j+1]
k=Number(T[1+h])
for i in [0...k]
arg=T[1+h+1+i].split(' ').map(Number)
s=arg[0]
t=arg[1]
console.log(if s<=h&&t<=w then accum[s-1][t] else 0)
Go
多次元配列を直接宣言する方法がないらしいので、Cと同様に最大値固定で。
paizapoh2.go
package main
import(
"fmt"
"os"
"text/scanner"
"strconv"
)
var sin scanner.Scanner
func scanint() int{
sin.Scan()
ret,_ := strconv.Atoi(sin.TokenText())
return ret
}
func main(){
//wtf?
var a [300][300][300]int
var accum [300][301]int
sin.Init(os.Stdin)
h:=scanint()
w:=scanint()
for i:=0;i<h;i++ {
sin.Scan()
line:=sin.TokenText()
for j:=0;j<w;j++ {a[i][j][0]=(int)(line[j]-'0')^1}
}
for i:=0;i<h;i++ {
accum[0][a[i][0][0]]++
for j:=1;j<w;j++ {
if a[i][j][0]>0 {
a[i][j][0]+=a[i][j-1][0]
accum[0][a[i][j][0]]++
}
}
}
for i:=1;i<h;i++ {
for j:=0;j<w;j++ {
for k:=1;k<=i;k++ {
a[i][j][k]=a[i-1][j][k-1]
if a[i-1][j][k-1]>a[i][j][k-1] { a[i][j][k]=a[i][j][k-1] }
if a[i][j][k]==0 {break}
accum[k][a[i][j][k]]++
}
}
}
for i:=0;i<h;i++ {
for j:=w-1;j>=0;j-- {
accum[i][j]+=accum[i][j+1]
}
}
for k:=scanint();k>0;k-- {
s:=scanint()
t:=scanint()
if s<=h&&t<=w {
fmt.Println(accum[s-1][t])
} else {
fmt.Println(0)
}
}
}
Scala
paizapoh2.scala
import scala.util.control.Breaks
object Main extends App{
val Array(h,w) = readLine().split(" ").map(_.toInt)
val a=Array.ofDim[Int](h,w,h)
val accum=Array.ofDim[Int](h,w+1)
for(i<-0 to h-1){
val line=readLine();
for(j<-0 to w-1)a(i)(j)(0)=(line.charAt(j)-'0')^1
}
for(i<-0 to h-1){
accum(0)(a(i)(0)(0))+=1
for(j<-1 to w-1){
if(a(i)(j)(0)>0){
a(i)(j)(0)+=a(i)(j-1)(0)
accum(0)(a(i)(j)(0))+=1
}
}
}
val b=new Breaks
for(i<-1 to h-1){
for(j<-0 to w-1){
b.breakable{
for(k<-1 to i){
a(i)(j)(k)=Math.min(a(i-1)(j)(k-1),a(i)(j)(k-1))
if(a(i)(j)(k)==0)b.break
accum(k)(a(i)(j)(k))+=1
}
}
}
}
for(i<-0 to h-1){
for(j<-w-1 to 0 by -1)accum(i)(j)+=accum(i)(j+1)
}
val K=readInt()
for(k<-1 to K){
val Array(s,t) = readLine().split(" ").map(_.toInt)
println(if(s<=h&&t<=w) accum(s-1)(t) else 0)
}
}
VB
paizapoh2.vb
module PaizaPOH2
dim SIZE as integer=9999999
dim z(SIZE-1) as byte
dim input_count as integer=0
function myget() as integer
dim r as integer
while 48<=z(input_count) andalso z(input_count)<=57
r=r*10+z(input_count)-48
input_count+=1
end while
input_count+=1
return r
end function
function mygetC() as integer
input_count+=1
return z(input_count-1)
end function
sub Main()
Console.OpenStandardInput().Read(z,0,SIZE)
dim h as integer=myget()
dim w as integer=myget()
dim a(h-1,w-1,h-1) as integer
dim accum(h-1,w) as integer
for i as integer=0 to h-1
for j as integer=0 to w-1
a(i,j,0)=(mygetC()-48) xor 1
next
mygetC()
next
for i as integer=0 to h-1
accum(0,a(i,0,0))+=1
for j as integer=1 to w-1
if a(i,j,0)>0
a(i,j,0)+=a(i,j-1,0)
accum(0,a(i,j,0))+=1
end if
next
next
for i as integer=1 to h-1
for j as integer=0 to w-1
for k as integer=1 to i
a(i,j,k)=Math.min(a(i-1,j,k-1),a(i,j,k-1))
if a(i,j,k)=0
exit for
end if
accum(k,a(i,j,k))+=1
next
next
next
for i as integer=0 to h-1
for j as integer=w-1 to 0 step -1
accum(i,j)+=accum(i,j+1)
next
next
dim k as integer=myget()
while k>0
dim s as integer=myget()
dim t as integer=myget()
Console.WriteLine(if(s<=h andalso t<=w,accum(s-1,t),0))
k-=1
end while
end sub
end module
F#
paizapoh5.fs
open System
let arg:String array=Console.ReadLine().Split(' ')
let h=int(arg.[0])
let w=int(arg.[1])
let a:int [,,]=Array3D.zeroCreate h w h
let accum:int [,]=Array2D.zeroCreate h (w+1)
for i in 0..h-1 do
let s=Console.ReadLine()
for j in 0..w-1 do
a.[i,j,0]<-1-((int s.[j])-48)
for i in 0..h-1 do
accum.[0,a.[i,0,0]]<-accum.[0,a.[i,0,0]]+1
for j in 1..w-1 do
if a.[i,j,0]>0 then
a.[i,j,0]<-a.[i,j,0]+a.[i,j-1,0]
accum.[0,a.[i,j,0]]<-accum.[0,a.[i,j,0]]+1
for i in 1..h-1 do
for j in 0..w-1 do
for k in 1..i do
a.[i,j,k]<-Math.Min(a.[i-1,j,k-1],a.[i,j,k-1])
if a.[i,j,k]>0 then
// break is preferred here actually
accum.[k,a.[i,j,k]]<-accum.[k,a.[i,j,k]]+1
for i in 0..h-1 do
for j in w-1..-1..0 do
accum.[i,j]<-accum.[i,j]+accum.[i,j+1]
let k=int(Console.ReadLine())
for i in 0..k-1 do
let arg:String array=Console.ReadLine().Split(' ')
let s=int(arg.[0])
let t=int(arg.[1])
if s<=h&&t<=w then
Console.WriteLine(accum.[s-1,t])
else
Console.WriteLine(0)