CF1279C.Stack of Presents
普及/提高-
通过率:0%
时间限制:1.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
Santa has to send presents to the kids. He has a large stack of n presents, numbered from 1 to n; the topmost present has number a1, the next present is a2, and so on; the bottom present has number an. All numbers are distinct.
Santa has a list of m distinct presents he has to send: b1, b2, ..., bm. He will send them in the order they appear in the list.
To send a present, Santa has to find it in the stack by removing all presents above it, taking this present and returning all removed presents on top of the stack. So, if there are k presents above the present Santa wants to send, it takes him 2k+1 seconds to do it. Fortunately, Santa can speed the whole process up — when he returns the presents to the stack, he may reorder them as he wishes (only those which were above the present he wanted to take; the presents below cannot be affected in any way).
What is the minimum time required to send all of the presents, provided that Santa knows the whole list of presents he has to send and reorders the presents optimally? Santa cannot change the order of presents or interact with the stack of presents in any other way.
Your program has to answer t different test cases.
圣诞老人需要给孩子们送礼物。他有一大堆 n 件礼物,编号为 1 到 n;最上面的礼物编号为 a1,其下方的礼物编号为 a2,依此类推;最底部的礼物编号为 an。所有编号互不相同。
圣诞老人有一张必须送出的 m 件不同礼物的清单:b1,b2,…,bm。他将严格按照该清单中给出的顺序依次送出这些礼物。
要送出一件礼物,圣诞老人需先在堆栈中找到它:移除它上方的所有礼物,取出该礼物,再将所有被移除的礼物放回堆栈顶部。因此,若某件目标礼物上方有 k 件礼物,则完成此次操作需耗时 2k+1 秒。幸运的是,圣诞老人可优化整个流程——当他把被移除的礼物放回堆栈时,可以按任意顺序重新排列它们(但仅限于被移除的那些礼物;位于目标礼物下方的礼物顺序完全不可更改)。
假设圣诞老人已知全部待送礼物清单,并能以最优方式重排礼物,那么送出所有礼物所需的最少时间是多少?圣诞老人无法以任何其他方式更改堆栈中礼物的顺序或与堆栈进行交互。
你的程序需处理 t 组不同的测试用例。
输入格式
The first line contains one integer t (1≤t≤100) — the number of test cases.
Then the test cases follow, each represented by three lines.
The first line contains two integers n and m (1≤m≤n≤105) — the number of presents in the stack and the number of presents Santa wants to send, respectively.
The second line contains n integers a1, a2, ..., an (1≤ai≤n, all ai are unique) — the order of presents in the stack.
The third line contains m integers b1, b2, ..., bm (1≤bi≤n, all bi are unique) — the ordered list of presents Santa has to send.
The sum of n over all test cases does not exceed 105.
第一行包含一个整数 t(1≤t≤100)—— 测试用例的数量。
接下来是各测试用例,每个测试用例由三行组成。
第一行包含两个整数 n 和 m(1≤m≤n≤105)—— 分别表示堆栈中礼物的数量以及圣诞老人需要送出的礼物数量。
第二行包含 n 个整数 a1, a2, ..., an(1≤ai≤n,且所有 ai 互不相同)—— 表示堆栈中礼物的排列顺序。
第三行包含 m 个整数 b1, b2, ..., bm(1≤bi≤n,且所有 bi 互不相同)—— 表示圣诞老人必须按此顺序送出的礼物列表。
所有测试用例的 n 值之和不超过 105。
输出格式
For each test case print one integer — the minimum number of seconds which Santa has to spend sending presents, if he reorders the presents optimally each time he returns them into the stack.
对于每个测试用例,输出一个整数——即圣诞老人发送礼物所需的最少秒数(假设每次他将礼物重新放回栈中时都以最优方式对礼物进行重排)。
输入输出样例
输入#1
2 3 3 3 1 2 3 2 1 7 2 2 1 7 3 4 5 6 3 1
输出#1
5 8
输入解题思路,AI测评打分。不知道怎么写?