CF1876F.Indefinite Clownfish

NOI/NOI+/CTSC

通过率:0%

时间限制:3.50s

内存限制:512MB

AC君温馨提醒

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

题目描述

Pak Chanek has just bought an empty fish tank and he has been dreaming to fill it with his favourite kind of fish, clownfish. Pak Chanek likes clownfish because of their ability to change their genders on demand. Because of the size of his fish tank, Pak Chanek wants to buy exactly kk clownfish to fill the tank.

Pak Chanek goes to the local fish shop. The shop provides nn clownfish numbered from 11 to nn, with clownfish ii having a size of aia_i. Initially, every clownfish in the store does not have an assigned gender, but has the ability to be assigned to two possible clownfish genders, female or male.

The store has a procedure which Pak Chanek should follow to buy clownfish. The shop owner will point at each clownfish sequentially from 11 to nn and for each clownfish, she asks Pak Chanek whether to buy it or not. Each time Pak Chanek is asked, he must declare whether to buy the currently asked clownfish or not, before the shop owner moves on to the next clownfish. If Pak Chanek declares to buy the currently asked clownfish, he must also declare the gender to be assigned to that clownfish immediately. When assigning the gender for the currently asked clownfish, the following conditions must be satisfied:

  • If Pak Chanek assigns it to be female and he has bought a female clownfish before, then the size of the current one must be exactly 11 bigger than the last female one.
  • If Pak Chanek assigns it to be male and he has bought a male clownfish before, then the size of the current one must be exactly 11 smaller than the last male one.

Pak Chanek wants to buy exactly kk clownfish such that:

  • There is at least one female clownfish and one male clownfish.
  • Among the kk clownfish Pak Chanek buys, the mean size of the female clownfish is equal to the mean size of the male clownfish.

Let ll and rr respectively be the minimum and maximum index of a clownfish Pak Chanek buys. What is the minimum possible value of r−lr-l?

帕克·查内克刚刚买了一个空鱼缸,他一直梦想着用自己最喜欢的鱼类——小丑鱼来填满它。帕克·查内克喜欢小丑鱼,是因为它们能按需改变自己的性别。由于鱼缸尺寸有限,帕克·查内克希望恰好购买 kk 条小丑鱼来填满鱼缸。

帕克·查内克来到当地鱼店。该店提供 nn 条编号为 11 到 nn 的小丑鱼,其中第 ii 条小丑鱼的体型大小为 aia_i。最初,店内每条小丑鱼均未被指定性别,但均可被指定为两种可能的性别之一:雌性或雄性。

商店有一套采购流程,帕克·查内克必须遵守。店主将依次指向编号从 11 到 nn 的每条小丑鱼;对每条小丑鱼,店主都会询问帕克·查内克是否购买。每次被问及时,帕克·查内克必须在店主转向下一条小丑鱼之前,立即声明是否购买当前被问及的小丑鱼。若帕克·查内克声明购买当前被问及的小丑鱼,则他还必须立即声明该小丑鱼被指定的性别。在为当前被问及的小丑鱼指定性别时,须满足以下条件:

  • 若帕克·查内克将其指定为雌性,且此前已购买过雌性小丑鱼,则当前小丑鱼的体型大小必须恰好比上一条已购雌性小丑鱼大 11;
  • 若帕克·查内克将其指定为雄性,且此前已购买过雄性小丑鱼,则当前小丑鱼的体型大小必须恰好比上一条已购雄性小丑鱼小 11。

帕克·查内克希望恰好购买 kk 条小丑鱼,使得:

  • 所购小丑鱼中至少包含一条雌性与一条雄性;
  • 在所购的 kk 条小丑鱼中,雌性小丑鱼的平均体型大小等于雄性小丑鱼的平均体型大小。

设 ll 和 rr 分别为帕克·查内克所购小丑鱼的最小与最大编号。那么 r−lr-l 的最小可能值是多少?

输入格式

The first line contains two integers nn and kk (2≤k≤n≤2⋅1052 \leq k \leq n \leq 2\cdot10^5) — the number of clownfish in the shop and the number of clownfish Pak Chanek has to buy.

The second line contains nn integers a1,a2,a3,…,ana_1,a_2,a_3,\ldots,a_n (1≤ai≤n1\leq a_i\leq n) — the size of each clownfish.

第一行包含两个整数 nn 和 kk(2≤k≤n≤2⋅1052 \leq k \leq n \leq 2\cdot10^5)—— 分别表示商店中小丑鱼的数量以及 Pak Chanek 需要购买的小丑鱼数量。

第二行包含 nn 个整数 a1,a2,a3,…,ana_1,a_2,a_3,\ldots,a_n(1≤ai≤n1\leq a_i\leq n)—— 表示每条小丑鱼的尺寸。

输出格式

An integer representing the minimum possible value of r−lr-l. If it is impossible to buy exactly kk clownfish and satisfy the problem's condition, print −1-1.

一个整数,表示 r−lr-l 的最小可能值。如果无法恰好购买 kk 条小丑鱼并满足题目的条件,则输出 −1-1。

输入输出样例

  • 输入#1

    9 6
    2 4 2 3 2 4 1 3 4

    输出#1

    6
  • 输入#2

    6 6
    2 6 5 2 6 5

    输出#2

    -1
  • 输入#3

    5 4
    1 2 3 4 5

    输出#3

    -1

说明/提示

In the first example, Pak Chanek can do the following:

  1. Do not buy clownfish 11.
  2. Buy clownfish 22 and assign it as male.
  3. Buy clownfish 33 and assign it as female.
  4. Buy clownfish 44 and assign it as male.
  5. Buy clownfish 55 and assign it as male.
  6. Do not buy clownfish 66.
  7. Buy clownfish 77 and assign it as male.
  8. Buy clownfish 88 and assign it as female.
  9. Do not buy clownfish 99.

Then, we get that:

  • The mean size among the 22 female clownfish Pak Chanek buys is 2+32=2.5\frac{2+3}{2} = 2.5.
  • The mean size among the 44 male clownfish Pak Chanek buys is 4+3+2+14=2.5\frac{4+3+2+1}{4} = 2.5.
  • l=2l=2, r=8r=8, r−l=6r-l=6.

There are no better strategies.

在第一个例子中,Pak Chanek 可以执行以下操作:

  1. 不购买小丑鱼 11。
  2. 购买小丑鱼 22,并将其指定为雄性。
  3. 购买小丑鱼 33,并将其指定为雌性。
  4. 购买小丑鱼 44,并将其指定为雄性。
  5. 购买小丑鱼 55,并将其指定为雄性。
  6. 不购买小丑鱼 66。
  7. 购买小丑鱼 77,并将其指定为雄性。
  8. 购买小丑鱼 88,并将其指定为雌性。
  9. 不购买小丑鱼 99。

此时,我们得到:

  • Pak Chanek 所购买的 22 条雌性小丑鱼的平均体型大小为 2+32=2.5\frac{2+3}{2} = 2.5。
  • Pak Chanek 所购买的 44 条雄性小丑鱼的平均体型大小为 4+3+2+14=2.5\frac{4+3+2+1}{4} = 2.5。
  • l=2l=2,r=8r=8,r−l=6r-l=6。

不存在更优的策略。

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

首页