結果

問題 No.723 2つの数の和
ユーザー maimai
提出日時 2018-08-03 22:37:58
言語 Ruby
(3.3.0)
結果
WA  
実行時間 -
コード長 540 bytes
コンパイル時間 226 ms
コンパイル使用メモリ 11,896 KB
実行使用メモリ 26,028 KB
最終ジャッジ日時 2023-10-19 21:29:44
合計ジャッジ時間 4,692 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 87 ms
16,076 KB
testcase_01 AC 86 ms
16,076 KB
testcase_02 AC 86 ms
16,076 KB
testcase_03 AC 142 ms
22,352 KB
testcase_04 AC 169 ms
24,252 KB
testcase_05 AC 155 ms
23,824 KB
testcase_06 AC 156 ms
23,876 KB
testcase_07 AC 120 ms
19,704 KB
testcase_08 AC 121 ms
20,044 KB
testcase_09 WA -
testcase_10 AC 119 ms
19,516 KB
testcase_11 AC 93 ms
16,600 KB
testcase_12 AC 130 ms
20,844 KB
testcase_13 AC 171 ms
24,656 KB
testcase_14 AC 105 ms
17,952 KB
testcase_15 AC 121 ms
19,812 KB
testcase_16 AC 137 ms
21,792 KB
testcase_17 AC 151 ms
23,172 KB
testcase_18 WA -
testcase_19 WA -
testcase_20 AC 86 ms
16,076 KB
testcase_21 AC 88 ms
16,076 KB
testcase_22 AC 85 ms
16,080 KB
testcase_23 AC 168 ms
26,028 KB
testcase_24 AC 111 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]
    end
end

p ans
0