QOJ.ac

QOJ

時間限制: 3 s 記憶體限制: 1024 MB 總分: 100

#7159. 糖果

统计

在古老的伊卡城(Ica),据说有一座富可敌国的宫殿。宫殿内有一条走廊,摆放着 $N$ 盒来自世界各地的糖果。路过的旅行者可以随意拿走糖果,前提是必须支付等同于糖果重量的黄金。

糖果盒从左到右编号为 $0$ 到 $N-1$。第 $i$ 个盒子里剩余 $a_i$ 单位的糖果,其中 $a_i$ 是一个非负整数。

作为宫殿的守护者,你希望移动这些盒子,使得糖果数量较多的盒子更靠近入口。

给定数组 $a_0, a_1, \dots, a_{N-1}$ 以及数字 $F$ 和 $T$。在一次操作中,你可以交换数组 $a_0, a_1, \dots, a_{N-1}$ 中任意两个相邻的元素。请问,为了使数组的前 $F$ 个元素之和至少为 $T$,最少需要进行多少次操作?

输入格式

第一行包含三个整数 $N, F, T$。

第二行包含 $N$ 个整数 $a_0, a_1, \dots, a_{N-1}$。

输出格式

如果无法通过操作达到目标,输出 “NO”。

否则,输出一个整数,表示最少需要的操作次数。

数据范围

  • $1 \le N \le 100$
  • $1 \le F \le N$
  • $0 \le T \le 10^{11}$
  • $0 \le a_i \le 10^9$ 对于 $i = 0, 1, \dots, N-1$

说明:输入中的数字可能无法用 32 位整数存储,如果你使用 C++,请注意溢出问题。

你的解法将在多组测试数据上进行测试,每组测试数据包含若干测试点。为了获得某一组测试数据的分数,你需要通过该组内的所有测试点。

组别 分数 数据范围
1 6 $N \le 2$ 且 $a_i \le 100$ ($i = 0, \dots, N-1$) 且 $T \le 10^9$
2 19 $a_i \le 1$ ($i = 0, \dots, N-1$)
3 16 $N \le 20$
4 30 $a_i \le 100$ ($i = 0, \dots, N-1$)
5 29 无附加限制

样例

样例输入 1

6 2 27
10 4 20 6 3 3

样例输出 1

1

样例说明 1

在第一个样例中,前两个元素之和应至少为 27。这可以通过交换两个相邻元素来实现:交换 4 和 20。交换后,数组变为 10 20 4 6 3 3,此时前两个元素之和为 $10 + 20 = 30 \ge 27$。

样例输入 2

6 5 5000000000
1000000000 1000000000 0 1000000000 1000000000 1000000000

样例输出 2

3

样例说明 2

在第二个样例中,0 必须移动到数组的最末端;这需要三次交换。

样例输入 3

3 2 100
20 30 60

样例输出 3

NO

样例说明 3

在第三个样例中,无法使前两个元素之和至少为 100;我们能做到的最好情况是 $60 + 30 = 90$。

样例输入 4

1 1 100
100

样例输出 4

0

Editorials

IDTypeStatusTitlePosted ByLast UpdatedActions
EditorialOpen Official EditorialQingyu- Download

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.