CF958F2.Lightsabers (medium)

普及+/提高

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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.

银河议会局势动荡。数千个恒星系已宣布意图脱离共和国。海蒂大师需要挑选绝地武士,派遣他们前往银河系各地执行维和任务。众所周知,任何维和任务的成功与否,取决于参与该任务的绝地武士光剑的颜色组合。

海蒂面前站着 nn 名绝地武士,每人手持一把 mm 种可能颜色之一的光剑。她深知:为使任务效果达到最佳,必须选出某一段连续的武士序列,使得其中恰好有 k1k_1 名武士持有第一种颜色的光剑,k2k_2 名武士持有第二种颜色的光剑,……,kmk_m 名武士持有第 mm 种颜色的光剑。

然而,自上一次以来,她已意识到:这样的连续区间并不总能被选出。因此,她决定让部分绝地武士“前往塔图因某些沙坑附近享受无限期无薪休假”——如果你明白我的意思的话。请帮助海蒂确定:在剩余武士中能够选出满足上述要求的连续区间的前提下,最少需要让多少名绝地武士离开。

输入格式

The first line of the input contains n (1 ≤ n ≤ 2·105) 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 ) – the desired counts of Jedi Knights with lightsabers of each color from 1 to m.

输入的第一行包含两个整数 nn(1≤n≤2⋅1051 \leq n \leq 2\cdot10^5)和 mm(1≤m≤n1 \leq m \leq n)。第二行包含 nn 个整数,取值范围为 {1,2,…,m}\{1, 2, \dots, m\},表示后续绝地武士光剑的颜色。第三行包含 mm 个整数 k1,k2,…,kmk_1, k_2, \dots, k_m(满足 ),表示颜色从 11 到 mm 的光剑各自所需绝地武士人数。

输出格式

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-1。

输入输出样例

  • 输入#1

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

    输出#1

    1

输入解题思路,AI测评打分。不知道怎么写?

首页