CF2178D.Xmas or Hysteria

普及+/提高

通过率:0%

时间限制:2.00s

内存限制:512MB

AC君温馨提醒

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

题目描述

Mass Hysteria (Hearthstone)

You are sus\color{white}{\text{You are sus}}

Franklin is plotting a Christmas attack where he casts the spell Mass Hysteria on a village of elves.

There are nn elves numbered 11 to nn, where elf ii has a health value of hih_i and an attack value of aia_i. Initially, hi=aih_i=a_i for all ii, and all aia_i are distinct. Elf ii is alive if and only if its health is positive (i.e., hi>0h_i \gt 0).

When Franklin casts Mass Hysteria, the following process is repeated:

  • Choose a pair of distinct living elves xx and yy (hx,hy>0h_x,h_y \gt 0) such that elf xx has not attacked before. If no such pair exists, terminate the process.
  • Then, elf xx attacks elf yy, decreasing hyh_y by axa_x. Additionally, due to recoil, hxh_x is decreased by aya_y. Note that axa_x and aya_y remain unchanged.

The process will repeat until it is impossible to choose a valid pair of elves. It can be shown that Mass Hysteria terminates after at most nn iterations.

Given an integer mm, construct a valid sequence of attacks such that exactly mm elves are alive when the process ends; or determine that no such sequence exists.

群体恐慌(《炉石传说》)

你很可疑\color{white}{\text{你很可疑}}

富兰克林正策划一场圣诞袭击,他将对一群精灵施放法术“群体恐慌”。

共有 nn 个精灵,编号为 11 到 nn,其中精灵 ii 的生命值为 hih_i,攻击力为 aia_i。初始时,对所有 ii 均有 hi=aih_i = a_i,且所有 aia_i 互不相同。当且仅当精灵 ii 的生命值为正(即 hi>0h_i > 0)时,该精灵存活。

当富兰克林施放“群体恐慌”时,以下过程将被重复执行:

  • 选择一对不同的存活精灵 xx 和 yy(即 hx,hy>0h_x, h_y > 0),且精灵 xx 尚未发动过攻击。若不存在这样的精灵对,则终止该过程。
  • 接着,精灵 xx 攻击精灵 yy,使 hyh_y 减少 axa_x;同时,由于反冲伤害,hxh_x 也减少 aya_y。注意:axa_x 和 aya_y 的值始终保持不变。

该过程将持续进行,直至无法选出满足条件的精灵对为止。可以证明,“群体恐慌”至多经过 nn 轮迭代后必然终止。

给定一个整数 mm,请构造一个合法的攻击序列,使得过程结束时恰好有 mm 个精灵存活;或者判断不存在这样的序列。

输入格式

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤1041 \le t \le 10^4). The description of the test cases follows.

The first line of each test case contains two integers nn and mm (2≤n≤2⋅105,0≤m≤n2\le n\le 2\cdot 10^5, 0\le m\le n) — the number of elves in the village and the number of elves to be left alive.

The second line contains nn integers a1,a2,…,ana_1,a_2,\ldots,a_n (1≤ai≤1091\le a_i\le 10^9) — the initial attack and health values of the elves.

It is guaranteed that all aia_i are distinct.

It is guaranteed that the sum of nn over all test cases does not exceed 2⋅1052\cdot 10 ^ 5.

每个测试包含多个测试用例。第一行包含测试用例的数量 tt(1≤t≤1041 \le t \le 10^4)。随后是各测试用例的描述。

每个测试用例的第一行包含两个整数 nn 和 mm(2≤n≤2⋅105, 0≤m≤n2\le n\le 2\cdot 10^5,\ 0\le m\le n)—— 分别表示村庄中精灵的数量以及需保留存活的精灵数量。

第二行包含 nn 个整数 a1,a2,…,ana_1,a_2,\ldots,a_n(1≤ai≤1091\le a_i\le 10^9)—— 表示各精灵初始的攻击力与生命值。

保证所有 aia_i 互不相同。

保证所有测试用例的 nn 值之和不超过 2⋅1052\cdot 10 ^ 5。

输出格式

For each test case, if it is impossible for exactly mm elves to remain alive, print −1-1.

Otherwise, output a valid sequence of attacks as follows:

  • On the first line, output an integer kk (0≤k≤n0\le k\le n) — the number of iterations in Mass Hysteria.
  • Then kk lines follow, the ii-th line containing two integers xix_i and yiy_i (1≤xi,yi≤n1\leq x_i,y_i\leq n, xi≠yix_i\neq y_i), indicating that elf xix_i attacks elf yiy_i in the ii-th iteration.

The sequence must satisfy all of the following conditions:

  • Immediately before the ii-th iteration, both elves xix_i and yiy_i are alive and elf xix_i has not attacked in any previous iteration.
  • After the kk-th iteration, exactly mm elves are alive and there does not exist a pair of distinct living elves xx and yy such that elf xx has not attacked before.

If there are multiple answers, you may output any of them. Any valid sequence satisfying the above conditions will be accepted.

对于每个测试用例,若不可能恰好有 mm 个精灵存活,则输出 −1-1。

否则,按如下格式输出一个合法的攻击序列:

  • 第一行输出一个整数 kk(0≤k≤n0\le k\le n)——表示“群体恐慌”(Mass Hysteria)的迭代次数;
  • 接下来 kk 行,第 ii 行包含两个整数 xix_i 和 yiy_i(1≤xi,yi≤n1\leq x_i,y_i\leq n,且 xi≠yix_i\neq y_i),表示在第 ii 次迭代中,精灵 xix_i 攻击精灵 yiy_i。

该序列必须满足以下所有条件:

  • 在第 ii 次迭代开始前,精灵 xix_i 和 yiy_i 均存活,且精灵 xix_i 在此前所有迭代中均未发动过攻击;
  • 在第 kk 次迭代结束后,恰好有 mm 个精灵存活,且不存在一对互异的存活精灵 xx 和 yy,使得精灵 xx 在此之前从未发动过攻击。

若存在多个合法答案,可输出任意一个。任何满足上述条件的合法序列均可被接受。

输入输出样例

  • 输入#1

    7
    4 2
    1 4 2 3
    2 2
    6 7
    3 0
    1 2 3
    3 1
    1 2 3
    3 2
    1 2 3
    4 1
    2 3 4 5
    6 0
    998244353 1000000000 314159265 676767677 999999999 987654321

    输出#1

    2
    3 1
    2 4
    -1
    2
    3 2
    1 3
    2
    1 2
    3 2
    -1
    2
    1 4
    4 2
    4
    3 1
    2 5
    6 1
    4 2

说明/提示

In the first test case, one possible sequence of attacks is shown below:

xx

yy

Health values after attack

Elves that have attacked

0

—

—

[1,4,2,3][1, 4, 2, 3]

[][]

1

33

11

[−1,4,1,3][-1, 4, 1, 3]

[3][3]

2

22

44

[−1,1,1,−1][-1, 1, 1, -1]

[2,3][2, 3]

After 22 iterations, only elves 22 and 33 are alive. Since both of them have already attacked, no further valid attack is possible, and Mass Hysteria terminates.

In the second test case, the only possible choices for (x,y)(x,y) in the first iteration are (1,2)(1,2) or (2,1)(2,1). In either case, elf 11 ends up with −1-1 health, so it is impossible for both elves to remain alive at the end. Note that Mass Hysteria will last at least one iteration as there exists a valid (x,y)(x,y) for the first iteration.

In the sixth test case, only elf 33 is alive after all attacks. Even though elf 33 has not attacked before, Mass Hysteria terminates since there is no other elf it can attack.

在第一个测试用例中,一种可能的攻击序列如下所示:

xx

yy

攻击后的生命值

已发动攻击的精灵

0

—

—

[1,4,2,3][1, 4, 2, 3]

[][]

1

33

11

[−1,4,1,3][-1, 4, 1, 3]

[3][3]

2

22

44

[−1,1,1,−1][-1, 1, 1, -1]

[2,3][2, 3]

经过 22 轮迭代后,仅剩精灵 22 和 33 存活。由于它们均已发动过攻击,因此不再存在合法的攻击操作,“群体恐慌”(Mass Hysteria)终止。

在第二个测试用例中,第一轮迭代中 (x,y)(x,y) 的唯一可能选择为 (1,2)(1,2) 或 (2,1)(2,1)。无论哪种情况,精灵 11 的生命值最终均为 −1-1,因此不可能使两个精灵在最后均存活。注意,“群体恐慌”至少持续一轮,因为第一轮迭代中存在合法的 (x,y)(x,y)。

在第六个测试用例中,所有攻击结束后仅剩精灵 33 存活。尽管精灵 33 尚未发动过攻击,但由于不存在其他可攻击的精灵,“群体恐慌”仍会终止。

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

首页