結果
| 問題 |
No.15 カタログショッピング
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2019-01-31 22:47:35 |
| 言語 | Nim (2.2.0) |
| 結果 |
AC
|
| 実行時間 | 24 ms / 5,000 ms |
| コード長 | 1,937 bytes |
| コンパイル時間 | 3,631 ms |
| コンパイル使用メモリ | 82,260 KB |
| 実行使用メモリ | 6,944 KB |
| 最終ジャッジ日時 | 2024-07-01 11:14:46 |
| 合計ジャッジ時間 | 4,272 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 10 |
コンパイルメッセージ
/home/judge/data/code/Main.nim(4, 1) Warning: template 'stopwatch' is implicitly redefined; this is deprecated, add an explicit .redefine pragma [ImplicitTemplateRedefinition] /home/judge/data/code/Main.nim(1, 27) Warning: imported and not used: 'tables' [UnusedImport] /home/judge/data/code/Main.nim(1, 49) Warning: imported and not used: 'sugar' [UnusedImport]
ソースコード
import sequtils,algorithm,tables,strutils,times,sugar
template `^`(n:int) : int = (1 shl n)
template stopwatch(body) = (let t1 = cpuTime();body;echo "TIME:",(cpuTime() - t1) * 1000,"ms")
template stopwatch(body) = body
proc getchar_unlocked():char {. importc:"getchar_unlocked",header: "<stdio.h>" .}
proc scan(): int =
while true:
let k = getchar_unlocked()
if k < '0': return
result = 10 * result + k.ord - '0'.ord
proc cmp(x,y:seq[int]):int =
for i in 0..<min(x.len,y.len):
if x[i] != y[i] : return x[i] - y[i]
return x.len - y.len
proc buildWithKey(P:seq[int]) : seq[tuple[k,v:int]] =
var answer = newSeq[tuple[k,v:int]](^P.len)
proc impl(n,x:int) =
if n == P.len : return
answer[x or ^n].v = answer[x].v + P[n]
answer[x or ^n].k = x or ^n
impl(n+1,x)
impl(n+1,x or ^n)
impl(0,0)
return answer
proc binaryToIntSeq(n:int):seq[int] =
result = @[]
for i in 0..64:
if (n and ^i) > 0: result &= i + 1
if n < ^(i+1) : return
proc build(P:seq[int]) : seq[int] =
var answer = newSeq[int](^P.len)
proc impl(n,x:int) = # 1つずつ
if n == P.len : return
answer[x or ^n] = answer[x] + P[n]
impl(n+1,x)
impl(n+1,x or ^n)
impl(0,0)
return answer
let n = scan()
let s = scan()
let P = newSeqWith(n,scan())
if n == 1: quit "1",0
let n2 = n div 2
let P1 = P[0..<n2]
let P2 = P[n2..<n]
stopwatch:
let I1 = P1.build()
let I2 = P2.buildWithKey().sortedByIt(it.v)
stopwatch:
var answers = newSeq[int]()
for x,i1 in I1:
let si = I2.binarySearch(s-i1,proc(K:tuple[k,v:int],V:int):int=K.v-V)
if si == -1 : continue
answers &= x or (I2[si].k shl n2)
for i in (si+1)..<I2.len:
if I2[i].v + i1 != s: break
answers &= x or (I2[i].k shl n2)
for i in (si-1).countdown(0):
if I2[i].v + i1 != s: break
answers &= x or (I2[i].k shl n2)
for ans in answers.map(binaryToIntSeq).sorted(cmp):
echo ans.mapIt($it).join(" ")