提出 #76656226
ソースコード 拡げる
Copy
class FastScannerdef initialize@data = STDIN.read@idx = 0@n = @data.lengthenddef next_tokendata = @datan = @ni = @idxi += 1 while i < n && data.getbyte(i) <= 32j = ij += 1 while j < n && data.getbyte(j) > 32@idx = jdata[i...j]endend
class FastScanner
def initialize
@data = STDIN.read
@idx = 0
@n = @data.length
end
def next_token
data = @data
n = @n
i = @idx
i += 1 while i < n && data.getbyte(i) <= 32
j = i
j += 1 while j < n && data.getbyte(j) > 32
@idx = j
data[i...j]
end
end
def solve_case(s, k)
k1 = k + 1
inf = 1 << 60
# state 0: 末尾が使えない
# state 1: 末尾が "A"
# state 2: 末尾が "AB"
dp = Array.new(3 * k1, inf)
ndp = Array.new(3 * k1, inf)
dp[0] = 0
# 元の文字列側で、直前 2 文字をカテゴリとして持つ
# 0=A, 1=B, 2=C, 3=その他
prev2 = 3
prev1 = 3
i = 0
n = s.length
while i < n
byte = s.getbyte(i)
oc =
if byte == 65 # A
0
elsif byte == 66 # B
1
elsif byte == 67 # C
2
else
3
end
# 元の S で、この位置を右端として ABC が存在するか
old = (prev2 == 0 && prev1 == 1 && oc == 2) ? 1 : 0
cost_a = (oc == 0 ? 0 : 1)
cost_b = (oc == 1 ? 0 : 1)
cost_c = (oc == 2 ? 0 : 1)
cost_x = (oc == 3 ? 0 : 1)
ndp.fill(inf)
st = 0
while st < 3
base = st * k1
# B を選んだときの遷移先
b_base = (st == 1 ? 2 * k1 : 0)
# C を選んだとき、今 ABC ができるか
add_c = (st == 2 ? 1 : 0)
d = 0
while d < k1
cur = dp[base + d]
# A, B, その他は新しい ABC を作らない
nd = d - old
if nd >= 0
# A を選ぶ
v = cur + cost_a
idx = k1 + nd
ndp[idx] = v if v < ndp[idx]
# B を選ぶ
v = cur + cost_b
idx = b_base + nd
ndp[idx] = v if v < ndp[idx]
# その他を選ぶ
v = cur + cost_x
idx = nd
ndp[idx] = v if v < ndp[idx]
end
# C を選ぶ
nd = d + add_c - old
if nd >= 0 && nd < k1
v = cur + cost_c
idx = nd
ndp[idx] = v if v < ndp[idx]
end
d += 1
end
st += 1
end
dp, ndp = ndp, dp
prev2 = prev1
prev1 = oc
i += 1
end
ans = dp[k]
v = dp[k1 + k]
ans = v if v < ans
v = dp[2 * k1 + k]
ans = v if v < ans
ans >= inf ? -1 : ans
end
fs = FastScanner.new
first = fs.next_token
exit if first.nil? || first.empty?
t = first.to_i
out = []
t.times do
a = fs.next_token
b = fs.next_token
# 通常は S K を想定。
# もし K S の形でも読めるようにしてあります。
if a.getbyte(0) >= 48 && a.getbyte(0) <= 57
k = a.to_i
s = b
else
s = a
k = b.to_i
end
out << solve_case(s, k).to_s
end
puts out.join("\n")
提出情報
| 提出日時 | |
|---|---|
| 問題 | F - More ABC |
| ユーザ | WoolWiz |
| 言語 | Ruby 3.3 (truffleruby 25.0.0) |
| 得点 | 500 |
| コード長 | 2888 Byte |
| 結果 | AC |
| 実行時間 | 1895 ms |
| メモリ | 319932 KiB |
ジャッジ結果
| セット名 | Sample | All | ||||
|---|---|---|---|---|---|---|
| 得点 / 配点 | 0 / 0 | 500 / 500 | ||||
| 結果 |
|
|
| セット名 | テストケース |
|---|---|
| Sample | example_00.txt |
| All | example_00.txt, hand_00.txt, hand_01.txt, hand_02.txt, hand_03.txt, hand_04.txt, hand_05.txt, hand_06.txt, hand_07.txt, hand_08.txt, hand_09.txt, random_00.txt, random_01.txt, random_02.txt, random_03.txt, random_04.txt, random_05.txt, random_06.txt, random_07.txt, random_08.txt, random_09.txt, random_10.txt, random_11.txt, random_12.txt, random_13.txt, random_14.txt, random_15.txt, random_16.txt, random_17.txt, random_18.txt, random_19.txt |
| ケース名 | 結果 | 実行時間 | メモリ |
|---|---|---|---|
| example_00.txt | AC | 64 ms | 134848 KiB |
| hand_00.txt | AC | 1104 ms | 297828 KiB |
| hand_01.txt | AC | 156 ms | 180836 KiB |
| hand_02.txt | AC | 50 ms | 135484 KiB |
| hand_03.txt | AC | 1432 ms | 303108 KiB |
| hand_04.txt | AC | 611 ms | 256760 KiB |
| hand_05.txt | AC | 636 ms | 257532 KiB |
| hand_06.txt | AC | 614 ms | 257784 KiB |
| hand_07.txt | AC | 1178 ms | 319932 KiB |
| hand_08.txt | AC | 1109 ms | 316332 KiB |
| hand_09.txt | AC | 76 ms | 147200 KiB |
| random_00.txt | AC | 1353 ms | 298792 KiB |
| random_01.txt | AC | 1234 ms | 304296 KiB |
| random_02.txt | AC | 1220 ms | 303416 KiB |
| random_03.txt | AC | 1344 ms | 306224 KiB |
| random_04.txt | AC | 1229 ms | 305800 KiB |
| random_05.txt | AC | 1401 ms | 304752 KiB |
| random_06.txt | AC | 1739 ms | 309700 KiB |
| random_07.txt | AC | 1895 ms | 309524 KiB |
| random_08.txt | AC | 1714 ms | 310552 KiB |
| random_09.txt | AC | 1624 ms | 304868 KiB |
| random_10.txt | AC | 959 ms | 295484 KiB |
| random_11.txt | AC | 658 ms | 257500 KiB |
| random_12.txt | AC | 990 ms | 296300 KiB |
| random_13.txt | AC | 1009 ms | 295280 KiB |
| random_14.txt | AC | 985 ms | 296716 KiB |
| random_15.txt | AC | 671 ms | 256848 KiB |
| random_16.txt | AC | 998 ms | 295940 KiB |
| random_17.txt | AC | 1228 ms | 302884 KiB |
| random_18.txt | AC | 968 ms | 295976 KiB |
| random_19.txt | AC | 966 ms | 296400 KiB |