CF387E.George and Cards
提高+/省选-
通过率:0%
时间限制:2.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
George is a cat, so he loves playing very much.
Vitaly put n cards in a row in front of George. Each card has one integer written on it. All cards had distinct numbers written on them. Let's number the cards from the left to the right with integers from 1 to n. Then the i-th card from the left contains number p__i (1 ≤ p__i ≤ n).
Vitaly wants the row to have exactly k cards left. He also wants the i-th card from left to have number b__i written on it. Vitaly gave a task to George, to get the required sequence of cards using the remove operation n - k times.
In one remove operation George can choose w (1 ≤ w; w is not greater than the current number of cards in the row) contiguous cards (contiguous subsegment of cards). Let's denote the numbers written on these card as _x_1, _x_2, ..., x__w (from the left to the right). After that, George can remove the card x__i, such that x__i ≤ x__j for each j (1 ≤ j ≤ w). After the described operation George gets w pieces of sausage.
George wondered: what maximum number of pieces of sausage will he get in total if he reaches his goal and acts optimally well? Help George, find an answer to his question!
乔治是一只猫,因此他非常喜欢玩耍。
维塔利在乔治面前从左到右摆放了 $ n $ 张卡片,每张卡片上写有一个整数,且所有卡片上的数字互不相同。我们从左到右依次将这些卡片编号为 $ 1 $ 到 $ n $。那么,从左起第 $ i $ 张卡片上写的数字为 $ p_i ( 1 \leq p_i \leq n $)。
维塔利希望最终这排卡片中恰好剩下 $ k $ 张,并且从左起第 $ i $ 张剩余卡片上写的数字为 $ b_i $。为此,他要求乔治执行 $ n - k $ 次“移除操作”,以得到所要求的卡片序列。
一次“移除操作”中,乔治可选择当前排中一段长度为 $ w ( 1 \leq w $,且 $ w $ 不超过当前卡片总数)的连续子段(即连续的一组卡片)。设该子段中卡片上的数字从左到右依次为 $ x_1, x_2, \dots, x_w $。随后,乔治可以从中移除某一张卡片 $ x_i $,满足 $ x_i \leq x_j $ 对所有 $ j = 1, 2, \dots, w $ 均成立(即移除该子段中的最小值)。执行完该操作后,乔治将获得 $ w $ 块香肠。
乔治不禁思考:若他成功达成目标(即最终留下指定的 $ k $ 张卡片),并且在整个过程中采取最优策略,那么他最多总共能获得多少块香肠?请帮助乔治回答这个问题!
输入格式
The first line contains integers n and k (1 ≤ k ≤ n ≤ 106) — the initial and the final number of cards.
The second line contains n distinct space-separated integers _p_1, _p_2, ..., p__n (1 ≤ p__i ≤ n) — the initial row of cards.
The third line contains k space-separated integers _b_1, _b_2, ..., b__k — the row of cards that you need to get. It is guaranteed that it's possible to obtain the given row by using the remove operation for n - k times.
第一行包含两个整数 n 和 k(1≤k≤n≤106)——分别为初始和最终的卡片数量。
第二行包含 n 个互不相同的、以空格分隔的整数 p1, p2, …, pn(1≤pi≤n)——初始的卡片排列。
第三行包含 k 个以空格分隔的整数 b1, b2, …, bk——你需要得到的目标卡片排列。题目保证可以通过恰好执行 n−k 次删除操作得到该排列。
输出格式
Print a single integer — the maximum number of pieces of sausage that George can get if he acts optimally well.
输出一个整数——乔治在最优策略下能够获得的香肠块数的最大值。
输入输出样例
输入#1
3 2 2 1 3 1 3
输出#1
1
输入#2
10 5 1 2 3 4 5 6 7 8 9 10 2 4 6 8 10
输出#2
30
输入解题思路,AI测评打分。不知道怎么写?