305058: CF958F2. Lightsabers (medium)

Memory Limit:256 MB Time Limit:1 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

Lightsabers (medium)

题意翻译

给你一个 $n$ 长数列 $A$ 与一个 $m$ 长数列 $B$。请你从 $A$ 中找出连续的一段,使得删除最少的元素后,满足——区间中值为 $i$ 的元素刚好有 $B[i]$ 个。 请输出与最少删除的元素数量。 样例解释: 对于 $3,3,1,2,2,1,1,3$ ,我们选取连续段 $1,2,2,1,1,3$ 后,只要删除一个 $2$,即可满足——$1$ 有 $3$ 个、$2 $ 有 $1$ 个、$3$ 有 $1$ 个。

题目描述

There is unrest in the Galactic Senate. Several thousand solar systems have declared their intentions to leave the Republic. Master Heidi needs to select the Jedi Knights who will go on peacekeeping missions throughout the galaxy. It is well-known that the success of any peacekeeping mission depends on the colors of the lightsabers of the Jedi who will go on that mission. Heidi has $ n $ Jedi Knights standing in front of her, each one with a lightsaber of one of $ m $ possible colors. She knows that for the mission to be the most effective, she needs to select some contiguous interval of knights such that there are exactly $ k_{1} $ knights with lightsabers of the first color, $ k_{2} $ knights with lightsabers of the second color, $ ... $ , $ k_{m} $ knights with lightsabers of the $ m $ -th color. However, since the last time, she has learned that it is not always possible to select such an interval. Therefore, she decided to ask some Jedi Knights to go on an indefinite unpaid vacation leave near certain pits on Tatooine, if you know what I mean. Help Heidi decide what is the minimum number of Jedi Knights that need to be let go before she is able to select the desired interval from the subsequence of remaining knights.

输入输出格式

输入格式


The first line of the input contains $ n $ ( $ 1<=n<=2·10^{5} $ ) and $ m $ ( $ 1<=m<=n $ ). The second line contains $ n $ integers in the range $ {1,2,...,m} $ representing colors of the lightsabers of the subsequent Jedi Knights. The third line contains $ m $ integers $ k_{1},k_{2},...,k_{m} $ (with ![](https://cdn.luogu.com.cn/upload/vjudge_pic/CF958F2/35fb4fe66d9c5af5cfc16ee37846c2adee7f43fd.png)) – the desired counts of Jedi Knights with lightsabers of each color from $ 1 $ to $ m $ .

输出格式


Output one number: the minimum number of Jedi Knights that need to be removed from the sequence so that, in what remains, there is an interval with the prescribed counts of lightsaber colors. If this is not possible, output $ -1 $ .

输入输出样例

输入样例 #1

8 3
3 3 1 2 2 1 1 3
3 1 1

输出样例 #1

1

Input

题意翻译

给你一个 $n$ 长数列 $A$ 与一个 $m$ 长数列 $B$。请你从 $A$ 中找出连续的一段,使得删除最少的元素后,满足——区间中值为 $i$ 的元素刚好有 $B[i]$ 个。 请输出与最少删除的元素数量。 样例解释: 对于 $3,3,1,2,2,1,1,3$ ,我们选取连续段 $1,2,2,1,1,3$ 后,只要删除一个 $2$,即可满足——$1$ 有 $3$ 个、$2 $ 有 $1$ 个、$3$ 有 $1$ 个。

加入题单

上一题 下一题 算法标签: