結果

問題 No.723 2つの数の和
ユーザー maimai
提出日時 2018-08-03 22:39:01
言語 Ruby
(3.3.0)
結果
AC  
実行時間 177 ms / 2,000 ms
コード長 546 bytes
コンパイル時間 225 ms
コンパイル使用メモリ 11,924 KB
実行使用メモリ 26,028 KB
最終ジャッジ日時 2023-10-19 21:29:54
合計ジャッジ時間 4,422 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 87 ms
16,076 KB
testcase_01 AC 89 ms
16,076 KB
testcase_02 AC 89 ms
16,076 KB
testcase_03 AC 146 ms
22,352 KB
testcase_04 AC 177 ms
24,252 KB
testcase_05 AC 162 ms
23,824 KB
testcase_06 AC 161 ms
23,640 KB
testcase_07 AC 125 ms
19,704 KB
testcase_08 AC 128 ms
20,044 KB
testcase_09 AC 175 ms
24,404 KB
testcase_10 AC 121 ms
19,516 KB
testcase_11 AC 95 ms
16,600 KB
testcase_12 AC 133 ms
20,844 KB
testcase_13 AC 175 ms
24,656 KB
testcase_14 AC 108 ms
17,952 KB
testcase_15 AC 126 ms
19,812 KB
testcase_16 AC 139 ms
21,792 KB
testcase_17 AC 153 ms
23,176 KB
testcase_18 AC 148 ms
23,588 KB
testcase_19 AC 152 ms
24,076 KB
testcase_20 AC 88 ms
16,072 KB
testcase_21 AC 88 ms
16,076 KB
testcase_22 AC 89 ms
16,076 KB
testcase_23 AC 172 ms
26,028 KB
testcase_24 AC 114 ms
18,420 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Main.rb:3: warning: assigned but unused variable - n
Syntax OK

ソースコード

diff #

def ascan; gets.split.map(&:to_i); end

n,X = ascan
A = ascan.sort

cc = []
aa = []
A.each do |a|
    if aa.empty? || aa[-1] != a
        cc << 1
        aa << a
    else
        cc[-1] += 1
    end
end

N = aa.size

left = 0
right = N-1

ans = 0
while left < right

    if aa[left] + aa[right] > X
        right -= 1
    elsif aa[left] + aa[right] < X
        left += 1
    else
        ans += cc[left]*cc[right]
        right -= 1
    end
end

ans *= 2

aa.each_with_index do |a,i|
    if a*2 == X
        ans += cc[i]*cc[i]
    end
end

p ans
0