公式

B - Default Price 解説 by en_translator


How can we find the price of the \(i\)-th plate? Inspect each \(j=1,2,\ldots,M\) to check if \(C_i=D_j\). If there is \(j\) such that \(C_i=D_j\), its price is \(P_j\) yen; if no \(j\) satisfies it, the price is \(P_0\) yen.

By performing this for all \(i\), the problem can be solved in a total of \(\mathrm{O}(NM)\) time.

Also, one can use a data structure like std::map in C++ to write a computationally better code.

Sample code (Python):

n, m = map(int, input().split())
c = list(input().split())
d = list(input().split())
p = list(map(int, input().split()))
ans = 0
for v in c:
    price = p[0]
    for j in range(m):
        if v == d[j]:
            price = p[j + 1]
            break
    ans += price
print(ans)

投稿日時:
最終更新: