結果

問題 No.325 マンハッタン距離2
ユーザー LeonardoneLeonardone
提出日時 2015-12-18 07:53:40
言語 Ruby
(3.3.0)
結果
WA  
実行時間 -
コード長 2,622 bytes
コンパイル時間 123 ms
コンパイル使用メモリ 11,620 KB
実行使用メモリ 15,320 KB
最終ジャッジ日時 2023-10-14 13:50:08
合計ジャッジ時間 3,584 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 72 ms
15,136 KB
testcase_01 WA -
testcase_02 AC 69 ms
15,048 KB
testcase_03 AC 65 ms
15,036 KB
testcase_04 AC 72 ms
15,148 KB
testcase_05 WA -
testcase_06 AC 71 ms
15,288 KB
testcase_07 AC 67 ms
15,316 KB
testcase_08 AC 64 ms
15,296 KB
testcase_09 WA -
testcase_10 AC 91 ms
15,156 KB
testcase_11 WA -
testcase_12 AC 67 ms
15,288 KB
testcase_13 WA -
testcase_14 AC 65 ms
15,296 KB
testcase_15 WA -
testcase_16 AC 66 ms
15,268 KB
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 AC 67 ms
15,244 KB
testcase_21 WA -
testcase_22 WA -
testcase_23 AC 66 ms
15,156 KB
testcase_24 AC 68 ms
15,160 KB
testcase_25 AC 66 ms
15,132 KB
testcase_26 WA -
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

#! ruby
# yukicoder My Practice
# author: Leonardone @ NEETSDKASU

def gs() gets.chomp end
def gi() gets.to_i end
def gss() gets.chomp.split end
def gis() gss.map(&:to_i) end
def nmapf(n,f) n.times.map{ __send__ f } end
def arr2d(h,w,v=0) h.times.map{[v] * w} end
def ngs(n) nmapf n,:gs end
def ngi(n) nmapf n,:gi end
def ngss(n) nmapf n,:gss end
def ngis(n) nmapf n,:gis end
def for2p(hr,wr,&pr) hr.each{|i|wr.each{|j| yield(i,j)}} end
def nsum(n) n * (n + 1) / 2 end

=begin
1) D範囲に完全内包
1.1: 軸に接しない 
3 1 10 9 100

1.2: 軸に接する
3 0 10 8 100

1.3: 軸をまたぐ
3 -1 10 7 100

1.4: 原点を含む
-3 -4 10 8 100

2) D範囲を完全内包
-10 -12 15 14 9

3) 一部だけD範囲と重なる
3.1: 三角範囲Dに入る
3.1.1: 軸に接しない
2 3 12 11 10

3.1.2: 片軸にだけ接する
0 3 12 11 10

3.1.3: 両軸に接する
0 0 12 11 10

4) Dの範囲の完全外側
10 12 20 17 7

=end


def bruteforce(x1, y1, x2, y2, d)
    count = 0
    (y1..y2).each do |y|
        (x1..x2).each do |x|
            count += 1 if x.abs + y.abs <= d
        end
    end
    count
end

def bunkatsu(z1_,z2_)
    z1, z2 = [z1_, z2_].minmax
    z1a, z2a = [z1.abs, z2.abs].minmax
    case
    when z2 < 0
        [[z1a, z2a]]
    when z2 == 0
        [[0, 0], [1, z1a]]
    when z1 > 0
        [[z1a, z2a]]
    when z1 == 0
        [[0, 0], [1, z2a]]
    when z1 < 0 && 0 < z2
        [[0, 0], [1, z1a], [1, z2a]]
    else
        sleep 10
        []
    end
end


# 4) Dの範囲の完全外側
def perfectly_outside?((x1, x2), (y1, y2), d)
    d < x1 + y1
end

# 三角形
def triangle_count((x1, x2), (y1, y2), d)
   return 0 if x1 * y1 == 0
   return 0 if d < x1 + y1 
   return 0 if [x1 + y2, x2 + y1].min < d
   rem = d - (x1 + y1)
   nsum(rem + 1)
end

# 直線
def line_count((x1, x2), (y1, y2), d)
    return 0 if x1 != x2 && y1 != y2
    return 0 if d < x1 + y1
    if d < x2 + y2
        d - (x1 + y1) + 1
    else
        (x2 + y2) - (x1 + y1) + 1
    end
end


X1,Y1,X2,Y2,D = gis

#bf = bruteforce(X1, Y1, X2, Y2, D)
#puts "bruteforce %d" % [bf]

# 1) D範囲に完全内包
if [X1.abs, X2.abs].max + [Y1.abs, Y2.abs].max <= D
    count = (X2 - X1).abs.succ * (Y2 - Y1).abs.succ
    puts count
    exit
end

# 2) D範囲を完全内包
if [X1..X2, Y1..Y2].all?{|r| r.include?(-D) && r.include?(D)}
    count = nsum(D) * 4 + 1
    puts count
    exit
end

xs = bunkatsu(X1, X2)
ys = bunkatsu(Y1, Y2)

#p xs, ys

count = 0

for2p(ys,xs) {|yt,xt|
    next if perfectly_outside?(xt, yt, D)
    count += triangle_count(xt, yt, D)
    count += line_count(xt, yt, D)
}

puts count



0