A9302 | George and Cards
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
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!
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!
输入格式
The first line contains integers $n$ and $k$ ( $1<=k<=n<=10^{6}$ ) — 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.
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.
输出格式
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
暂无题解
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted