結果

問題 No.1872 Dictionary Order
ユーザー chineristACchineristAC
提出日時 2022-03-12 02:10:32
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 219 ms / 2,000 ms
コード長 3,161 bytes
コンパイル時間 188 ms
コンパイル使用メモリ 82,388 KB
実行使用メモリ 108,968 KB
最終ジャッジ日時 2024-09-19 08:45:10
合計ジャッジ時間 5,316 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 219 ms
108,968 KB
testcase_01 AC 57 ms
65,176 KB
testcase_02 AC 131 ms
87,620 KB
testcase_03 AC 70 ms
70,912 KB
testcase_04 AC 150 ms
86,888 KB
testcase_05 AC 60 ms
65,920 KB
testcase_06 AC 112 ms
87,552 KB
testcase_07 AC 95 ms
82,432 KB
testcase_08 AC 60 ms
66,304 KB
testcase_09 AC 71 ms
71,296 KB
testcase_10 AC 101 ms
84,540 KB
testcase_11 AC 124 ms
87,168 KB
testcase_12 AC 77 ms
74,528 KB
testcase_13 AC 68 ms
69,760 KB
testcase_14 AC 122 ms
87,252 KB
testcase_15 AC 133 ms
86,912 KB
testcase_16 AC 65 ms
68,608 KB
testcase_17 AC 107 ms
85,376 KB
testcase_18 AC 111 ms
86,912 KB
testcase_19 AC 79 ms
74,368 KB
testcase_20 AC 74 ms
72,320 KB
testcase_21 AC 117 ms
85,988 KB
testcase_22 AC 203 ms
102,528 KB
testcase_23 AC 126 ms
86,912 KB
testcase_24 AC 111 ms
83,456 KB
testcase_25 AC 146 ms
88,048 KB
testcase_26 AC 109 ms
83,456 KB
testcase_27 AC 143 ms
87,936 KB
testcase_28 AC 169 ms
87,332 KB
testcase_29 AC 201 ms
100,992 KB
testcase_30 AC 59 ms
66,176 KB
testcase_31 AC 210 ms
104,584 KB
testcase_32 AC 48 ms
56,576 KB
testcase_33 AC 47 ms
56,320 KB
testcase_34 AC 56 ms
64,128 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class SegmentTree:
    def __init__(self, init_val, segfunc, ide_ele):
        n = len(init_val)
        self.segfunc = segfunc
        self.ide_ele = ide_ele
        self.num = 1 << (n - 1).bit_length()
        self.tree = [ide_ele] * 2 * self.num
        self.size = n
        for i in range(n):
            self.tree[self.num + i] = init_val[i]
        for i in range(self.num - 1, 0, -1):
            self.tree[i] = self.segfunc(self.tree[2 * i], self.tree[2 * i + 1])

    def update(self, k, x):
        k += self.num
        self.tree[k] = x
        while k > 1:
            k >>= 1
            self.tree[k] = self.segfunc(self.tree[2*k], self.tree[2*k+1])

    def query(self, l, r):
        if r==self.size:
            r = self.num

        res = self.ide_ele

        l += self.num
        r += self.num
        right = []
        while l < r:
            if l & 1:
                res = self.segfunc(res, self.tree[l])
                l += 1
            if r & 1:
                right.append(self.tree[r-1])
            l >>= 1
            r >>= 1

        for e in right[::-1]:
            res = self.segfunc(res,e)
        return res

    def bisect(self,l,r,x):
        if r==self.size:
            r = self.num
        l += self.num
        r += self.num
        segment = []
        segment_r = []
        while l<r:
            if l&1:
                segment.append(l)
                l += 1
            if r&1:
                segment_r.append(r-1)
            l >>= 1
            r >>= 1
        segment += segment_r[::-1]
        tmp = self.ide_ele
        for pos in segment:
            check = self.segfunc(tmp,self.tree[pos])
            if check > x:
                break
            else:
                tmp = check
        else:
            return -1
        
        while pos < self.num:
            check = self.segfunc(tmp,self.tree[2*pos])
            if check > x:
                pos = 2 * pos
            else:
                tmp = check
                pos = 2 * pos + 1
        return pos - self.num

import sys,random,bisect
from collections import deque,defaultdict
from heapq import heapify,heappop,heappush
from itertools import permutations
from math import log,gcd

input = lambda :sys.stdin.readline().rstrip()
mi = lambda :map(int,input().split())
li = lambda :list(mi())

N,M = mi()
A = li()
P = li()

pos = [-1] * (N+1)
for i in range(N):
    pos[P[i]] = i

dp = [[False for j in range(M+1)] for i in range(N+1)]
dp[N][0] = True
for i in range(N)[::-1]:
    for j in range(M+1):
        if A[i] <= j:
            dp[i][j] = dp[i+1][j]|dp[i+1][j-A[i]]
        else:
            dp[i][j] = dp[i+1][j]


if not dp[0][M]:
    exit(print(-1))

res = []
L = 0
S = 0
use = [False] * (N+1)
while True:
    for i in range(1,N+1):
        tmp_L = max(L,pos[i]+1)
        tmp_rest = M - S - A[pos[i]]
        if 0 <= tmp_rest and not use[i] and L <= pos[i] and dp[tmp_L][tmp_rest]:
            res.append(i)
            L = max(L,pos[i]+1)
            S = S + A[pos[i]]
            use[i] = True
            break
        else:
            pass
    else:
        break

print(len(res))
print(*[pos[i]+1 for i in res])

0